Data Structure BASICS (4): Data Structure Basics

Source: Internet
Author: User

Data Structure BASICS (4): Data Structure Basics

Fast sorting is the most popular and the fastest Sorting Algorithm (C ++ STL's sort function is to implement fast sorting); quick sorting) it is an improvement for Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data sequence into an ordered sequence. The algorithm features a pivot. The elements on the left of the pivot are less than/equal to the elements pointed to by the pivot. The elements on the right of the pivot are greater than those pointed to by the pivot;

 

Idea of the Quick Sort Algorithm:

Set the array to be sorted to A [0],..., A [N-1], first randomly select A data as standard (usually the last 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 (in fact, as long as all the elements smaller than it are placed before it, the next condition is automatically met ), this process is called quick sorting. It is worth noting that quick sorting is not a stable sorting algorithm, that is, the relative positions of multiple identical values may change at the end of the algorithm. (Source: Baidu encyclopedia)


One division

Objectives:

Find a record and use its keyword/subscript as "pivot/forward". All elements whose values are less than the pivot Value move to the record pointed to by the pivot, any record with a keyword greater than pivot is moved to the record.

The unordered record sequence R [s .. t] is divided into two parts: R [s .. i-1] and R [I + 1 .. t], and

R [j]. value ≤ R [I]. value ≤ R [j]. value

// Implement template <typename Type> int partitionBy3Loop (Type * array, int p, int r) {int I = p; int j = r + 1; // j: type x = array [p] next position beyond the end element; // use the leftmost element as the pivot element // exchange the <x element to the Left area // exchange the> x element to the right area while (true) {// find an element larger than x (> = x) while (I <r & array [++ I] <x ); // find an element smaller than x (<= x) while (array [-- j]> x); if (I> = j) break; // exchange std :: swap (array [I], array [j]);} // exchange the pivot element with array [p] std: swap (array [p], array [j]); // returns the pivot return j ;}
/** Note: the implementation of Partition in almost all data structures and algorithms in China is similar to the above one. Although it is easy to understand, it is too complicated to implement; <Introduction to algorithms> provides another implementation method. Although this method is not easy to understand (you will fall in love with her after understanding its principles), it is easy to implement! */Template <typename Type> int partitionBy1Loop (Type * array, int p, int r) {Type x = array [r]; // x points to the final pivot element. // I points to the last element on the left of the pivot. // It is the subscript int I = p-1 of the element adjacent to x; // j continues to search for the next <= x element for (int j = p; j <r; ++ j) {if (array [j] <= x) {++ I; std: swap (array [I], array [j]) ;}} std: swap (array [I + 1], array [r]); // eventually make all elements on the left of (I + 1) <= array [I + 1], // Therefore, all arrays [I + 2: the elements of r] are all greater than the return I + 1;} of array [I + 1 ;}

Quick sorting

First, the unordered record sequence is divided once, and then the two subsequences are sorted recursively.

// Implement template <typename Type> void quickSort (Type * array, int p, int r) {if (p <r) {int minute = partitionBy1Loop (array, p, r); quickSort (array, p, audio-1); quickSort (array, audio + 1, r );}}

Time complexity of quick sorting

Assuming that the Pivot Position I = k obtained by one division, the time required for fast sorting of n records is as follows:

T (n) = {Tpass (n) + T (k-1) + T (n-k) | Tpass (n) is the time required for one division of n records}

If the keywords recorded in the columns to be sorted are randomly distributed, k is likely to take any value from 1 to n.

Therefore, the average time required for quick sorting is:

If Tavg (1) is less than or equal to B, the following result is displayed:

 

Therefore, the time complexity of fast sorting is O (nlogn)

If the initial state of the record to be sorted is sorted by keyword, the quick sorting is sorted by bubble, and the time complexity is O (n ^ 2 ).

In order to avoid this situation, we need to "pre-process" before a division, that is, first R (s ). key, R (t ). key and R [random (s + t)/2 random]. key, and then take the element in the center of the three elements as the pivot record.

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.