1 Packagesorting;2 3 /**4 * Merge Sort5 * Average O (nlogn), preferably O (nlogn), Worst O (nlogn), Space complexity O (n), stable, more complex6 * @authorZeng7 *8 */9 Public classGuibingpaixu {Ten One Public Static voidMerge (int[] arr,intStartIndex,intMidindex, A intEndIndex) { - int[] Temparr =New int[arr.length]; -System.out.println ("Merge" + StartIndex + "~" +endIndex); the inti = startIndex, j = midindex + 1, k =StartIndex; - while(I! = Midindex + 1 && J! = EndIndex + 1) { - if(Arr[i] >Arr[j]) -temparr[k++] = arr[i++]; + Else -temparr[k++] = arr[j++]; + } A while(I! = Midindex + 1) attemparr[k++] = arr[i++]; - while(J! = EndIndex + 1) -temparr[k++] = arr[j++]; - for(i = startIndex; I <= endIndex; i++) -Arr[i] =Temparr[i]; - for(intP:arr) inSystem.out.print (P + ""); - System.out.println (); to } + - Static voidMergeSort (int[] arr,intStartIndex,intEndIndex) { the intMidindex; * if(StartIndex <EndIndex) { $Midindex = (StartIndex + endIndex)/2;Panax NotoginsengMergeSort (arr, StartIndex, Midindex);//Left Order -MergeSort (arr, Midindex + 1, endIndex);//Right Order the Merge (arr, StartIndex, Midindex, endIndex); + } A } the + Public Static voidMain (string[] args) { - int[] B = {49, 38, 65, 97, 76, 13, 27, 50 }; $MergeSort (b, 0, b.length-1); $ for(inti:b) -System.out.print (i + "")); - the } -}
Sorting algorithm Java implementation (merge sort)