INSERTION-SORT(A)的C++代码
2013-11-29 22:03
393 查看
书中伪代码:
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;
}
}
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;
}
}
相关文章推荐
- 数据结构 - 折半插入排序(Binary Insertion Sort) 详解 及 代码(C++)
- C++ insertion sort(插入排序)
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- 数据结构 - 直接插入排序(Straight Insertion Sort) 详解 及 代码(C++)
- LeetCode 之 Insertion Sort List — C++ 实现
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- leetcode_c++:链表:Insertion Sort List(147)
- 数据结构 - 简单选择排序(simple selection sort) 详解 及 代码(C++)
- [C++] 插入排序法 (Insertion sort)
- 排序算法之插入排序<Insertion_Sort>及其C语言代码实现
- 数据结构 - 堆排序(heap sort) 详解 及 代码(C++)
- PAT程序设计考题——甲级1098(Insertion or Heap Sort ) C++实现
- 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- awk、sort等命令搞定几十行代码C++做的事------用好工具, 事半功倍
- C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- C/C++实现如下 根据Merge Sort原理, 自己实现的归并排序算法+详细注释+代码(C#,C/C++)
- 数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)
- 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)