INSERTION-SORT(A)的C++代码

来源:互联网 发布:苗阜相声水平知乎 编辑:程序博客网 时间:2024/06/07 00:56

书中伪代码:

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;    }}


原创粉丝点击