Three implementations of two direct insertion sequences for vernacular classical algorithm series

Source: Internet
Author: User

The basic idea of a direct insert sort (insertion sort) is to insert a record to be sorted each time, by its keyword size, into the appropriate position in the previously ordered subsequence until all the records have been inserted.

Set the array to a[0...n-1].

1. Initially, a[0] self into 1 ordered areas, unordered area is a[1..n-1]. Make I=1

2. Merge A[i] into an ordered interval of A[0...I] formed in the current ordered region A[0...i-1].

3. i++ and repeat the second step until the i==n-1. Sorting is complete.

The following code is written in strict accordance with the definition (from small to large sort):

void Insertsort1 (int a[], int n) {int I, J, k;for (i = 1; i < n; i++) {//= A[i] Find an appropriate position for (j = a[0...i-1) in the ordered interval of the preceding i-1] J >= 0;  j--) if (A[j] < a[i]) break;//If a suitable position is found if (j! = i-1) {//will be greater than a[i] data is moved backwards int temp = a[i];for (k = i-1; k > J; k--) a[k + 1] = a[k];//put a[i] in the correct position a[k + 1] = temp;}}}

This code is too long, not clear enough. Now make a rewrite, merging the two steps of search and data back. That is, each a[i] first and previous data a[i-1], assuming a[i] > a[i-1] Description a[0...i] is also ordered, without adjustment. otherwise j=i-1,temp=a[i]. Then move the data a[j] backward and forward, and when there is data a[j]<a[i] stop and drop the temp at A[j + 1].

void Insertsort2 (int a[], int n) {int I, j;for (i = 1; i < n; i++) if (A[i] < a[i-1]) {int temp = a[i];for (j = i-1 ; J >= 0 && a[j] > temp; j--) A[j + 1] = a[j];a[j + 1] = temp;}}


Then rewrite the method used to insert a[j] into the ordered interval of the previous a[0...j-1] and replace the data with data exchange. Suppose A[j] [previous data a[j-1] > A[j], swap a[j] and a[j-1], then j--until A[j-1] <= a[j]. This also enables the new data to be incorporated into an orderly interval.

void Insertsort3 (int a[], int n) {int I, j;for (i = 1; i < n; i++) for (j = i-1; J >= 0 && A[j] > a[j + 1 ]; j--) Swap (A[j], a[j + 1]);}


Three implementations of two direct insertion sequences for vernacular classical algorithm series

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.