书中伪代码:
for j <- 2 to length[A]
do key <- A[j]
i <- j - 1
while i > 0 and A[i] > key
do A[i+1] <- A[i]
i <- i -1
A[i+1] <- key
我的C++代码实现:
void Insertion_Sort(int *List,int length){
for(int j = 1;j < length;j++){
int key = List[j];
int i = j - 1;
while(i >= 0 && List[i] > key){
List[i+1] = List[i];
i--;
}
List[i+1] = key;
}
}