Several Basic insert sorting and insert sorting
1. Insert sorting
Insertion Sort is a simple and intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements.
Time Complexity: O (n ^ 2 );
Algorithm Description:
1. Starting from the first element, this element can be considered to have been sorted
2. Retrieve the next element and scan the sorted element sequence from the back to the front.
3. If the element (sorted) is greater than the new element, move the element to the next position.
4. Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.
5. Insert the new element to this position.
6. Repeat Step 2 ~ 5
Animation demonstration:
By Swfung8
Algorithm Demonstration:
- /*
- ** Insert directly to sort
- */
- Void InsertSort (int a [], int len)
- {
- Int I, j, key;
- For (I = 1; I <len; ++ I ){
- Key = a [I];
- For (j = I-1; j> = 0; -- j ){
- If (a [j]> key)
- A [j + 1] = a [j];
- Else
- Break;
- }
- A [j + 1] = key;
- }
- }
2. Semi-insert sorting
Binary insertion sort is an improvement on the insertion sorting algorithm. The so-called sorting algorithm process is to insert elements into the sequence with the sorted order in sequence. Time complexity O (n ^ 2 );
Algorithm Description:
When inserting a new element into an sorted array, when looking for the insertion point, set the first element of the area to a [low]. when the last element is set to a [high], the elements to be inserted during the round comparison will be compared with a [m], where m = (low + high)/2, if it is larger than the reference element, select a [low] To M-1] as the new inserted area (that is, high = s-1 ), otherwise, select a [m + 1] to a [high] as the new insert area (that is, low = m + 1) Until low <= high is not true, move all elements after this position one by one and insert the new element into a [high + 1].
Algorithm Demonstration:
- /*
- ** Semi-insert sorting
- */
- Void BinsertSort (int a [], int len)
- {
- Int I, j;
- Int low, high, mid;
- Int key;
- For (I = 1; I <len; I ++ ){
- Key = a [I];
- Low = 1; high = I-1;
- While (low <= high ){
- Mid = (low + high)/2;
- If (key <a [mid])
- High = mid-1;
- Else
- Low = mid + 1;
- }
- For (j = I-1; j> = high + 1; -- j)
- A [j + 1] = a [j];
- A [high + 1] = key;
- }
- }
3. Hill sorting
Hill sorting, also known as the descending incremental sorting algorithm, is an efficient improved version of insert sorting.
Hill sorting is a stable Sorting Algorithm with the time complexity of O (n ^ 3/2 );
The hill algorithm proposes an improvement method based on the following two attributes of insertion sorting:
* When inserting and sorting data that has almost been sorted, the efficiency is high to achieve Linear sorting efficiency;
* Insertion sorting is generally inefficient because insertion sorting can only move one bit of data at a time;
Algorithm Description:
1. Take a positive integer d1 (d1 <n) and divide all records into d1 groups. All records whose distance is multiples of d1 are regarded as a group, and insert and sort the records in each group.
2. Then take d2 (d2 <d1)
3. Repeat the preceding grouping and sorting operations until the di = 1 (I> = 1) position is obtained, that is, all records are grouped into a group, and insertion and sorting of the group are performed. Generally, d1 is about n/2, d2 is d1/2, d3 is d2/2 ,..., Di = 1.
Animation demonstration:
1. suppose there is an array a = [80, 93, 60, 12, 42, 30, 68, 85, 10], first take d1 = 4, divide the array into four groups, for example, the same color indicates a group:
2. Insert and sort the four groups respectively. The sorting result is as follows:
3. Then, take d2 = 2 and divide the original array into two groups, for example:
4. insert and sort the two groups respectively. The sorting result is as follows:
5. Finally, take d3 = 1 and sort the inserts to get the final result:
Algorithm Demonstration:
- /*
- ** Hill sorting
- */
- Void ShellSort (int a [], int n)
- {
- Int I, j, gap;
- For (gap = n/2; gap> 0; gap/= 2) // step size
- For (I = 0; I <gap; ++ I ){
- For (j = I + gap; j <n; j + = gap ){
- If (a [j] <a [j-gap]) {
- Int temp = a [j];
- Int k = j-gap;
- While (k> = 0 & a [k]> temp ){
- A [k + gap] = a [k];
- K-= gap;
- }
- A [k + gap] = temp;
- }
- }
- }
- }
4. example code:
- Int main ()
- {
- Int I;
- Int a [] = {80, 93, 60, 12, 42, 30, 68, 85, 10 };
- InsertSort (a, sizeof (a)/sizeof (a [0]);
- For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
- Printf ("% d", a [I]);
- Printf ("\ n ");
- BinsertSort (a, sizeof (a)/sizeof (a [0]);
- For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
- Printf ("% d", a [I]);
- Printf ("\ n ");
- ShellSort (a, sizeof (a)/sizeof (a [0]);
- For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
- Printf ("% d", a [I]);
- Printf ("\ n ");
- Return 0;
- }