Home > Back-end >  Merging algorithm is run out of the million little problem (save small white
Merging algorithm is run out of the million little problem (save small white

Time:09-18

 # include & lt; stdio.h> 
#include
Void mergearray (int a [], int first, int mids, int last, int temp []);
Void mergesort (int a [], int first, int last, int temp []);
Bool MergeSort (int a [], int n);

Int main ()
{
Int a [6]={4, 5, 6, 1, 2, 3};
if (! MergeSort (a, 6))
{
exit(0);
}
for (int i=0; i <6; I++)
{
Printf (" % d ", a [I]);
}
Return 0;
}

Void mergearray (int a [], int first, int mids, int last, int temp [])
{
Int I=first;
Int j=mid + 1;
Int k=first;
While (i<=mid & amp; & J<=last)
{
If (a [I] {
Temp=[k++] a [i++];
}
The else
Temp=[k++] a [j++];
}
While (I & lt; First)
{
Temp=[k++] a [i++];
}
While (j & lt; The last)
{
Temp=[k++] a [j++];
}
For (int m=first; M & lt;=last;)
{
A [m++]=\ [m++];
}
}

Void mergesort (int a [], int first, int last, int temp [])
{
If (first & lt; The last)
{
Int mid=+ last (first)/2;
Mergesort (a, first, mid, temp);
Mergesort (a, mid + 1, the last, temp);
Mergearray (a, first, mid, last, temp);
}
}
Bool MergeSort (int a [], int n)
{
Int * p=new int [n].
If (p==NULL)
{
return false;
}
The else
{
Mergesort (a, 0, n - 1, p);
The delete [] p;
return true;
}

}


CodePudding user response:

I already know where is wrong,,,
  • Related