#include<stdio.h> #include<stdlib.h> void merge(int left[],int right[],int a[],int half); void mergesort(int a[],int n); int main() { int a[6]={3,4,8,1,2,4}; int i; mergesort(a,6); for(i=0;i<6;i++) printf("%d\t",a[i]); return 0; } void merge(int left[],int right[],int a[],int half) { int i=0,j=0,k=0; while(i<half&&j<half) { if(left[i]<right[j]) { a[k]=left[i]; i++; } if(right[j]<left[i]) { a[k]=right[j]; j++; } } if(i==half) { for(j=j;j<half;j++) { a[k]=right[j]; k++; } } if(j==half) { for(i=i;i<half;i++) { a[k]=left[i]; k++; } } void mergesort(int a[],int n) { int half=n/2,i; int left[half]; int right[half]; for(i=0;i<half;i++) left[i]=a[i]; for(i=0;i<half;i++) right[i]=a[i]; mergesort(left,half); mergesort(right,half); merge(left,right,a,half); } }
merge
mergesort
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)