Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Merge problem
/** * Divide : Divide the n-element array into two n/2-element * sub-arrays * Conquer : Sort the two sub-arrays recursively using * merge sort * Combine : Merge the two sorted subsequences to form the * sorted array **/ #include<stdio.h> int arr[20]; // array to be sorted int main() { int n,i; clrscr(); printf("Enter the size of array\n"); // input the elements scanf("%d",&n); printf("Enter the elements:"); for(i=0; i<n; i++) scanf("%d",&arr[i]); merge_sort(arr,0,n-1); // sort the array printf("Sorted array:"); // print sorted array for(i=0; i<n; i++) printf("%d",arr[i]); getch(); return 0; } int merge_sort(int arr[],int low,int high) { int mid; int i; if(low<high) { mid=(low+high)/2; // Divide and Conquer merge_sort(arr,low,mid); merge_sort(arr,mid+1,high); // Combine merge(arr,low,mid,high); } return 0; } int merge(int arr[],int l,int m,int h) { int arr1[10],arr2[10]; // Two temporary arrays to //hold the two arrays to be merged int n1,n2,i,j,k,z; i=0;j=0; for(z=l;z<=m;z++) { arr1[i]=arr[z]; i++; } for(z=m+1;z<=h;z++) { arr2[j]=arr[z]; j++; } n1=i; n2=j; i=0; j=0; for(k=l; k<=h; k++) { //process of combining two sorted arrays if(i<n1 && j<n2) { if(arr1[i]<=arr2[j]) arr[k]=arr1[i++]; else arr[k]=arr2[j++]; } else if(i>=n1) { arr[k]=arr[j++]; } else { arr[k]=arr[i++]; } } return 0; }
run
|
edit
|
history
|
help
0
mutable constexpr
single_digit
Reverse factorial
Updated Linked Lists - 5/10/2017 V4.0
water drop/ water land
Teatime Snack
4149 coj WIP
LRU - Set Sol
Different Subarray Sum Problem
gcc set_terminate