Previously, we followed the author to review the query algorithm and partial Sorting Algorithm in the data structure. Now we continue to follow the author to learn some basic sorting algorithms.
Select sort
Usage conditions:A set of comparable sizes.
Algorithm idea:Select the smallest or largest element from the data elements to be sorted, and place the elements in sequence at the end of the sorted series until all the data elements to be sorted are arranged.
Example programming:Int B [10] =}
- // Simple selection and sorting
- Void SimpleSelect (int B [10])
- {
- Int temp;
- Int I;
- For (I = 0; I <9; I ++)
- {
- For (int j = I + 1; j <9; j ++)
- {
- If (B [I]> B [j])
- {
- Temp = B [I];
- B [I] = B [j];
- B [j] = temp;
- }
- }
- }
- Cout <"the sort is :";
- For (int I = 0; I <10; I ++)
- {
- Cout <B [I] <"";
- }
- Cout <endl;
- }
Performance analysis:Time Complexity: On ^ 2)
Heap sorting
Usage conditions:A set of comparable sizes.
Algorithm idea:In fact, heap sorting is an evolution of Simple selection and sorting. It is mainly used to reduce the number of comparisons. What is heap? If the sequence is regarded as a Complete Binary Tree, the values of all non-terminal nodes in the full binary tree are not greater than or less than) The values of left and right child nodes can be called heap. By the nature of the heap, we can know that the top of the heap is the largest keyword (or the smallest keyword ). After the output heap top, the remaining elements are built into a heap and then output to the top. After repeated execution, we can get an ordered sequence, which is a heap sorting process.
Heap sorting involves two steps:
Example programming:Int B [10] =}
- // Heap sorting
- Void HeapSort (int B [10])
- {
- Void HeapAdjuest (int B [10], int min, int max );
- Void Sawp (int * a, int * B );
- Int I;
- // Because the binary tree is complete, heap conversion starts from the last non-leaf node.
- For (I = 9/2; I> = 0; I --)
- {
- HeapAdjuest (B, I, 9 );
- }
- // Sort the heap top data from the new heap
- For (I = 9; I> 0; I --)
- {
- Sawp (& B [I], & B [0]);
- HeapAdjuest (B, 0, I-1 );
- }
- }
- // Heap adjustment (large top heap)
- // Adjust the start position of the min data in the array.
- // Adjust the end position of the max data in the data
- Void HeapAdjuest (int B [10], int min, int max)
- {
- If (max <= min) return;
- Int temp;
- Temp = B [min];
- Int j;
- // Extend its child node cycle
- For (j = 2 * min; j <= max; j * = 2)
- {
- // Select its big child
- If (j <max & B [j] <B [j + 1])
- {
- J ++;
- }
- // The child with the heap top less than it will not handle it
- If (temp> B [j])
- {
- Break;
- }
- // Replace a large number with a small one
- B [min] = B [j];
- Min = j;
- }
- B [min] = temp;
- }
- // Exchange functions
- Void Sawp (int * a, int * B)
- {
- Int temp;
- Temp = *;
- * A = * B;
- * B = temp;
- }
✓ Performance analysis:Time complexity O (nlogn)
The merge algorithm is also called the 2-way merge algorithm.
Usage conditions:A set of comparable sizes.
Algorithm idea:Assuming that the initial sequence contains n records, it can be considered as n ordered subsequences. The length of each subsequence is 1, and then the two subsequences are merged, we can get [n/2] characters with a length of 2 or 1. Here the length is 1. Maybe the sequence length is an odd number. Then the last sequence is placed in a single order, so the length is 1 ); merge them in pairs, so repeated until an ordered sequence with a length of n is obtained.
Example programming:Int B [10] =}
- // Merge and sort
- Void MergeSort (int B [10], int d [10], int min, int max)
- {
- // Use and store the sequence obtained from the intermediate shard Region
- Int c [10];
- Void Merge (int c [10], int d [10], int min, int mid, int max );
- If (min = max) d [min] = B [min];
- Else
- {
- // Divide the image into two regions.
- Int mid = (min + max)/2;
- // Merge and sort the region
- MergeSort (B, c, min, mid );
- // Merge and sort the region
- MergeSort (B, c, mid + 1, max );
- // Merge two regions
- Merge (c, d, min, mid, max );
- }
- }
- // Merge the ordered sequence d [min-mid] And d [mid + 1-max] into the ordered sequence c [min-max]
- Void Merge (int c [10], int d [10], int min, int mid, int max)
- {
- Int I, j, k;
- For (I = j = min, k = mid + 1; j <= mid & k <= max; I ++)
- {
- If (c [j]> c [k])
- {
- D [I] = c [k];
- K ++;
- }
- Else
- {
- D [I] = c [j];
- J ++;
- }
- }
- If (j <= mid)
- {
- For (; j <= mid; j ++, I ++)
- {
- D [I] = c [j];
- }
- }
- If (k <= max)
- {
- For (; k <= max; k ++, I ++)
- {
- D [I] = c [k];
- }
- }
- }
Performance analysis:Time complexity O (nlogn)
Summary
Because different sorting methods adapt to different application changes and requirements, the following factors are taken into account when selecting an appropriate sorting method:
So when will so many sorting algorithms be used?
IfN is relatively small, for example, n <= 50 ),AvailableInsert sorting directly or simply select sorting.
IfThe initial sequence state is basically ordered., OptionalInsert sort directly, bubble sort.
IfN price comparisonThe time complexity is O (nlogn:Fast sorting, heap sorting, and Merge Sorting.