It can be seen that both sorts are divided into different methods. Gradually divide a large array into smaller arrays and then sort them.
The difference is that the merge sort takes a small-to-large strategy, merging two small arrays into a new sorted array, merging a complete array gradually.
The quick sort is from big to small, find a scalar inside the array, left small right oita separated, keep each number so operation, also sorted out the entire array.
Merge sort:
Public classmergesortstandard{ Public Static voidMain (string[] arg) {int[] test={1,3,2,4,7,5,6,-1,9,0}; int[] Result=sort (test,0,test.length-1); System.out.println (arrays.tostring (result)); } Public Static int[] Sort (int[] num,intLowintHigh ) { intMid = (Low+high)/2; if(low<High ) {sort (num,low,mid); Sort (Num,mid+1, high); MergeSort (Num,low,mid,high); } returnnum; } Public Static voidMergeSort (int[] num,intLowintMidintHigh ) { intI=Low ; intJ=mid+1; intK=0; int[] temp=New int[High-low+1]; while(I<=mid && J<=high && i<j) {if(num[i]<Num[j]) {Temp[k++]=num[i++]; }Else{temp[k++]=num[j++]; } } while(i<=mid) {temp[k++]=num[i++]; } while(j<=High ) {Temp[k++]=num[j++]; } for(intL = 0; L < Temp.length; l++) {Num[low+l]=Temp[l]; } }}
Quick Sort
Public classQuiksort { Public Static voidMain (string[] args) {} Public voidSortint[] num,intLeftintRight ) { if(left>Right ) { return; } inttemp=Num[left]; intI=Left ; intj=Right ; intswaptemp; while(I!=J) {//If you i=j, you need to put the sentry in the middle. while(Num[j]>=temp && i<j) {J--; }//from the right to the small to the left, because the Sentinel on the left, so start from the right to find while(Num[i] <= temp && i<j) {I++; }//from the left, find the big, put it on the right. if(i<j) {Swaptemp=Num[i]; Num[i]=Num[j]; NUM[J]=swaptemp; }} Num[left]=num[i];//put the sentry in the middle .num[i]=temp; Sort (Num,left,i-1);//Sentinel to the left of the sortSort (num,i+1,right);//Sentinel to the right of the sort }}
Quick Sort, Merge sort