0% found this document useful (0 votes)
29 views2 pages

Merge

This C++ program implements merge sort recursively and iteratively to sort arrays. It includes functions to merge sorted subarrays, recursively split arrays and merge halves, iteratively split arrays into pairs and merge, and print sorted arrays. The main function takes user input for the array, calls the appropriate sorting function, and prints the result.

Uploaded by

Rijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Merge

This C++ program implements merge sort recursively and iteratively to sort arrays. It includes functions to merge sorted subarrays, recursively split arrays and merge halves, iteratively split arrays into pairs and merge, and print sorted arrays. The main function takes user input for the array, calls the appropriate sorting function, and prints the result.

Uploaded by

Rijit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#define SIZE 10
using namespace std;

void merge(int arr[], int left[], int leftSize, int right[], int
rightSize){
int i=0, j=0, k=0;
while(i<leftSize && j<rightSize){
if(left[i]<=right[j]){
arr[k] = left[i];
i++;
}
else{
arr[k] = right[j];
j++;
}
k++;
}
while(i<leftSize){
arr[k]=left[i];
i++;
k++;
}
while(j<rightSize){
arr[k]=right[j];
j++;
k++;
}

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


if(size<=1)
return;
int mid = size/2;
int left[mid];
int right[size-mid];

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


left[i]=arr[i];
for(int j=mid; j<size; j++)
right[j-mid]=arr[j];

mergeSort(left, mid);
mergeSort(right, size-mid);

merge(arr, left, mid, right, size-mid);

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


if(size <= 1)
return;
for(int currSize=1; currSize<size; currSize=2*currSize){
for(int start=0; start<size-1; start+=2*currSize){
int mid = start + currSize - 1;
int end = min(start + 2 * currSize - 1, size - 1);
int leftSize = mid - start + 1;
int rightSize = end - mid;
int left[leftSize], right[rightSize];
for(int i=0; i<leftSize; i++)
left[i] = arr[start + i];
for(int i=0; i<rightSize; i++)
right[i] = arr[mid + 1 + i];
merge(arr + start, left, leftSize, right, rightSize);
}
}
}

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


for(int i=0; i<size;i++)
cout<<arr[i]<<", ";
cout<<endl;
}

int main(){
int arr[SIZE], choice;
cout<<"Enter the array to be sorted: "<<endl;
for(int i=0; i<SIZE; i++){
cin>>arr[i];
}
do {
cout<<"0.Exit"<<endl;
cout << "1.Recursive"<<endl;
cout<<"2.Iterative"<<endl;
cin >> choice;
switch (choice) {
case 0:
break;
case 1:
mergeSort(arr, SIZE);
cout<<"Sorted array by Recursion is: "<<endl;
printArray(arr, SIZE);
break;
case 2:
mergeSortIterative(arr, SIZE);
cout<<"Sorted array by Iteration is:"<<endl;
printArray(arr, SIZE);
break;
default:
cout <<"Wrong choice, try again!"<<endl;
}
}
while(choice!=0);
return 0;
}

You might also like