Refer the intrdouction of algorithm (算法导论)
Question:
Rewrite the INSERTION-SORT
procedure to sort into non-increasing instead of non-decreasing order(降序排列).
Answer:
We just need to reverse the comparison of Aj and Key(Key represent the insert value)
// use the pseudocode(伪代码进行解释说明)
for j = 2 to A.length
key = A[j]
// Insert A[j] into the sorted sequence A[1..j − 1]
i = j − 1
while i > 0 and A[i] < key
A[i + 1] = A[i]
i = i − 1
A[i + 1] = key
this is use for me to learn the introduction of algorithm and exercise