Kids Learn data structure (8): Direct Insert Sort

Source: Internet
Author: User

Kids Learn data structure (8): Direct Insert Sort

(a) Basic ideas

In the set of numbers to be sorted, assuming that the number of front (n-1) [n>=2] is already in the order, now the nth number is inserted into the ordinal number in front, so that the n number is also in order. This cycle is repeated until all the rows are in order.

1-1.jpg (b) C language code implementation
#Include<stdio.h>voidInsertsort(int a[],int n) {int I, j, temp;for (i =1; I < n; i++) {temp = A[i]; j = i;Move the value greater than temp to one unit after the wholewhile (J >0 && A[j-1] > Temp) {a[j] = a[j-1]; j--;} a[j]=temp;}} int main() { int arr[] = {   52}; int len = sizeof (arr)/ sizeof (int); Insertsort (arr, Len); int i = 0; for (; i < Len; i++) { printf ("%d", Arr[i]);} return 0;}                 

Operation Result:

52, 57, 59, 68

Program Analysis:
For Loop,
(1) i = 1, temp = a[1] = a[0, j = 1, a[0] =,] > temp not set, no adjustment required

(2) i = 2,temp = a[2] = 59,
①j = 2,a[1] = > temp, execution loop a[2] = a[1] = 68,j auto minus.
②j = 1, a[0] = > Temp not set, Loop end.
③ last execution a[1] = temp = 59, at which time arr = {57,59,68,52}

(3) i = 3,temp = a[3] = 52
①j = 3, a[2] = > temp, execution loop a[3] = a[2] = 68,j Auto minus
②j = 2,a[1] = > temp, execution loop a[2] = a[1] = 59,j Auto minus
③j = 1,a[0] = > temo, execution loop a[1] = a[0] = 57,j self-reduced to 0, loop over
④ last execution a[0] = temp = 52, at this time a= {52, 57, 59, 68}

(iii) The program in the big talk data structure
#Include<iostream>UsingNamespaceStd#Define MAX 5typedefstruct{int R[max +1];int Len;} SeqvoidMyswap(Seq *x,int I,Int J) {int temp = x->r[i]; X->r[i] = x->r[j]; X-&GT;R[J] = temp;}voidInsertsort(Seq *x) {Int J =0;for (int i =2; I <= x->len; i++) {if (X->r[i] < x->r[i-1]) {x->r[0] = x->r[i];X->r[0] As a sentinel, that is, a temporary variablefor (Int J = i-1; X-&GT;R[J] > x->r[0]; j--) {x->r[j +1] = x->r[j]; } X->r[j +1] = x->r[0]; } }}int main() { cout << "input data:"; seq s; For (int i = 1; I <= MAX; i++) { cin >> s.r[i];} s.len = MAX; Insertsort (&s); cout << "sorted results:"; For (int i = 1; I <= S.len; i++) { cout << s.r[i] << ";} cout << Endl; return 0;}                  

Operation Result:

输入数据:5 4 3 2 1排序结果:1 2 3 4 5


Kids Learn data structure (8): Direct Insert Sort

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.