diff --git a/sorting/merge_sort.c b/sorting/merge_sort.c index 6533713f..ea2bfba3 100644 --- a/sorting/merge_sort.c +++ b/sorting/merge_sort.c @@ -1,91 +1,92 @@ #include +#include -void swap (int *a,int *b)//To swap the variables// +void swap(int *a, int *b) //To swap the variables// { int t; - t= *a; - *a=*b; - *b=t; - + t = *a; + *a = *b; + *b = t; } -void merge(int a[],int l,int r,int n)//To merge // -{ int *b = (int*)malloc(n*sizeof(int)); -int c=l; - int p1,p2; - p1 = l;p2=((l+r)/2)+1; - while ((p1<((l+r)/2)+1) &&(p2 a[r]) + swap(&a[l], &a[r]); + } + else if (l == r) { - if (a[l]>a[r]) - swap(&a[l],&a[r]); - } - else if(l==r) - {} else - {mergesort(a,n,l,(l+r)/2); - mergesort(a,n,((l+r)/2)+1,r); - merge(a,l,r,n); - + { + merge_sort(a, n, l, (l + r) / 2); + merge_sort(a, n, ((l + r) / 2) + 1, r); + merge(a, l, r, n); } - } -int main(void) { //main function// -int *a,n,i; -scanf("%d",&n); -a = (int*)malloc(n*sizeof(int)); -for (i=0;i