Fast sorting is the most popular and the fastest sorting algorithm (c + + STL's sort function is the implementation of the fast sort); Quick Sort (Quicksort) is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data will be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data to be quickly sorted, the entire sorting process can be recursive , so that the entire data sequence becomes an ordered sequence. Its algorithm is characterized by a pivot (pivot), the left side of the pivot element is less than/equal to the pivot point of the element, the pivot to the right of the elements are larger than the pivot point;
Quick Sorting Algorithm idea:
Set to sort the array is a[0], ..., a[n-1], first arbitrarily select a data as standard (usually choose the last 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 ( In fact, as long as all the elements smaller than his are in front of it, then the latter condition is automatically satisfied), this process is called a quick 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. (Information source: Baidu Encyclopedia)
One Division
Goal:
Find a record with its keyword/subscript as a "pivot/pivot", where the element with a value less than the pivot moves to the record pointed by the pivot, where the record with the keyword greater than the pivot is moved to the record.
After a trip is ordered, the unordered sequence of records is R[s. T] will be split into two parts: R[s. I-1] and r[i+1..t], and
R[j].value≤r[i].value≤r[j].value
//implementation template <typename type>int Partitionby3loop (Type *array, int p, int r) {int i = P; int j = r+1; J: The next position beyond the end of the element is Type x = array[p]; Swap the leftmost element as a PIVOT element//Exchange The <X element to the left area//swap 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 that is smaller than X (<=x) while (Array[--j] > x); if (i >= j) break; Exchange Std::swap (Array[i], array[j]); }//to Exchange pivot elements with Array[p] Std::swap (Array[p], array[j]); Returns the pivot return J;}
/** Description: Almost all of the data structures and algorithms in the textbook partition implementation are similar to the one above, although easy to understand, but the implementation is too complex; An introduction to < algorithm > gives another way to do this, although it's not easy to understand (you'll love her after you understand the principle), but it's easier to achieve!*/template <typename type>int Partitionby1loop (type *array, int p, int r) { type x = Array[r]; X as the final pivot point to the element //i points to the last element to the left of the pivot//that is, with the X their neighbourhood element subscript int i = p-1; J is constantly searching 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 all (i+1) left elements are <=array[i+1], //So all Array[i+2:r] elements are return i+1 greater than array[i+1] ;}
Quick Sort
Firstly, the unordered sequence of records is "once divided", and then the two sub-sequences "recursive" are sorted quickly.
Implement template <typename type>void quickSort (Type *array, int p, int r) { if (P < r) { int pivot = parti Tionby1loop (Array, p, r); QuickSort (array, p, pivot-1); QuickSort (Array, pivot+1, R);} }
time complexity for quick sorting
Assuming that the pivot position i = k is divided once, the time required to make a quick row of N records:
t (n) = {Tpass (n) + t (k-1) + t (n-k) | Tpass (n) the time required to make a split of N Records }
If the keywords recorded in the sequence are randomly distributed, the probability of K taking any one of the values from 1 to n is the same.
The average time required to quickly sort by this is:
Set Tavg (1) ≤b, the result is:
so : the time complexity for quick sorting is O (NLOGN)
If the initial state of a pending record is ordered by the keyword, the fast sort will degenerate into a bubble sort, with a time complexity of O (n^2).
To avoid this, "preprocessing", i.e. R (s) first, is required before a partition is made. Key, R (T). Key and r[? ( S+T)/2?]. Key to compare each other and then take the keyword as the pivot record for the element in the middle of the three elements.
Data Structure Basics (4)--Quick sort