排序算法之插入排序
来源:互联网 发布:java float与double 编辑:程序博客网 时间:2024/06/12 00:09
如何把一个数字插入到一段有序数字中,就需要用插入排序,如:3 5 7 4,如何把4移动到3 的后边,先找到位置,后移动。只要 数字比 4 大(a[i]>temp),就往后移动,(a[i]=a[i-1])。退出循环的条件是:i = 0或者 a[i]<temp。因此。把第 n 个数字插入有序数列中的算法如下:
void insert(int a[],int n){ int temp = a[n]; while(n>0 && temp < a[n-1]) { a[n] = a[n-1]; n--; } a[n] = temp;}
选择排序算法如下:
#include<stdio.h>void insert(int a[],int n){ int temp = a[n]; while(n>0 && temp < a[n-1]) { a[n] = a[n-1]; n--; } a[n] = temp;}void insertSort(int a[],int n){ int i; for(i=1;i<n;i++) insert(a,i);}int main(){ int i; int a[10]={23,19,45,22,13,10,12,4,56,1}; insertSort(a,10); for(i =0;i<11;i++) printf("%d ",a[i]); return 0;}
阅读全文
0 0
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之 插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- 排序算法之插入排序
- tensorflow-使用flags定义命令行参数
- “戏”说Spark---Spark初认识
- MyBatis学习总结(一)——MyBatis快速入门
- Navicat Premium 连接Oracle 数据库(图文教程)
- 【Scikit-Learn 中文文档】十八:神经网络模块(有监督)- 监督学习
- 排序算法之插入排序
- WEB前端 -- 浮动
- 获取指定进程被启动的次数
- 4用于cifar10的卷积神经网络-4.19tanh和softsign激活函数的输出汇总分析
- Zookeeper安装
- HDU1540--线段树(最长连续区间)
- 11.27学习心得
- 表单提交前验证实现方法
- 你也许要付出5000亿刀的学费,才能明白架构的重要性