Beginner algorithm-C + + implementation of fast sequencing and linear time selection (deterministic Selection)

Source: Internet
Author: User

The fast sort algorithm actually does only two things: looking for a split point (pivot) and exchanging data.

The so-called search for a split point, both to find a point that is expected to be near the middle position, of course, as close as possible to the midpoint better.

The so-called exchange of data, is a smaller than this partition of the data, and ultimately placed on the left side of the split point, bigger than it is placed on the right.

Set to sort the array is a[left] ... A[right], the first arbitrary selection of data (General algorithm: Use random numbers to select a range of numbers. Literary algorithm: Take A[left], A[right], and A[rand ()] of the median. The second algorithm: choose the first number of the array as the key data, and then put all the smaller than its number in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm.

The exact algorithm for fast sorting is:

1) Set two variables I, J, at the beginning of the order: i=left,j=left+1;

2) Take the key data and A[left] Exchange, assign value to key, that is key =a[left];

3) Backward Search from J, that is, start backward search (j + +), find the first a[j [less than key ], swap a[++i] and a[j].

4) Repeat the 3rd step until J>right, at which time the loop ends

5) make int q=i at this time; A[left] ... A[Q-1] and a[q+1] ... A[right] Repeat the 1-4 process until the end of recursion.

In this way, we can redeem it as code:

/** * the quick sort algorithm by c++ * average time  cost: nlogn * author: zheng chen / arc001 * copyright 2015  xi ' an university of posts & telecommunications */#include  < iostream> #include  <ctime> #include  <cstdlib> #include  <fstream>using  Namespace std;long long ans = 0;void swap (INT&NBSP;&AMP;A,&NBSP;INT&NBSP;&AMP;B) {    int c = a;    a = b;     b = c;} Int partition (int a[],int l,int r) {    int t = rand ()% ( R-L); Int x = a[l+t];int i = l;int j = l+1;swap (A[l+t],A[l]); j<=r;j++) {if (a[j]<=x) {++i;swap (a[i],a[j]);}} Swap (a[i],a[l]); return i;} Void quick_sort (INT&NBSp A[],int l,int r) {    if (l<r) {         Int q = partition (a,l,r);         quick_sort (A,l,q-1);         quick_sort (a,q+1,r);     }}int main () {     /*int a[] = {7,6,5,4,3,2,1};    quick_sort (A,0,6);     for (int i=0;i<7;i++)     cout<<A[i]<< '   ';     */    fstream in;    in.open (" QuickSort.txt ");    int *a = new int[10000];     Int i = 0;    for (i=0;i<10000;i++)          in>>a[i];    quick_sort (a,0,9999);     for (i=0;i< 10;i++) &NBSP;&NBSP;&NBSP;&Nbsp;    cout<<a[i]<< '   ';     return 0;} 


As for the linear time selection, it's a little late today, and will be updated tomorrow. If you feel this article has a little help to you, welcome to reprint and collection!



Beginner algorithm-C + + implementation of fast sequencing and linear time selection (deterministic Selection)

Related Article

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.