Select a sorting algorithm quickly
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 <stdio. h>
- # Include <stdlib. h>
- # Include <iostream>
- Using namespace std;
-
- Int partition (int arr [], int low, int high)
- {
- Int key;
- Key = arr [low]; // defines A variable key. Next, we divide array A into two parts based on the value of the key.
- While (low
- {
// Scan left from the array element pointed to by high. At the same time, the array elements whose subscript is high are compared with the benchmark value key in sequence; // until high is not greater than low or the first array element smaller than the base value key is found, then assign this value to the while (low
Void quick_sort (int arr [], int start, int end)
{
Int pos = 0;
If (start <end)
{
Pos = partition (arr, start, end );
Quick_sort (arr, start, pos-1 );
Quick_sort (arr, pos + 1, end );
}
Return;
}
- 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" <endl;
- For (I = 0; I <N; I ++)
- Cout <arr [I] <endl;
- Quick_sort (arr, 0, N-1 );
- Cout <"array elements after sorting output" <endl;
- For (I = 0; I <N; I ++)
- Cout <arr [I] <endl;
- System ("pause ");
-
- Return 0;
- }
# Include <stdio. h> # include <stdlib. h> # define N 6int partition (int arr [], int low, int high) {int key; key = arr [low]; while (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.
What is the idea of selecting a sorting algorithm?
Select the maximum value of the current sequence for each time:
For example, 1, 3, 6, 9, and 5 are sorted in ascending order!
Step 1
Step 2, 1
Step 3, 3, 5
Step 4, 1, 5, 6
Step 5, 1, 5, 6, 9
// Progressively select the implementation of sorting!
Void SelectSort (double * head, int amount)
{
Double temp;
Int m, n;
For (m = 0; m <amount-1; m ++)
{
For (n = m + 1; n <amount; n ++)
{
If (head [n] {
Temp = head [n];
Head [n] = head [m];
Head [m] = temp;
}
}
}
}
Bubble, direct insertion, selection, fast, Hill, Merge Sorting Algorithm for comparison
(1) Bubble Sorting
Bubble Sorting is to call a small element forward or a large element backward. The comparison is an adjacent comparison between two elements, and the Exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not be bored to exchange them. If the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the Bubble Sorting is a stable sorting algorithm.
(2) Select sorting
The sorting mode selects the smallest element for each position. For example, you can specify the smallest element for the first position, select the second smallest element for the second element in the remaining element, and so on, the n-th element does not need to be selected until the n-th element, because only one of its largest elements is left. If the current element is smaller than an element, and the small element appears after an element equal to the current element, then the stability will be damaged after the exchange. Compare interfaces. For example, in the sequence 5 8 5 2 9, we know that the first selection of 1st elements 5 will exchange with 2, therefore, the relative order of the two 5 in the original sequence is damaged. Therefore, selecting sorting is not a stable sorting algorithm.
(3) Insert sorting
Insert sorting inserts an element at a time based on an ordered small sequence. Of course, at the beginning, this ordered small sequence had only one element, which was the first element. The comparison starts from the end of the ordered sequence, that is, the element to be inserted is compared with the already ordered sequence. If it is larger than it, it is directly inserted after it, otherwise, search until you find the inserted position. If you encounter an element that is equal to the inserted element, the inserted element is placed behind the element that you want to insert. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting, so insertion sorting is stable.
(4) Fast sorting
There are two directions for quick sorting. The I subscript on the left is always directed to the right. When a [I] <= a [center_index], center_index is the array subscript of the central element, it is generally set to an array of 0th elements. The j subscript on the right goes to the left, when a [j]> a [center_index]. If I and j cannot move, I <= j, exchange a [I] And a [j], repeat the above process until I> j. Exchange a [j] And a [center_index] to complete a quick sorting. When the central element is exchanged with a [j], it is very likely to disrupt the stability of the preceding elements. For example, the sequence is 5 3 3 4 3 8 9 10 11, now the exchange of central elements 5 and 3 (5th elements, subscript starting from 1) will disrupt the stability of element 3, so fast sorting is an unstable sorting algorithm, instability occurs when the central element is exchanged with a [j.
(5) Merge and sort
Merge Sorting refers to recursively dividing a sequence into short sequences. The recursive exit means that a short sequence has only one element (that is, directly ordered) or two sequences (one comparison and exchange ), then, the ordered segments are merged into an ordered long sequence until all the original sequences are sorted. It can be found that when one or two elements, one element will not be exchanged. If two elements are equal in size, no one will intentionally exchange them, which will not damage stability. So, in the process of merging short ordered sequences, is stability damaged? No. During the merge process, we can ensure that if the two current elements are equal, we store the elements in the previous sequence before the result sequence, thus ensuring stability. Therefore, Merge Sorting is also a stable sorting algorithm.
(6) Base sorting
Base sorting is sorted first by low position, then collected; then sorted by high level, and then collected; and so on until the highest bit. Sometimes some attributes have a priority order. They are first sorted by low priority and then by high priority. The final order is the highest priority, and the highest priority is the highest priority. Base sorting is based on separate sorting and collected separately, so it is a stable sorting algorithm.
(7) shell)
Hill sorting sorts elements based on different step sizes. When there are no elements at the beginning... the rest of the full text>