Skip to content

2.1 Insertion sort

2.1-1

Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT\text{I\scriptsize NSERTION-\normalsize S\scriptsize ORT} on an array initially containing the sequence 31,41,59,26,41,58\langle 31, 41, 59, 26, 41, 58 \rangle.

A=31,41,59,26,41,58A=31,41,59,26,41,58A=31,41,59,26,41,58A=26,31,41,59,41,58A=26,31,41,41,59,58A=26,31,41,41,58,59\begin{aligned} A &= \langle 31,41,59,26,41,58 \rangle \\ A &= \langle 31,41,59,26,41,58 \rangle \\ A &= \langle 31,41,59,26,41,58 \rangle \\ A &= \langle 26,31,41,59,41,58 \rangle \\ A &= \langle 26,31,41,41,59,58 \rangle \\ A &= \langle 26,31,41,41,58,59 \rangle \\ \end{aligned} A hand-written note simulating the process of Insertion-Sort

2.1-2

Consider the procedure SUM-ARRAY\text{S\scriptsize UM-\normalsize A\scriptsize RRAY} on the facing page. It computes the sum of the nn numbers in array A[1:n]A[1:n]. State a loop invariant for this procedure, and use its initialization, maintenance, and termination properties to show that the SUM-ARRAY\text{S\scriptsize UM-\normalsize A\scriptsize RRAY} procedure returns the sum of the numbers in A[1:n]A[1:n].

Let’s state the loop invariant as follows: At the start of each iteration of the for\textbf{for} loop of lines 2-4, the sumsum holds the total value of subarray A[1:i1]A[1:i-1].

  • Initialization: Before the first iteration, i=1i=1, and A[1:i1]A[1:i-1] gives us an empty array whose sum adds up to 00.
  • Maintenance: In each iteration, A[i]A[i] joins the subarray A[1:i1]A[1:i-1]. Therefore, the total value also increases by the value of A[i]A[i]. Given that the loop invariant is satisfied before the start of each iteration, adding sumsum with the value of A[i]A[i] ensures the loop invariant is true after each iteration.
  • Termination: The loop variable ii starts at 11 and increases by 11 in each iteration, until exceeding nn. Therefore, the loop will stop at i=n+1i=n+1. At this point, sumsum holds the total value of A[1:i1]=A[1:n]A[1:i-1]=A[1:n]. Hence, the algorithm is correct.

2.1-3

Rewrite the INSERTION-SORT\text{I\scriptsize NSERTION-\normalsize S\scriptsize ORT} procedure to sort into monotonically decreasing instead of monotonically increasing order.

Replace A[j]>keyA[j] > key in line 5 by A[j]<keyA[j] < key will do.

2.1-4

Consider the searching problem:

Input: A sequence of nn numbers a1,a2,,an\langle a_1, a_2, \ldots, a_n \rangle stored in array A[1:n]A[1:n] and a value xx.

Output: An index ii such that xx equals A[i]A[i] or the special value NIL\text{\scriptsize NIL} if xx does not appear in AA.

Write pseudocode for linear search, which scans through the array from beginning to end, looking for xx. 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}
Pseudocode of Linear-Search

We state the loop invariant as follows: At the start of each iteration of the for\textbf{for} loop of lines 1-3, xx does not appear in subarray A[1:i1]A[1:i-1].

  • Initialization: Before the first iteration, i=1i=1, and A[1:i1]A[1:i-1] 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 xx does not equal A[i]A[i], A[i]A[i] will join subarray A[i:i1]A[i:i-1], and thereby the loop invariant is maintained. Second, if xx equals A[i]A[i], ii will be returned as the result, and the loop will stop.
  • Termination: The loop variable ii starts at 11 and increases by 11 in each iteration, until exceeding nn or xx is found. If the loop ends because that xx is found, the output will be correct, as discussed previously. Otherwise, i=n+1i=n+1, by the loop invariant, xx does not appear in array A[1:i1]=A[i:n]A[1:i-1]=A[i:n], therefore NIL\text{\scriptsize NIL} will be the output. In conclusion, the algorithm is correct.

2.1-5

Consider the problem of adding two nn-bit binary integers aa and bb, stored in two nn-element arrays A[0:n1]A[0:n-1] and B[0:n1]B[0:n-1], where each element is either 00 or 11, a=i=0n1A[i]2ia=\sum_{i=0}^{n-1} A[i]\cdot 2^i, and b=i=0n1B[i]2ib=\sum_{i=0}^{n-1} B[i]\cdot 2^i. The sum c=a+bc=a+b of the two integers should be stored in binary form in an (n+1)(n+1)-element array C[0:n]C[0:n], where c=i=0nC[i]2ic=\sum_{i=0}^{n} C[i]\cdot 2^i. Write a procedure ADD-BINARY-INTEGERS\text{A\scriptsize DD-\normalsize B\scriptsize INARY-\normalsize I\scriptsize NTEGERS} that takes as input arrays AA and BB , along with the length nn, and returns array CC 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}
Pseudocode of Add-Binary-Integers