C + + Several basic insertion sort (graphics and text) _c language

Source: Internet
Author: User

1. Insert Sort

The Insert sort (insertion sort) is a simple and intuitive sort algorithm. It works by building an ordered sequence, scanning the sorted sequence for unsorted data, and finding the location and inserting it. Insert sorting on implementation, usually using in-place sorting (that is, just use the extra space of O (1) to sort), and in the process of backward scanning, you need to repeatedly move the sorted elements backwards and forwards, providing insert space for the newest elements.
Complexity of Time: O (n^2);

Algorithm Description:

1. Starting with the first element, the element can be considered to have been sorted
2. Take out the next element and scan backwards in the sorted sequence of elements
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 sorted element is less than or equal to the new element's position
5. After inserting the new element into the location
6. Repeat steps 2~5

Animation Demo:


Author: Swfung8

Algorithm Demo:


/* * * * Direct Insert 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. Binary Insert Sort

binary insertion ordering (binary insertion sort) is an improvement to the insert sort algorithm, the so-called sort algorithm process, in which elements are constantly inserted into the sequence of the preceding sequences. Time complexity O (n^2);

Algorithm Description:

In the process of inserting a new element into a sorted array, when the insertion point is found, the first element of the area to be inserted is set to A[low], and the end element is set to A[high], then the element and a[m will be inserted when the wheel is compared, where the m= (Low+high)/2 comparison, if larger than the reference element, Select A[low] to a[m-1] for the new insertion area (that is, high=m-1), or select a[m+1] to A[high for the new insertion area (that is, low=m+1), so that until the Low<=high is not established, all elements will be moved back one after this position, and inserts the new element into the a[high+1]

Algorithm Demo:


/* * * * binary Insert sort
/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 sort

The hill sort, also called the descending incremental sort algorithm, is an efficient and improved version of the insertion sort.
Hill sort is a stable sort algorithm, with time complexity of O (N^3/2);
The hill algorithm is based on the two-point nature of the insertion sequence and proposes an improved method:
* The insertion sort can achieve the efficiency of linear sorting in the operation of the data which is almost already arranged;
* But insertion sort is generally inefficient because the insertion sort can only move data one bit at a time;

Algorithm Description:

1. Take a positive integer d1 (D1 < n), divide all records into D1 groups, and all records with a multiple distance of D1 as a group and then insert sort within each group
2. Then take D2 (D2 < D1)
3. Repeat the above grouping and sorting operations until the DI = 1 (i >= 1) position is taken, that is, all records become a group, and the group is then inserted to sort. General election D1 about N/2,D2 for D1/2, D3 for D2/2, ..., di = 1.

Animation 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, and the same color in the following figure represents a group:

2. Then insert the sorting of 4 groups, the results of which are:

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

4. Then insert the sorting of 2 groups, the results of which are:

5. Finally, take d3 = 1 and make the insertion order to get the final result:

Algorithm Demo:

*
* * Hill
sort
/void Shellsort (int a[], int n)
{
  int i, J, Gap;

  for (gap = N/2 gap>0; Gap/= 2)//step for
    (i = 0; I < gap; ++i) {for 
      (j = i+gap; j<n;j+=gap) {
        if (a[j) &L T 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; } 
Related Article

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.