Introduction to Algorithms (third edition) practice 2.1-1 ~ 2.1-4

Source: Internet
Author: User
Tags array length

2.1-1

(a) 31-"41"-59-26-41-58

(b) 31-41-"59"-26-41-58

(c) 31-41-59-"26"-41-58 (31, 41, 59 move right one position, insert 31 at the original 26 position)

(d) 26-31-41-59-"41"-58 (59 move right one position, insert 41 at the original 59 position)

(e) 26-31-41-41-59-"58" (59 move right one position, insert 58 at the original 59 position)

(f) 26-31-41-41-58-59

The number in "" "is temporarily stored in key.

2.1-2

insertion-SORT (A)    for j = 2 to a.length        = A[j]        = j-1 while           and A[i] < key            a[i+1] = A[i]            = i-1        a[i+1] = key

2.1-3

We Use the loop invariants to help us understand what an algorithm is correct.

Try to prove it , maybe it's wrong!

Initialize: proves that the loop is invariant before the first iteration. When the input size is 1 (array length), the array is composed of only a single element a[1], after an equal judgment, the output either takes i = 1 (this number is exactly equal to V), or take V = NIL (this number is not v), the loop invariant form.

Hold: proves that each iteration keeps the loop invariant. Assuming that the input scale is n, the loop is invariant, that is, either the subscript I (a[i] = = v) is taken in the array n, or it is not found (v = NIL). Then add an element, make the scale of n+1, easy to know, the size of n since the establishment (can get a useful nature, conclusion), scale n+1 at most one time equal judgment (that is, in the original array does not take the case of I), and finally can obtain useful conclusions. The iteration (in this case, size + 1) will keep the loop invariant.

Termination: Finally, the study of what happened at the end of the cycle. The condition that causes the iteration to terminate is I > a.length=n or take subscript I, if the former so that V = NIL, this time has been a useful conclusion, so the algorithm is correct.

2.1-4

Input: An array of length n, a, B, storing an n-bit binary integer, respectively. (the highest bit must be 1, the other bit is 0 or 1)

Output: An array of length n+1 C that stores the sum of the binary integers stored with a and B.

Pseudo code:

ADD (A, B, C)      for 1 To a.length         = A[k] + b[k]        if2            c[k+1] = c[k+11              2

The correctness of this algorithm can also be understood by cyclic invariant.

Introduction to Algorithms (third edition) practice 2.1-1 ~ 2.1-4

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.