2.1 Insertion sort
2.1-1
Using Figure 2.2 as a model, illustrate the operation of on the array .
The operation of on the array . Array indices appear above the rectangles, and values stored in the array positions appear within the rectangles.
(a)-(e) are iterations of the for loop of lines 1-8.
In each iteration, the black rectangle holds the key taken from , which is compared with the values in shaded rectangles to its left in the test of line 5. Dotted arrows show array values moved one position to the right in line 6. and solid arrows indicate where the key moves to in line 8.
(f) is the final sorted array.
The changes of array A during traversal:
2.1-2
Rewrite the procedure to sort into nonincreasing instead of nondecreasing order.
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
2.1-3
Consider the searching problem:
Input: A sequence of numbers and a value .
Output: An index such that or the special value if does not appear in .
Write pseudocode for linear search, which scans through the sequence, looking for . Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.
LINEAR-SEARCH(A, v)
for i = 1 to A.length
if A[i] == v
return i
return NIL
Loop invariant: At the start of each iteration of the for loop, the subarray consists of elements that are different than .
Initialization: Before the first loop iteration (), the subarray is the empty array, so the proof is trivial.
Maintenance: During each loop iteration, we compare with . If they are the same, we return , which is the correct result. Otherwise, we continue to the next iteration of the loop. At the end of each loop iteration, we know the subarray does not contain , so the loop invariant holds true. Incrementing for the next iteration of the for loop then preserves the loop invariant.
Termination: The loop terminates when . Since increases by , we must have at that time. Substituting , for in the wording of the loop invariant, we have that the subarray consists of elements that are different than . Thus, we return . Observing that , we conclude that the entire array does not have any element equal to . Hence the algorithm is correct.
2.1-4
Consider the problem of adding two -bit binary integers, stored in two -element arrays and . The sum of the two integers should be stored in binary form in an -element array . State the problem formally and write pseudocode for adding the two integers.
Input: An array of booleans and an array of booleans , each representing an integer stored in binary format (each digit is a number, either or , least-significant digit first) and each of length .
Output: An array such that where , and are the integers, represented by , and .
ADD-BINARY(A, B)
C = new integer[A.length + 1]
carry = 0
for i = 1 to A.length
C[i] = (A[i] + B[i] + carry) % 2 // remainder
carry = (A[i] + B[i] + carry) / 2 // quotient
C[i + 1] = carry
return C