Merge Sorting is actually a sort of choice. The idea is relatively simple. The following is the sample code.
Void Merge (int sourceArr [], int targetArr [], int startIndex, int midIndex, int endIndex) {int I, j, k; for (I = midIndex + 1, j = startIndex; startIndex <= midIndex & I <= endIndex; j ++) {if (sourceArr [startIndex] <sourceArr [I]) {targetArr [j] = sourceArr [startIndex ++];} else {targetArr [j] = sourceArr [I ++];} if (startIndex <= midIndex) {for (k = 0; k <= midIndex-startIndex; k ++) {targetArr [j + k] = sourceArr [startIndex + k];} if (I <= endIndex) {for (k = 0; k <= endIndex-I; k ++) {targetArr [j + k] = sourceArr [I + k] ;}}// recursion is used internally. The spatial complexity is n + lognvoid MergeSort (int sourceArr [], int targetArr [], int startIndex, int endIndex) {int midIndex; int tempArr [100]; // modify the size of if (startIndex = endIndex) as needed) {targetArr [startIndex] = sourceArr [startIndex];} else {midIndex = (startIndex + endIndex)/2; MergeSort (sourceArr, tempArr, startIndex, midIndex); MergeSort, tempArr, midIndex + 1, endIndex); Merge (tempArr, targetArr, startIndex, midIndex, endIndex) ;}// call int main (int argc, char * argv []) {int a [8] = {50, 10, 20, 30, 70, 40, 80, 60}; int B [8]; MergeSort (a, B, 0, 7 ); for (int I = 0; I <sizeof (a)/sizeof (* a); I ++) cout <B [I] <''; cout <endl; system ("pause"); return 0 ;}