Fast sequencing of C + + algorithms

Source: Internet
Author: User

The idea of quick sequencing:

By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Main ideas:

First, from the back of the loop to find a value smaller than the key value, put this value in front of the key, and then start from the front to find a value larger than key;

The code is as follows:

QuickSort.cpp: Defines the entry point of the console application.  #include "stdafx.h" #include <iostream>using namespace std;void QuickSort (int a[], int start,int end) {if (start    > End) {return;    } int i = start;    int j = END;    int k = A[i];        while (I < J) {while (I < J && A[j] > K)//First find the value of less than K j--;        A[i] = a[j];//Find a value less than K, replace A[i] while (i < J && A[i] < K)//from the front to find a value greater than k i++; A[J] = a[i];//found a value greater than K, just a[j] The value has been assigned just the value of a[i];a[j] is now found in the value of greater than K fill} a[j] = k;//last i = j;    Assign the value of K to A[i];    QuickSort (A,i+1,end); QuickSort (a,start,i-1);}    int _tmain (int argc, _tchar* argv[]) {int a[] = {1,3,5,7,9,2,4,6,8,0};    int len=sizeof (a)/sizeof (A[0])-1;     for (int i=0;i<=len;i++) {cout<<a[i]<< "";    } cout<<endl;    cout<< "Quick sort after:" <<endl;    Len=sizeof (a)/sizeof (A[0])-1;    QuickSort (A,0,len); for (int i=0;i <= len;i++) {cout<<a[i]<< "";    } getchar (); return 0;}


Fast sequencing of C + + algorithms

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.