Merge Sort:- In this technique list is divided into equal halves and merge them in sorted form.
Code:
Code:
#include<stdio.h> int x[10]={10,40,20,50,30,60,70,100,90,80}; void merge(int low,int mid,int high) { int i,j,k,t[20]; i=low; j=mid+1; k=low; while(i<=mid && j<=high) { if(x[i]>=x[j]) t[k++]=x[j++]; else t[k++]=x[i++]; } while(i<=mid) t[k++]=x[i++]; while(j<=high) t[k++]=x[j++]; for(i=low;i<=high;i++) x[i]=t[i]; } void div(int low,int high) { int mid; if(low!=high) { mid=(low+high)/2; div(low,mid); div(mid+1,high); merge(low,mid,high); } } void main() { int i,n=10; printf("\nList of elements"); for(i=1;i<n;i++) printf("\n%d",x[i]); div(0,n-1); printf("\nAfter Sorting"); for(i=0;i<n;i++) printf("\n%d",x[i]); getch(); }
Output:

Explanation:

No comments:
Post a Comment