Reference URL: http://blog.csdn.net/morewindows/article/details/6678165
MergeSort.cpp: Defines the entry point of the console application. Merge sort # include "stdafx.h" #include <cstdlib>//merges two ordered arrays void Mergearray (int * array,int first,int mid,int Last,int * temp) {int i = First;int J = Mid + 1;int k = First;while (i <= mid && J <= last) {if (Array[i] < array[j]) { temp[k++] = array[i++];} else{temp[k++] = array[j++];}} Insert the remainder into the while (I <= mid) {temp[k++] = array[i++];} while (J <= last) {temp[k++] = array[j++];} Let array arrays partially order for (int i = First, I <= last; i++) {array[i] = Temp[i];}} void merge (int * Array,int first,int last,int * temp) {if (last > First) {int mid = (first + last)/2;merge (Array,first, MID,TEMP);//The first half of the ordered merge (array,mid+1,last,temp);//The right half part is ordered. Mergearray (array,first,mid,last,temp);}} void mergesort (int * array,int num) {int * Temparray = (int *) malloc (sizeof (int) * num); merge (Array,0,num-1,temparray); fre E (temparray);} static int testarray[] = {33,22,99,77,66,44,55,88,11,100};int _tmain (int argc, _tchar* argv[]) {mergesort (testarray,10) ; for (int i = 0; i < 10; i++) {printf ("%d\t", Testarray[i]);} return 0;}
Look at the data structure write code (64) Merge sort