Self-write merge sort and call system sort method, C + +

Source: Internet
Author: User

#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 + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.