1. Sorting
Sorting is an important operation in computer programming, so it is an important task to study and study various sorting algorithms.
2. Type of sorting
Depending on the number of sorted records and the memory in the sorting process, sorting can be divided into internal and external sorting.
Internal sorting: Refers to the process of storing a sorted record in the computer's memory for sorting.
External sort: Refers to the number of records with sorting so large that the memory does not hold all records at once, and the sorting process of the external access is still required during the sorting process.
3. Sort stability
About the stability of the sort: if the first 2 equal numbers in the order of the sequence and after the ordering of their two before and after the order of the same position, is a stable sort, otherwise it is an unstable sort.
What are the benefits of stable sequencing?
Stable sequencing can reduce the number of exchanges, which is obvious, because if the two number is the same, we do not need to exchange two numbers, unless it is a special occasion.
4. Stability analysis of common sorting algorithms
This article only discusses the stability of the internal ordering, the following analysis of the stability of the common sorting algorithm:
(1) Bubble sort
The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.
(2) Insert sort
An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. Of course, there are only 1 elements at the beginning of this ordered sequence, which is the first element. The comparison starts at the end of the ordered sequence, that is, the element that you want to insert and the one that is already in order, is inserted directly behind it if it is larger than it is, or until it is found where it is inserted. If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.
(3) Merge sort
The merge sort is to divide the sequence recursively into the short sequence, the recursive exit is the short sequence only 1 elements (think directly ordered) or 2 sequences (1 comparison and exchange), then merges each ordered segment sequence into an orderly long sequence, merges continuously until the original sequence is all ordered. It can be found that when 1 or 2 elements, 1 elements are not exchanged, and 2 elements are equal in size and no one is intentionally exchanged, which does not destabilize the stability. So, in the process of merging short ordered sequences, is stability compromised? No, we can guarantee that if the two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, which guarantees stability. Therefore, the merge sort is also a stable sorting algorithm.
(4) Base order
The cardinality sort is sorted by low, then collected, sorted by high order, then collected, and so on, until the highest bit. Sometimes some properties are prioritized, sorted by low priority, then sorted by high priority, and the final order is high priority high, high priority is the same low-priority high. Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.
(5) Select sort
The choice of sorting is to choose the smallest current element for each location, such as selecting the smallest one for the first position, selecting the second small for the second element in the remaining element, and so on, until the n-1 element, the nth element is not selected, because it is left with one of its largest elements. Then, in a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the post-swap stability is destroyed. Compare the awkward, for example, sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the selection of sorting is not a stable sorting algorithm.
(6) Quick Sort
The quick sort has two directions, the left I subscript goes right, when A[i] <= A[center_index], where Center_index is the array subscript of the central element, generally takes the No. 0 element of an array. and the right J subscript goes left, when a[j] > A[center_index]. If I and J are not moving, I <= J, Exchange A[i] and A[j], repeat the process above until I > J. Exchange A[j] and A[center_index], to complete a quick sort of a trip. When the central element and A[j] are switched, it is very possible to disrupt the stability of the previous element, such as the sequence is 5 3 3 4 3 8 9 10 11, now the central element 5 and 3 (5th element, subscript from 1) The exchange will be the stability of element 3 is disturbed, so fast sorting is an unstable sorting algorithm, Instability occurs at the moment of the exchange of central elements and a[j].
(7) Hill sort (shell)
Hill sort is the insertion of elements according to different steps, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; When the elements are basically ordered, the step size is very low, and the insertion sort is very efficient for ordered sequences. So, the time complexity of hill sorting would be better than O (n^2). Because of the number of insertions, we know that one insert sort is stable and does not change the relative order of the same elements, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the shell sort is unstable.
(8) Heap Sorting
We know that the heap structure is the child of node I is 2 * I and 2 * i + 1 nodes, the large top heap requires that the parent node is greater than or equal to its 2 child nodes, and the small top heap requires the parent node to be less than or equal to its 2 child nodes. In a sequence of n, the process of heap sequencing is to select the largest (large top heap) or the smallest (small top heap) from the beginning of N/2 and its child nodes by a total of 3 values, and the choice between the 3 elements will of course not destabilize. But when for N/2-1, N/2-2, ... 1 When these parent nodes select an element, the stability is broken. It is possible that the N/2 parent exchange has exchanged the latter element, while the N/2-1 parent node does not exchange the subsequent same element, then the stability between the 2 identical elements is destroyed. Therefore, heap sorting is not a stable sorting algorithm.
Summarize:
Stable sorting algorithm: bubble sort, insert sort, merge sort, base sort
Unstable sorting algorithm: Select sort, quick sort, hill sort, heap sort
Stability of the internal sorting algorithm