Quick Sort, Merge sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.