Data structure Learning (13), quick sort

Source: Internet
Author: User

The basic idea: to divide the pending records into two separate sections by a single pass, in which some of the recorded keywords are smaller than the keywords in the other part of the record.

You can continue to sort the two parts separately, repeat operation above, has reached the whole sequence of the purpose of order

void QuickSort (SqList *l) {    QSort (l,1,l->length);}  
/*L->dara[low the subsequence of the sequence table L: High] Fastest sort*/void QSort (SqList *l,int low,int high ) { int pivot; if (low< high) {pivot = Partition (L,low,high); / * will l->data[low. High] in Split, and calculate the demarcation point * /QSort (l,low,pivot-1);  /* order to the left of the demarcation point * /QSort (l,pivot+1,high); / * sort on the right of the dividing point */ }}              
/*Swaps the records of a neutron meter so that the pivot record is in place and returns its position at this time, the record before it is small (smaller) than it* /int Partition (sqlist *l,int Low,Int. High ) { int pivotkey; pivotkey = l->R[low];  while (low< high) { while (Lowpivotkey) high--; Swap (L,low, High); while (Lowpivotkey) low++; swap (L,low,high);} return low ;}                 

Improved algorithm:

1. Optimize Pivot selection

Three numbers in the method, that is, the first choice of three keywords to sort, the middle number as a pivot, generally take the left, middle, and right end three numbers.

/*swaps the records of a neutron meter so that the pivot record is in place and returns its position at this time, the record before it is small (smaller) than it*/ intPartition (SqList *l,intLowintHigh ) {    intPivotKey; intm = low + (high-low)/2;/*calculates the subscript of an array's intermediate elements*/    if(L->data[low]>l->data[high])/*swap around to ensure the left side of the smaller*/swap (L,low,high); if(L->data[m]>l->data[high])/*swap middle and right side to guarantee the middle of the smaller*/swap (L,m,high); if(L->data[low]>l->data[m])/*swap the middle and left side to ensure that the left side is smaller*/swap (L,m,low); PivotKey= l->data[m];/*pivot with the middle value of the child table*/L->data[0] = PivotKey;/*back up the axis keywords to l->data[0]*/     while(low<High ) {         while(LowPivotKey) High--; L->data[low] = l->data[high];/*Replace instead of swap*/         while(Lowpivotkey) Low++; L->data[high] = l->data[low];/*Replace instead of swap*/} L->data[low] = l->data[0];/*Replace pivot value back to L->data[low]*/    returnLow ;}

Optimize for unnecessary switching

Data structure Learning (13), quick 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.