Divide and conquer the fast Platoon

Source: Internet
Author: User

Fast sorting, the first step to determine a key value key (typically set to the first element), the basic idea is to put a small number than key on the left side of key will be larger than the number of key on the key to the right, so complete a quick row

Next, the same method is applied to the left and right of the key respectively.

Algorithm steps:

1. Select a benchmark key (the first element is typically selected)

2 set two pointers low and high, initially pointing to the first element and the last element

3. Scan high from right to left until you find an element that is smaller than key, move this element to the low position, and then scan from left to right to find a position that is larger than the key element

The condition for the end of a quick line is Low==high

4. Tail work, insert key into low position

5 to the left side of the low to the right respectively recursion, repeat the above procedure

Algorithm complexity of O (NLOGN)

#include <iostream>;
using namespace Std;
void Qsort (int*list, int left, int. right);
int main () {
int n,i;
while (CIN >> N) {
int *a = new Int[n];
for (i = 0; i < n; i++)
CIN >> A[i];
Qsort (A, 0, n-1);
for (i = 0; i < n; i++)
cout << A[i] << "";
cout << Endl;
return 0;
}
}
void Qsort (int*list, int left, int. right) {
/* Exit Condition */
if (left >= right)
Return
Setting key values
int key = List[left];
Two pointer to left and right scan
int low = left and high = right;
while (Low < high) {/* One scan end flag */
/* Find the number on the right larger than key */
while (List[high]>=key&&high > Low)
high--;
List[low] = list[high];//found and moved to the low end
/* Find the number on the left that is smaller than key */
while (list[low]<= Key&&high>low)
low++;
List[high] = List[low]; Find and move to high end
}
List[low] = key;//The last step to insert K, at this time, a quick line is complete
/* Repeat the above method for two sub-sequences recursively, respectively */
Qsort (list, left, low-1);
Qsort (list, low+1,right);
}

Divide and conquer the fast Platoon

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.