- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
I am looking at the algorithm of the insertion sort:
There is the following sentence:
At the beginning of each iteration "for" ,the subsequence $ A[1 \dots j-1]$ consists of the elements that are from the beginning at these positions, but sorted with the desired way.
I found the following proof in my textbook:
But...could you tell me if there is also an other way to prove the sentence? (Thinking)
I am looking at the algorithm of the insertion sort:
Code:
Input: A[1 ... n] for j <- 2 to n
key <- A[j]
i <- j-1
while (i>0 and A[i]>key)
A[i+1] <- A[i]
i <- i-1
end
A[i+1] <- key
end
There is the following sentence:
At the beginning of each iteration "for" ,the subsequence $ A[1 \dots j-1]$ consists of the elements that are from the beginning at these positions, but sorted with the desired way.
I found the following proof in my textbook:
- Initial control, when $j=2:$
The sentence is true,because the subsequence $A[1]$ is sorted in a trivial way.
- Preservation control:
The algorithm moves the elements $ A[j-1], A[j-2], \dots $ to the right side,till the right position for $ A[j] $ is found.
- Verification of the result:
When the loop "for" ends, $j$ has the value $n+1$. The subsequence is now $ A[1 \dots n]$, which is now sorted.
But...could you tell me if there is also an other way to prove the sentence? (Thinking)