Fast sequencing of C + + algorithms

Source: Internet
Author: User

Quick Sort

Ideas:

An array, arbitrarily choose a base, bigger than it put his right side, smaller than it put it to the left, a round down divided into two groups, the two groups were again according to the method recursion.

Specific operation:

An array, the first one to do the cardinality out to exist in a variable, set two cursors to begin and end the array, respectively,

Start loop:

Select a and cardinal ratio by the cursor from right to left first, the number is larger than the base of the end of the cursor to the left one bit, smaller than the base of the number is placed on the start of the cursor, and then follow the cursor from left to right to select a number and the base ratio, the number is smaller than the cardinality of the beginning of the cursor to the right one bit, larger than the base on the number of , and then start the cycle anew.

Until two cursors are equal, start recursion.

intMain () {intData[] = { the, $, A, +, the,5, +,Ten,3, -}, K; intlen=sizeof(data)/sizeof(int); QuickSort (data,0, len-1);  for(inti =0; i < Len; i++) {cout<<data[i]<<"\ n"; }}voidQuickSort (intArray[],intBegin,intend) {    if(begin<end) {        intL = begin, R = end, x =Array[begin];  while(L <R) {             while(L<r && array[r]>=x) R--; if(l<R) {Array[l]=Array[r]; L++; }             while(L<r && array[l]<x) L++; if(l<R) {Array[r]=Array[l]; R--; }} Array[l]= Array[r] =x; QuickSort (array, begin, R-1); QuickSort (Array, R+1, end); }}

Fast sequencing of C + + algorithms

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.