Merging and sorting is an O (nlogn) algorithm. its basic idea is a grouping strategy. division is performed first and then merged. The following is an example. There is such a group of data, {5, 4, 12, 32, 45, 21}. if you sort it by merging, first separate it from the middle. in this way, it is divided into two arrays: {5, 4,} {12, 32, 45, 21 }.
Merging and sorting is an O (nlogn) algorithm. its basic idea is a grouping strategy. division is performed first and then merged. The following is an example.
There is such a group of data, {5, 4, 12, 32, 45, 21}. if you sort it by merging, first separate it from the middle. in this way, it is divided into two arrays: {5, 4,} {12, 32, 45, 21 }.
These two arrays are also divided step by step until they cannot be further divided (each sub-array has only one element). In this way, the division process ends.
The division process is shown in:
Next, we perform the merge operation. according to the division process, the process is from top to bottom, and the merge process is from bottom to top, for example, medium, bottom {5 }, {4} arrays. if they are sorted in ascending order, the merged array is {4, 5 }. {1} and {22} are merged into {1, 22 }. The arrays {} and {} belong to the same branch, and they also need to be merged. because these two sub-arrays are in order, the merging process is, select the smallest element from the two child arrays to be merged, and put the element into the merged array. The first two arrays are merged and then }. And so on until the data is merged to the top layer. this is the data sorting completed.
Shows the merge process. This process is from bottom up.
The C language implementation code is as follows:
# Include
// Merge void merge (int data [], int start, int mid, int end) {int * tmpLeft, * tmpRight; int leftSize, rightSize; int l, r, j; printArray (data, 8); printf ("\ n"); l = 0; r = 0; j = 0; leftSize = mid-start + 1; rightSize = end-mid; tmpLeft = (int *) malloc (leftSize * sizeof (int); tmpRight = (int *) malloc (rightSize * sizeof (int )); while (j <leftSize) {tmpLeft [j] = data [start + j]; j ++;} j = 0; while (j <rightSize) {tmpRight [j] = data [mid + 1 + j]; j ++;} j = 0; while (l <leftSize & r <rightSize) {if (tmpLeft [l] <tmpRight [r]) {data [start + j ++] = tmpLeft [l ++];} else {data [start + j ++] = tmpRight [r ++] ;}} while (l <leftSize) {data [start + j ++] = tmpLeft [l ++];} while (r <rightSize) {data [start + j ++] = tmpRight [r ++];} free (tmpLeft); free (tmpRight);} void merge_sort (int data [], int start, int end) {int mid; if (start <end) {// divides the array into mid = (start + end)/2; merge_sort (data, start, mid ); merge_sort (data, mid + 1, end); // combine the divided two arrays merge (data, start, mid, end );}}
Javascript version:
Function merge (left, right) {var result = []; while (left. length> 0 & right. length> 0) {if (left [0] <right [0]) {result. push (left. shift (); // extract the smallest one first and put it in the result set} else {result. push (right. shift () ;}} return result. concat (left ). concat (right); // The rest is merge, so that the order is sorted.} function mergeSort (array) {if (array. length = 1) {return array;} var middle = Math. floor (array. length/2), // Obtain the midpoint left = array. slice (0, middle), // split array right = array. slice (middle); return merge (mergeSort (left), mergeSort (right); // recursive merge and sort}
Ruby version:
def merge(left, right)final = []until left.empty? or right.empty?final << ( left.first < right.first ? left.shift : right.shift )endfinal + left + rightenddef mergeSort(array)return array if array.size < 2left = array.first(array.size/2)right = array.last(array.size - array.size/2)merge(mergeSort(left), mergeSort(right))end
Runable version: