Several basic insertion sorts

Source: Internet
Author: User
Tags sorts

1. Insert Sort
Insert sort (Insertion sort) is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it. The insertion sort is implemented on an implementation, usually in the order of In-place (that is, the ordering of extra space using only O (1), so that in the backward-forward scanning process, the ordered elements need to be moved back and forth gradually, providing the insertion space for the newest elements.
Time complexity: O (n^2);
Algorithm Description:
1. Starting with the first element, the element can be thought to have been sorted
2. Take the next element and scan from the back forward in the sequence of ordered elements
3. If the element (sorted) is greater than the new element, move the element to the next position
4. Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element
5. After inserting a new element into the location
6. Repeat steps 2~5
Animated Demo :

Swfung8
Algorithm Demo:

  1. /*
  2. * * Direct Insert 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. Binary Insert Sort
Binary Insert sort (binary insertion sort) is an improvement to the insertion sorting algorithm, which is the process of sequentially inserting elements into sequential sequences. Time complexity O (n^2);
Algorithm Description:
In the process of inserting a new element into an ordered array, when looking for the insertion point, set the first element of the insertion area to A[low] and the end element to A[high], then the wheel will be inserted into the element with A[m], where m= (Low+high)/2 is compared, if larger than the reference element, Select A[low] to a[m-1] for the new insert area (i.e. high=m-1), otherwise select a[m+1] to A[high] for the new insert area (i.e. low=m+1), so that until the Low<=high is not established, all elements after this position will be moved back one bit, and insert the new element into the a[high+1]
Algorithm Demo:

  1. /*
  2. * * Binary Insert Sort
  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 sort
The hill sort, also known as the descending incremental sorting algorithm, is an efficient and improved version of the insertion sort.
Hill sort is a kind of stable sorting algorithm, the time complexity is O (N^3/2);
The hill algorithm proposes an improved method based on the two-point nature of the insertion order:
* Insert sort the efficiency of the linear sequencing can be achieved when the data operation is almost well-arranged;
* But the insertion sort is generally inefficient because the insertion sort can only move the data one bit at a time;
Algorithm Description:
1. First take a positive integer d1 (D1 < n), divide all records into D1 groups, all records with a multiple of D1 are considered as a group, then insert sort within each group
2. Then take D2 (D2 < D1)
3. Repeat the above grouping and sorting operations until you take di = 1 (i >= 1) position, that is, all records become a group, and finally the group is inserted to sort. General election D1 is about N/2,D2 for D1/2, D3 for D2/2, ..., di = 1.
Animated Demo:
1. Suppose there are arrays a = [80, 93, 60, 12, 42, 30, 68, 85, 10], First Take D1 = 4, divide the array into 4 groups, such as the same color in the same group:

2. Then insert the order of 4 groups, and the result is:

3. Then, take d2 = 2 and divide the original array into 2 groups, such as:

4. Then insert the order of 2 groups, and the result is:

5. Finally, take d3 = 1 and get the final result after inserting the sort:

Algorithm Demo:

  1. /*
  2. * * Hill Sort
  3. */
  4. void Shellsort (int a[], int n)
  5. {
  6. int I, j, Gap;
  7. for (gap = N/2; gap>0; gap/= 2)//step
  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. }

Several basic insertion sorts

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.