Fast sorting of sorting algorithms: Sorting of sorting algorithms

Source: Internet
Author: User

Fast sorting of sorting algorithms: Sorting of sorting algorithms

Definition of Quick Sort:

  Set the array to be sorted to A [0]... A [N-1], first randomly select A data (usually the first number of the array) as the key data, and then put all the smaller than it before it, all the numbers larger than it are placed behind it. This process is called a fast sorting.

Basic Idea:

The idea of fast sorting is the idea of divide governance.

Quick sorting is to find an element (in theory, you can find one) as the benchmark, and then partition the array so that the value of the element on the left of the benchmark is not greater than the benchmark value, the element values on the right of the benchmark are not smaller than the benchmark value, so that the elements used as the benchmark are adjusted to the correct position after sorting. Recursive quick sorting: adjust other n-1 elements to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sorting is complete. Therefore, the core algorithm of the quick sorting algorithm is partitioning, that is, how to adjust the benchmark location and adjust the final location of the returned benchmark for segmentation and recursion.

For example, this may not be easy to understand. Assume that the sequence to be sorted is

2 2 2 4 9 3 6 7 1 5 first use 2 as the benchmark and use the I and j pointers to scan from both sides, separate elements smaller than 2 from those larger than 2. First, compare the values of 2 and 5 to 2, and move j left.

2 2 4 9 3 6 7 1 5 compare 2 and smaller than 2, so put 1 at the 2 position

2 1 4 9 3 6 7 1 5 compare 2 and 4 4 is greater than 2, so move 4 to the back

2 1 4 9 3 6 7 4 5 compare 2 and 7, 2 and 6, 2 and 3, 2 and 9, all greater than 2, meet the conditions, so unchanged

After the first round of fast sorting, the elements become the following

[1] 2 [4 9 3 6 7 5]

Then, the elements on the left side of 2 are arranged in a quick way. Because there is only one element, the quick row ends. On the right side, perform quick sorting and recursion to generate the final result.

Class Program {static void Main (string [] args) {int [] array = new [] {234,632, 23,643, 2, 6,-2,423,234 2, 43}; Console. writeLine ("Before sorting:"); Console. writeLine (string. join (",", array); QuickSort (array, 0, array. length-1); Console. writeLine ("sorted:"); Console. writeLine (string. join (",", array); Console. readKey ();} /// <summary> /// fast sorting /// </summary> /// <param name = "sources"> target array </param> /// <param name = "left"> Minimum array index </param> // <param name = "right"> maximum array index </param> private static void QuickSort (int [] sources, int left, int right) {// terminate recursion. if (left> right) return; int occurrence = sources [left], // reference number low = left, high = right; while (low 

 


C ++ quick sort algorithm code

Let's take a look at Baidu encyclopedia. It's very detailed:
Baike.baidu.com/view/42516.htm

Sort the following arrays using a quick Sorting Algorithm

# Include <iostream>
Using namespace std;
Int Por (int * ar, int l, int h ){
Int k = ar [l];
While (l

While (l

H --;
Ar [l] = ar [h];
While (l

L ++;
Ar [h] = ar [l];
}
Ar [l] = k;
Return l;
}
Void QSort (int * ar, int l, int h, int n ){
If (l

Int m = Por (ar, l, h );
For (int I = 0; I <n; I ++)
Cout <ar [I] <"";
Cout <endl;
QSort (ar, l, M-1, n );
QSort (ar, m + 1, h, n );
}
}
Int main (){
Int n, * ar;
Cin> n;
Ar = new int [n];
For (int I = 0; I <n; I ++)
Cin> ar [I];
QSort (ar, 0, n-1, n );
Return 0;
}
Input:
8
60 56 65 99 22 16 88 100
Output:
16 56 22 60 99 65 88 100
16 56 22 60 99 65 88 100
16 22 56 60 99 65 88 100
16 22 56 60 88 65 99 100
1622 56 60 65 88 99 100

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.