Merge sorting (merge) is the merging of two (or more than two) ordered tables into a new ordered table, which divides the ordered sequence into a number of sub-sequences, each of which is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If you combine two ordered tables into an ordered table, it is called a 2-way merge.
The merge sort algorithm is stable, the array needs O (n) extra space, the list needs O (log (n)) extra space, time complexity is O (Nlog (n)), the algorithm is not adaptive, does not require random reading of the data.
Working principle:
1, the application space, so that its size is the sum of two sorted sequences, the space used to store the combined sequence
2, set two pointers, the initial position is the starting position of two sorted series
3. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position
4. Repeat step 3 until a pointer reaches the end of the sequence
5. Copy all remaining elements of another sequence directly to the end of the merge sequence
Run code
1 PackageCom.zc.manythread;2 3 ImportJava.util.Random;4 /**5 * Merge Sort6 * @authoreven my yes7 *8 */9 Public classMergeSort {Ten One Public Static voidMergeSort (int[] data) { ASort (data, 0, data.length-1); - } - the Public Static voidSortint[] Data,intLeftintRight ) { - if(Left >=Right ) - return; - //Find the intermediate index + intCenter = (left + right)/2; - //recursive to the left array + sort (data, left, center); A //recursive to the right array atSort (data, center + 1, right); - //Merging - merge (data, left, center, right); - print (data); - } - in /** - * Merges two arrays, merges the first 2 arrays in order, and then merges them into order to * + * @paramData - * Array Object the * @param Left * * Index of the first element of the left array $ * @paramCenterPanax Notoginseng * Index of the last element of the left array, center+1 is the index of the first element of the right array - * @param Right the * Index of the last element of the right array + */ A Public Static voidMergeint[] Data,intLeftintCenterintRight ) { the //Temporary Array + int[] Tmparr =New int[data.length]; - //The first element index of the right array $ intMID = center + 1; $ //third records the index of a temporary array - intThird =Left ; - //caches the index of the first element of the left array the intTMP =Left ; - while(Left <= Center && mid <=Right ) {Wuyi //Take the smallest of two arrays into a temporary array the if(Data[left] <=Data[mid]) { -tmparr[third++] = data[left++]; Wu}Else { -tmparr[third++] = data[mid++]; About } $ } - //The remainder is placed in a temporary array (actually two while only one is executed) - while(Mid <=Right ) { -tmparr[third++] = data[mid++]; A } + while(Left <=Center) { thetmparr[third++] = data[left++]; - } $ //copies the contents of the temporary array back to the original array the //(The contents of the original Left-right range are copied back to the original array) the while(TMP <=Right ) { theDATA[TMP] = tmparr[tmp++]; the } - } in the Public Static voidPrintint[] data) { the for(inti = 0; i < data.length; i++) { AboutSystem.out.print (Data[i] + "\ T"); the } the System.out.println (); the } + /** - * Generate a random array the * @paramCountBayi * @return the */ the Private Static int[] CreateDate (intcount) { - int[] Data=New int[Count]; -Random Rand =NewRandom (); the Boolean[] bool =New Boolean[100]; the intnum = 0; the for(inti = 0; I < count; i++) { the Do { - //if the resulting number is the same, continue the loop thenum = rand.nextint (100); the} while(Bool[num]); theBool[num] =true;94 thedata[i]=num; the } the returndata;98 } About Public Static voidMain (string[] args) { - int[] data = CreateDate (10);101 print (data);102 mergesort (data);103System.out.println ("Sorted array:");104 print (data); the }106 107}
Operation Result:
Merge sort Algorithm--java