Introduction to Algorithms Learning Notes: 2.1-inserting sort

Source: Internet
Author: User

Write this code without hesitation:

 /*  n: Array length   for  (i = 1 ; i < N; I++ int  t = I;  while  (T > 0 ? a[t]<a[t-< Span style= "color: #800080;" >1 ]:) { int  k = a[t];        A[t]  = a[t-1  ];        A[t -1 ] = K;    T  = t-1  ; }}

The elements in the original a[i] have been found, if the order is correct, as compared to the previous one. A[0..I-1] elements are ordered, which is called "cyclic invariant" in the book.

Also: can expressions in a while loop be written as "T > 0 && a[t] < a[t-1]"? Is it likely that an access is out of bounds? Please also give directions.

The pseudo-code given in the book is written as follows:

 for 1; i < N; i++) {    int1;      while 0 && a[j] > t) {        1] = a[j];        J--;    }     1] = t;}

There is no obvious exchange, in fact, and "Find out a[i" should be in the position a[k], will a[k+1..i-1] elements of the back one "is the same principle, but the book is clearly a lot of concise.

Introduction to Algorithms Learning Notes: 2.1-inserting 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.