Public classSorttest {Private Static intarray_number=10; Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//prepare the sequence to be sortedRandom random =NewRandom (); int[] Array =New int[Array_number]; for(inti = 0; i < Array.Length; i++) {Array[i]= Random.nextint (20); } System.out.println ("The initial ordering of the array is as follows: \ n"); System.out.println (arrays.tostring (array)); MergeSort (Array,0,array.length-1); System.out.println ("The merge of arrays is sorted as follows: \ n"); System.out.println (arrays.tostring (array)); } //Two-way merge sort Private Static voidMergeSort (int[] list,intLeftintRight ) { if(Left <Right ) { intMid = (left+right)/2; MergeSort (List,left,mid); MergeSort (List,mid+1, right); Mergeration (List,left,mid,right); } } Private Static voidMergeration (int[] list,intLeftintMidintRight ) { //TODO auto-generated Method Stub inti,j,k; int[] temp =New int[Right-left+1]; K= 0; I=Left ; J=mid+1; while(i<=mid&&j<=Right ) { if(List[i] <=List[j]) {Temp[k+ +] = list[i++]; }Else{temp[k+ +] = list[j++]; } } while(i<=mid) {Temp[k+ +] = list[i++]; } while(j<=Right ) {Temp[k+ +] = list[j++]; } //to copy back into the corresponding segment of the original array after the sequence is sorted for(i=0;i<temp.length;i++) {List[left+i]=Temp[i]; } }}
Two-way merge sort of algorithm