Professional Documents
Culture Documents
Merge
Merge
#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++;
}
mergeSort(left, mid);
mergeSort(right, size-mid);
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;
}