2.1 Insertion sort
2.1-1
Using Figure 2.2 as a model, illustrate the operation of on an array initially containing the sequence .

2.1-2
Consider the procedure on the facing page. It computes the sum of the numbers in array . State a loop invariant for this procedure, and use its initialization, maintenance, and termination properties to show that the procedure returns the sum of the numbers in .
Let’s state the loop invariant as follows: At the start of each iteration of the loop of lines 2-4, the holds the total value of subarray .
- Initialization: Before the first iteration, , and gives us an empty array whose sum adds up to .
- Maintenance: In each iteration, joins the subarray . Therefore, the total value also increases by the value of . Given that the loop invariant is satisfied before the start of each iteration, adding with the value of ensures the loop invariant is true after each iteration.
- Termination: The loop variable starts at and increases by in each iteration, until exceeding . Therefore, the loop will stop at . At this point, holds the total value of . Hence, the algorithm is correct.
2.1-3
Rewrite the procedure to sort into monotonically decreasing instead of monotonically increasing order.
Replace in line 5 by will do.
2.1-4
Consider the searching problem:
Input: A sequence of numbers stored in array and a value .
Output: An index such that equals or the special value if does not appear in .
Write pseudocode for linear search, which scans through the array from beginning to end, looking for . Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.
\begin{codebox} \Procname{$\proc{Linear-Search}(A, n, x)$} \li \For $i \gets 1$ \To $n$ \Do \li \If $A[i] \isequal x$ \Do \li \Return $i$ \End \End \li \Return \nil\end{codebox}

We state the loop invariant as follows: At the start of each iteration of the loop of lines 1-3, does not appear in subarray .
- Initialization: Before the first iteration, , and gives us an empty array. An value of course cannot be in an empty array.
- Maintenance: In each iteration, there are two possible situations. First, if does not equal , will join subarray , and thereby the loop invariant is maintained. Second, if equals , will be returned as the result, and the loop will stop.
- Termination: The loop variable starts at and increases by in each iteration, until exceeding or is found. If the loop ends because that is found, the output will be correct, as discussed previously. Otherwise, , by the loop invariant, does not appear in array , therefore will be the output. In conclusion, the algorithm is correct.
2.1-5
Consider the problem of adding two -bit binary integers and , stored in two -element arrays and , where each element is either or , , and . The sum of the two integers should be stored in binary form in an -element array , where . Write a procedure that takes as input arrays and , along with the length , and returns array holding the sum.
\begin{codebox} \Procname{$\proc{Add-Binary-Integers}(A, B, n)$} \li $C \gets \varnothing, carry \gets 0$ \li \For $i \gets 0$ \To $n-1$ \Do \li $C[i] \gets A[i] \oplus B[i] \oplus carry$ \li \If $A[i] + B[i] + carry \ge 2$ \Do \li $carry \gets 1$ \li \Else \li $carry \gets 0$ \End \End \li $C[n] \gets carry$ \li \Return $C$\end{codebox}
