【算法导论】2-1 插入排序insertion-sort.cpp
2015-05-02 17:18
253 查看
#include <iostream> using namespace std; //插入排序算法,书p10 正确性证明见p11 时间复杂度o(n平方) int main1() { int a[10]={5,6,2,1,3,7,8,10,9,4}; int time=0; int j=0,i=0; int key=0; for (j=1;j<10;j++) { key=a[j]; i=j-1; while(i>=0 &&a[i]>key) { time++; a[i+1]=a[i]; i=i-1; //实际上是完成交换 } a[i+1]=key; for (i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; } for (i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; cout<<time<<endl; } //测试输出如下: //5 6 2 1 3 7 8 10 9 4 //2 5 6 1 3 7 8 10 9 4 //1 2 5 6 3 7 8 10 9 4 //1 2 3 5 6 7 8 10 9 4 //1 2 3 5 6 7 8 10 9 4 //1 2 3 5 6 7 8 10 9 4 //1 2 3 5 6 7 8 10 9 4 //1 2 3 5 6 7 8 9 10 4 //1 2 3 4 5 6 7 8 9 10 //1 2 3 4 5 6 7 8 9 10
相关文章推荐
- 算法系列-------直接插入排序(InsertionSort)
- 插入排序(insertion sort)算法实现
- [Leetcode] insertion sort list 链表插入排序
- LeetCode Insertion Sort List 链表插入排序
- [LeetCode] Insertion Sort List 单向链表插入排序
- 【DS】排序算法之插入排序(Insertion Sort)
- 算法笔记:(一)插入排序:直接插入排序(Straight Insertion Sort)
- 直接插入排序(Insertion Sort)
- 插入排序(InsertionSort)
- 插入排序-Insertion sort
- 插入排序(InsertionSort)
- 【lintcode】——链表插入排序-Insertion Sort List
- 插入排序 Insertion Sort
- 直接插入排序(Insertion Sort)
- 算法学习记录-排序——插入排序(Insertion Sort)
- 「算法导论」:插入排序
- 插入排序的递归实现和二分查找递归实现,算法导论2.3-4和2.3-5
- 插入排序【算法导论】
- 算法导论:插入排序
- 算法不会,尚能饭否之排序——直接插入排序(Insert sort)