算法导论(第三版)参考答案:练习2.1-1,练习2.1-2,练习2.1-3,练习2.1-4
Exercise 2.1-1
Using figure 2.4 as a model, illustrate the operations of Insertion-Sort on the array A=⟨31,41,59,26,41,58⟩ .
插入排序思想:在遍历第二到最后元素过程中,给这个元素挪出正确的位置。
Exercise 2.1-2
Rewrite the Insertion-Sort procedure to sort into nonincreasing instead of nondecreasing order.
非升序排列:
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
Exercise 2.1-3
Consider the searching problem:
Input: A sequence of n numbers A=⟨a1,a2,…,an⟩ and a value ν .
Output: And index
i such that ν=A[i] or the special value NIL if ν