Quick sorting and quick sorting algorithms

Source: Internet
Author: User

Quick sorting and quick sorting algorithms

Quick sorting uses the idea of divide and conquer. First, find a benchmark number and divide the number into two parts. One part is larger than the benchmark number, and the other part is smaller than the benchmark number. Then, it can be implemented recursively...

// Quick sorting
# Include <iostream>
Using namespace std;
Void Sort (int * a, int left, int right );
Int main (){
Int n, I;
Cin> n;
Int * p = new int [n];
For (I = 0; I <n; I ++)
Cin> p [I];
Sort (p, 0, n-1 );
For (I = 0; I <n; I ++)
Cout <p [I] <"";
Cout <endl;
Delete [] p;
Return 0;
}
Void Sort (int * a, int left, int right ){
If (left> = right) // condition for loop bounce
Return;
Int key;
Int low = left, high = right;
Key = a [low]; // select a number as the base
While (low While (a [high]> = key & low High --;
A [low] = a [high]; // move the number smaller than the key to the front of the key
While (a [low] <= key & low Low ++;
A [high] = a [low]; // move the number greater than the key to the right of the key
}
A [low] = key; // Insert the key to the center
Sort (a, left, low-1); // recursion on the left of the key
Sort (a, low + 1, right); // recursion on the right of the key
}

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.