#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#include <algorithm>
#include <ctime>
#define LENGTH 10
using namespace Std;
BOOL Compare (int a, int b)
{
return a > B;
}
void mergesorted (int *array, int start, int end);
int main ()
{
Srand (Time (NULL));
int* Array;
Array = (int *) malloc (sizeof (int) * length);
for (int i = 0;i < length; i++)
{
Array[i] = rand ()%100;
cout<<array[i]<< "";
}
cout<<endl;
Call system comes with sort function
Sort (array, array+10, compare);
cout<< "Result:" <<endl;
for (int i = 0;i < length; i++)
{
cout<<array[i]<< "";
}
cout<<endl;
Mergesorted (array,0,length);
return 0;
}
void mergesorted (int *array,int start, int end)
{
if (End-start <= 1)
{
Return
}
int mid = (end + start)/2;
Mergesorted (Array,start, mid);
Mergesorted (Array, Mid, end);
The first half of the first start, the back only may be larger than the J before, can not be compared to J before the small
int j = 0;
Departing from the last half
for (int i = mid; I < end; i++)
{
for (; J < Mid; J + +)
{
if (Array[i] < array[j])
{
Shift
int temp = Array[i];
for (int i1 = i; i1 > J;)
{
ARRAY[I1] = Array[--i1];
}
ARRAY[J] = temp;
}
}
}
}
Self-write merge sort and call system sort method, C + +