插入排序
2016-01-08 16:31
253 查看
<pre name="code" class="cpp">/* 少量元素排序的有效方法 1、从第二个数据开始于前面的全部数据比较 2、将比较的数据插入到正确的位置 */ #include<iostream> using namespace std; void insert_sort(int a[], int length); int main() { int a[] = {2 ,5 ,8 ,3 ,9,10 ,4 ,6 ,1 ,7 }; int length = sizeof(a) / 4; insert_sort(a, length); for (int i = 0; i < length; i++) { cout << a[i] << " "; } cout << endl; system("pause"); return 0; } void insert_sort(int a[] , int length) { for (int i = 1; i < length; i++) { for (int j = 0; j < i; j++) { if (a[i] < a[j]) { //插入数据 //将数据放到正确位置,比该数据大的全部后移一位 int temp = a[i]; for (int m = i; m > j; m--) { a[m] = a[m - 1]; } a[j] = temp; break; } } } } /* 外循环是需要比较的数据,内循环从数组的0位开始循环比较 */ /*时间复杂度:最好O(n) 最坏O(n^2);空间复杂度O(n)*/
相关文章推荐
- 插入排序
- 我是运营,我没有假期
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 修复mysql数据库
- 浅析SQL数据操作语句
- SQLServer 数据导入导出的几种方法小结
- MySQL数据备份之mysqldump的使用详解
- C#实现窗体间传递数据实例
- 给你的数据库文件减肥
- Oracle数据更改后出错的解决方法
- Oracle数据库数据丢失恢复的几种方法总结
- C#将Sql数据保存到Excel文件中的方法
- MFC实现在文件尾追加数据的方法
- 把excel表格里的数据导入sql数据库的两种方法
- 用文本作数据处理
- 桌面中心(一)创建数据库
- 桌面中心(四)数据显示
- PHP+JS实现大规模数据提交的方法