dimanche 29 mai 2016

Garbage value in MergeSorting

I keep getting a garbage value while i am performing a merge sort. I am a beginner kindly help me sort this problem. Here is my code.

#include<bits/stdc++.h>

int Merge_Sort(int a[], int p,int q)
{
 int b[50];//buffer array
 int i,j,x,mid=(p+q)/2;//initial variables
 for(i=p,j=mid+1,x=p;i<=mid,j<q;)
 {
  if(a[j]<=a[i])
    b[x++]==a[j++];
  else
    b[x++]==a[i++];
 }
//copying the rest of array a into b.
 while(i<=mid)
 {
  b[x++]=a[i++];
 }
 while(j<=q)
 {
  b[x++]=a[j++];
 }

 for(int i=p;i<q;i++)
  a[i]=b[i];
}

int Merge_Split(int a[],int p,int q)
{
 int mid;
 if (p-q<2)
  return
 mid=(p+q)/2;
 Merge_Split(a,p,mid);
 Merge_Split(a,mid+1,q);
 Merge_Sort(a,p,q);
}


int main()
{

 int a[5];
 for(int i=0;i<5;i++)
  scanf("%d", &a[i]);
 Merge_Sort(a,0,4);
 for(int i=0;i<5;i++)
  printf("%d", a[i]);
 return 0;
}

This code should work fine. I am not able to find out my problem. Kindly help

Aucun commentaire:

Enregistrer un commentaire