Quick Selection of sorting algorithms
Quick sorting is an improvement of the Bubble sorting method.
1 sorting thought:
Sort the records to be sorted into two separate parts. the keywords of some records are smaller than those of other records; then, sort the two parts of the records in the next segment to achieve the entire sequence order. Repeat the above Division operations until all the data to be sorted changes to the order.
We may not have A deep understanding of fast sorting based on the basic idea. Next we will take n unordered Series A [0], A [1]…, A [n-1] uses the fast sorting method for ascending order as an example.
(1) define two variables, low and high, respectively set low and high as the starting element of the sequence to be sorted and the subscript of the last element. For the first time, the values of low and high are 0 and n-1, respectively. The values of next time are determined by the subscript of the starting element and the last element of the sequence obtained by division.
(2) define A variable key. Next, use the value of the key as the base to divide array A into the left and right parts. Normally, the key value is the first element value to be sorted. The value of the first time is A [0], and the value of the second time is determined by the starting element of the sequence to be split.
(3)
Scan left from the array element pointed to by high, and compare the array elements whose subscript is high with the benchmark value key, until high is not greater than low or finds the first array element smaller than the base value key, and then assigns the value to the array element pointed to by low.
(4) If low is still less than high, the array elements pointed to by low start scanning to the right, at the same time, the array element values with the subscript of low are compared with the benchmark value key until low is not less than high or the first array element greater than the benchmark value key is found, then, assign the value to the array element pointed to by high.
(5) Repeat Step (3) (4) Until the planting of low is no less than high. After successful division, the left and right sides of the result are A [low ...... Pos-1] And A [pos + 1 ...... High], where, the value of the array element corresponding to the pos subscript is the benchmark value key for division, so the array element whose subscript is pos must be assigned as the key at the end of division.
(6) divide the left and right parts into A [low ...... Pos-1] And A [pos + 1 ...... High] continue to use the above steps for division until an ordered sequence is obtained.
In order to deepen the reader's understanding, we will use a piece of code to understand the specific implementation method of quick sorting.
Copy text-only new window
# Include
# Include
# Include
Using namespace std; int partition (int arr [], int low, int high) {int key; key = arr [low]; // defines a variable key, next, the value of the key is used to divide array A into the left and right parts while (low
= Key) high --; arr [low] = arr [high]; // If low is still less than high, scan the right from the array element to which low points, at the same time, the array element values with the subscript of low are compared with the base value key; // until low is not less than high or finds the first array element greater than the base value key, and then assigns the value to the array element pointed to by high. While (low
Void quick_sort (int arr [], int start, int end) {int pos = 0; if (start
Int main (void) {int I = 0; int arr [] = {32, 12, 7, 78, 23,45}; int N = sizeof (arr) /sizeof (arr [0]); cout <"output of elements in the array before sorting" <
# Include
# Include
# Define N 6int partition (int arr [], int low, int high) {int key; key = arr [low]; while (low
= Key) high --; if (low
Running result:
Sorting first 32 12 7 78 23 45 sorting last 7 12 23 32 45 78
In the above Code, a quick sorting algorithm is implemented step by step based on the steps described above. Next, we will demonstrate the first partitioning operation.
In the first division operation, perform initial settings first. The key value is the benchmark for division, and its value is the first element value of the number of groups to be divided, in the preceding sorting sequence, the first element value is 32, and low is set as the subscript of the first element in the array to be sorted. The value is 0 in the first sorting operation, set high to the subscript of the last element of the sequence to be sorted, and set the first value to 5 in the sequence above. First, compare the array element with the key whose subscript is high. Because the element value is greater than the key, the high element moves to the left to continue scanning. Since the next value is 23, less than the key value, 23 is assigned to the array element pointed to by the subscript low. Next, move low to the right position, and compare the values of the array elements to which low points with the key. The values of the following 12 and 7 are smaller than the key, so low continues the right shift scan, until the subscript low points to the value of the array element is 78, that is, greater than the key, 78 is assigned to the array element pointed to by the subscript as high, and the high is shifted to a left position. Next, the Division ends because low is no longer smaller than high. Note that during the division, the scan value is compared with the key value. If the value is smaller than the key value, the value is assigned to another element in the array, the value of this element has not changed. We can see this, so we need to assign the key value of the benchmark to the array element whose subscript is pos at the end of the division. This element is no longer involved in the next division operation.
First Division operation
After the first round of division, we obtain the sequence A [0], A [1], A [2], A [4], A [5], continue to divide the sequence, that is, the sequence of the two parts obtained after the sequence wheel is divided until the sequence is obtained.