Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Alert when this question is answered

× Download the Chegg Study App GE T


★★★★★ (80K+)

  Textbook Solutions Expert Q&A Practice 

home / study / engineering / computer science / computer science questions and answers / write...
Find solutions for your homework

Question: Write down C++ code for Merge Sort Algorithm.


The algorithm should sort the data in ascending ord...

Write down C++ code for Merge Sort Algorithm. The algorithm should sort the data
in ascending order. The input data should of size 8. Once a program starts, user should be provided
an option to input the data randomly. Once the sorting operation is completed the output should be
displayed along with the number of computations it took to complete the whole sorting process.

1 Comment

Expert Answer

Anonymous
answered this

Here is the code..

/******************************************************************************

Online C++ Compiler.

*******************************************************************************/

#include <iostream>
using namespace std;
int c=0;
void merge(int arr[], int start, int mid, int end) {

int len1 = mid - start + 1;


int len2 = end - mid;
int leftArr[len1], rightArr[len2];

for (int i = 0; i < len1; i++)


leftArr[i] = arr[start + i];
for (int j = 0; j < len2; j++)
rightArr[j] = arr[mid + 1 + j];

int i, j, k;
i = 0;
j = 0;
k = start;

while (i < len1 && j < len2) {


if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < len1) {


arr[k] = leftArr[i];
i++;
k++;
}

while (j < len2) {


arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(int arr[], int start, int end) {
if (start<end) {
c++;
int mid = start + (end - start) / 2;

mergeSort(arr, start, mid);


mergeSort(arr, mid + 1, end);

merge(arr, start, mid, end);


}
}

void display(int arr[], int size) {


for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
cout<<"number of comparisions: "<<c <<endl;
}

int main() { int n,i=0,arr[10];


cout<<"Enter the lenth of array: ";
cin>> n;
cout<<"Enter "<<n << " elements for array. ";
for(i=0;i<n;i++)
{cin>> arr[i] ;}
int size = n;
cout << "Original array \n";
display(arr, size);

mergeSort(arr, 0, size - 1);

cout << "Sorted array \n";


display(arr, size);
return 0;
}

for variable lenth of array, you can introduce vector in the code [here I am using xed lenth for the array
(i.e. 10)].

0 Comments

Was this answer helpful? 0

0
Up next for you in Computer Science

Write a python program Hi, Please could anyone


that prompts the user for be of assistance on this
the names of two text Algorithm assignment?
les and compare the Thank you In this
contents of the two les assignment, you are See more quest
to see if they are the required to be for subjects you s
same. If they are, the researching some sorting
scripts should simply algorithms available and

See answer See answer

Questions viewed by other students

Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two
arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both
with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the
two arrays as arguments. The method returns a merged array with size equal...
See answer

Build an application which populates (Builds) BST and then saves all nodes data into a le data.dat by
using object IO. Each node should have following data Patient Name Father Name Age NIC(Numeric)
Address Disease Doctor Name Patient ID(Numeric). NIC and patient ID should be unique. Choice should
be given to the user either build BST on basis of NIC or Patient ID.
See answer 100% (1 rating)

Show more 
COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

You might also like