Several Basic insert sorting and insert sorting

Source: Internet
Author: User

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:

  1. /*
  2. ** Insert directly to sort
  3. */
  4. Void InsertSort (int a [], int len)
  5. {
  6. Int I, j, key;
  7. For (I = 1; I <len; ++ I ){
  8. Key = a [I];
  9. For (j = I-1; j> = 0; -- j ){
  10. If (a [j]> key)
  11. A [j + 1] = a [j];
  12. Else
  13. Break;
  14. }
  15. A [j + 1] = key;
  16. }
  17. }

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:

  1. /*
  2. ** Semi-insert sorting
  3. */
  4. Void BinsertSort (int a [], int len)
  5. {
  6. Int I, j;
  7. Int low, high, mid;
  8. Int key;
  9. For (I = 1; I <len; I ++ ){
  10. Key = a [I];
  11. Low = 1; high = I-1;
  12. While (low <= high ){
  13. Mid = (low + high)/2;
  14. If (key <a [mid])
  15. High = mid-1;
  16. Else
  17. Low = mid + 1;
  18. }
  19. For (j = I-1; j> = high + 1; -- j)
  20. A [j + 1] = a [j];
  21. A [high + 1] = key;
  22. }
  23. }

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:

  1. /*
  2. ** Hill sorting
  3. */
  4. Void ShellSort (int a [], int n)
  5. {
  6. Int I, j, gap;
  7. For (gap = n/2; gap> 0; gap/= 2) // step size
  8. For (I = 0; I <gap; ++ I ){
  9. For (j = I + gap; j <n; j + = gap ){
  10. If (a [j] <a [j-gap]) {
  11. Int temp = a [j];
  12. Int k = j-gap;
  13. While (k> = 0 & a [k]> temp ){
  14. A [k + gap] = a [k];
  15. K-= gap;
  16. }
  17. A [k + gap] = temp;
  18. }
  19. }
  20. }
  21. }

4. example code:

    1. Int main ()
    2. {
    3. Int I;
    4. Int a [] = {80, 93, 60, 12, 42, 30, 68, 85, 10 };
    5. InsertSort (a, sizeof (a)/sizeof (a [0]);
    6. For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
    7. Printf ("% d", a [I]);
    8. Printf ("\ n ");
    9. BinsertSort (a, sizeof (a)/sizeof (a [0]);
    10. For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
    11. Printf ("% d", a [I]);
    12. Printf ("\ n ");
    13. ShellSort (a, sizeof (a)/sizeof (a [0]);
    14. For (I = 0; I <sizeof (a)/sizeof (a [0]); ++ I)
    15. Printf ("% d", a [I]);
    16. Printf ("\ n ");
    17. Return 0;
    18. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.