Cod*_*ack 0 algorithm pseudocode insertion-sort
我正在阅读算法入门书和伪代码
INSERTION-SORT(A)
1 for j ? 2 to length[A]
2 do key ? A[j]
3 ? Insert A[j] into the sorted sequence A[1 j - 1].
4 i ? j - 1
5 while i > 0 and A[i] > key
6 do A[i + 1] ? A[i]
7 i ? i - 1
8 A[i + 1] ? key
Run Code Online (Sandbox Code Playgroud)
虽然维基上的伪代码是
for j ?1 to length(A)-1
key ? A[ j ]
> A[ j ] is added in the sorted sequence A[0, .. j-1]
i ? j - 1
while i >= 0 and A [ i ] > key
A[ i +1 ] ? A[ i ]
i ? i -1
A [i +1] ? key
Run Code Online (Sandbox Code Playgroud)
为什么一个以2开始并循环到长度而另一个以1开始并循环直到A -1的长度?