Quick sorting and quick sorting algorithms
The basic idea of quick sorting is to sort by one click, select an element as the pivot, then place all elements smaller than the pivot to the left of the pivot, And put elements larger than the pivot to the right of the pivot, such sorting is also called a division. Then the Left and Right subsequences of the pivot are divided separately, so that recursion. In terms of average time, quick sorting is currently considered as the best internal sorting method. The average time is O (nlogn), and the worst case is O (n2) (n-party). The worst case is to sort the data in the forward order after the descending order.
The C ++ code is as follows:
# Include "stdafx. h "# define MAXSIZE 20 typedef struct {int r [MAXSIZE + 1]; int len;} SqList; int Partition (SqList & L, int low, int high) {L. r [0] = L. r [low]; // use the first element as the pivot int struct tkey = L. r [low]; // record the pivot keyword while (low -- High; // locate the first element forward from the high position that is smaller than the pivot. r [low] = L. r [high]; // place the element found smaller than the pivot to the idle position on the front side while (low + + Low; // locate the first element greater than pivot from the low position. r [high] = L. r [low]; // place the element found greater than the pivot value to the idle position on the back.} L. r [low] = L. r [0]; // return low to the idle position in the middle;} void QSort (SqList & L, int low, int high) {if (low
// Attach a division process, the first behavior index, the second behavior is to sort the sequence // 0 1 2 3 4 5 6 7 // _ 49 38 65 97 76 13 27 // when the start, the number 0 is idle (low: 1, high: 7) // 49 _ 38 65 97 76 13 27 // use element 1st as the pivot and place it in position 0 and redundancy at position 1 (low: 1, high: 7) // 49 27 38 65 97 76 13 _ // found 27 is smaller than 49, put the value of 7 to 1, and the value of 7 is redundant (low: 1, high: 7) // 49 27 38 _ 97 76 13 65 // found 65 is bigger than 49, put the value of 3 to the value of 7, and the value of 3 is redundant (low: 3, high: 7) // 49 27 38 13 97 76 _ 65 // found 13 is smaller than 49, put the value of 6 to 3, and the value of 6 is redundant (low: 3, high: 6) // 49 27 38 13 _ 76 97 65 // It is found that 97 is bigger than 49. Place the value of 4 to the value of 6, and the value of 4 is redundant (low: 4, high: 5) // _ 27 38 13 49 76 97 65 // low = high, place the Pivot Position on the 4 th. At this time, the left side of 49 is smaller than 49, and the right side is larger than 49.