Sort-merge sort
Basic idea: It means merging two or more two ordered tables into a new ordered table.
Specific steps:
(1 First, the entire table is considered to be N ordered sub-table, each child table has a length of 1.)
(2) Then 22 merge, get n/2 a length of 2 ordered sub-table.
(3) Then 22 merges until a sequential table of length n is reached.
Average Time: O (NLOGN)
Best case: O (NLOGN)
Worst case scenario: O (N2)
Auxiliary Space: O (N)
Stability: Stable
applicable scenario:N Large time
Code implementation:
1 Public Static voidMergeSort (int[] list) {2MergeSort (list, 0, list.length-1);3 }4 5 Private Static voidMergeSort (int[] list,intLeftintRight ) {6 if(Left >=Right )7 return;8 intCenter = (left + right)/2;9 mergesort (list, left, center);TenMergeSort (list, center + 1, right); One merge (list, left, center, right); A } - - Private Static voidMergeint[] list,intLeftintCenterintRight ) { the int[] Tmplist =New int[list.length]; - intMID = center + 1; - intK =Left ; - intTMP =Left ; + while(Left <= Center && mid <=Right ) { - if(List[left] <=List[mid]) +tmplist[k++] = list[left++]; A Else { attmplist[k++] = list[mid++]; - } - } - - while(Left <=Center) { -tmplist[k++] = list[left++]; in } - while(Mid <=Right ) { totmplist[k++] = list[mid++]; + } - //saves the data in the temporary array to the original array the while(TMP <=Right ) { *LIST[TMP] = tmplist[tmp++]; $ }Panax Notoginseng}
Merge sort-java