Quick Sort, Sort basics

Source: Internet
Author: User
Tags array sort

Fast sorting is the best way to sort the average performance at the moment, and is preferred as an array sort.

The basic idea: to divide an array into the left and right parts with Q as the bounds, the left part is less than Q, the lower part is greater than q; Next, divide and conquer the two parts according to this rule, until it cannot be divided, the array is sorted.

Key: How to divide the left and right parts? Q Which value should I take? (with my stupidity, the approach to the introduction of the algorithm did not understand, so forced himself to think of a solution)

Analysis: Since this array is not sorted (order random), it is better to let the first value of the array is q, smaller than Q on the left, larger than Q to the right, but there is a skill, if the first and then the Q, if compared to the second, if smaller than q to the entire array backwards to insert this small value, If larger than Q, you need to move the array forward.

So instead of setting a variable to the value of the temp p, the first position is empty, first, looking forward to a small value than Q to fill in the first position, then this position will be empty (this position should be used to put a higher value than Q), Then the previous backward looking for a larger value than Q fill in this position (the space time out of the position should be used to put a smaller than Q value), so loop until the previous backward and from the back forward coincident, this position is the position of P (left than Q small, the right is larger than Q)

intDevide (int*a,intLowintHigh ) {    inttemp =A[low];  while(low<High ) {         while(temp<a[high]&&high>Low ) { High--; }//A[low] = A[high];//low++;        if(high>Low ) {A[low]=A[high]; Low++; }         while(temp>a[low]&&high>Low ) { Low++; }//A[high] = A[low];//high--;         if(high>Low ) {A[high]=A[low]; High--; }} A[low]=temp; returnLow ;}

Divide and Conquer:

void quick_sort (int *a,int Low,int. High ) {    if(low< High ) {        int q = devide (a,low,high);        Quick_sort (a,low,q-1);        Quick_sort (a,q+1, high);    }}

Test code:

#include <iostream>using namespacestd;intDevide (int*a,intLowintHigh ) {    inttemp =A[low];  while(low<High ) {         while(temp<a[high]&&high>Low ) { High--; }//A[low] = A[high];//low++;        if(high>Low ) {A[low]=A[high]; Low++; }         while(temp>a[low]&&high>Low ) { Low++; }//A[high] = A[low];//high--;         if(high>Low ) {A[high]=A[low]; High--; }} A[low]=temp; returnLow ;}voidQuick_sort (int*a,intLowintHigh ) {    if(low<High ) {        intQ =devide (A,low,high); Quick_sort (A,low,q-1); Quick_sort (A,q+1, high); }}intMain () {inta[ the];  for(intI=0;i<Ten; i++) {cin>>A[i]; } quick_sort (A,0,9);  for(intI=0;i<Ten; i++) {cout<<a[i]<<" "; }    return 0;}

Quick Sort, Sort basics

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.