插入排序--直接插入
来源:互联网 发布:淘宝用是谁写的 编辑:程序博客网 时间:2024/06/02 21:49
直接插入:就是把未排序的数据插入到已排序的数列中去
步骤:从排序数组的第二个数开始,依次与前面的数比较,比前面的数大的话就插入到前面去,依次循环完。
public static void sort(int a[]){ //key用来保存待插入的值,index用来指示已排序的数组下标 int key,index; for(int i = 1 ; i < a.length; i++){ key = a[i]; for(index = i -1; index >= 0 &&a[index] < key; index--){ a[index+1] = a[index]; } a[index+1] = key; } }
完整代码:
package sort;/** * 直接插入排序 * * @author lin * */public class Select { static int a[] = {5,3,6,7,3,2,9,6,3,6}; public static void main(String[] args) { sort(a); print(a); } public static void sort(int a[]){ //key用来保存待插入的值,index用来指示已排序的数组下标 int key,index; for(int i = 1 ; i < a.length; i++){ key = a[i]; for(index = i -1; index >= 0 &&a[index] < key; index--){ a[index+1] = a[index]; } a[index+1] = key; } } public static void print(int a[]){ for(int i = 0; i < a.length; i ++) System.out.format("%d ", a[i]); }}
0 0
- 插入排序--直接插入
- 插入排序------直接插入
- 插入排序---直接插入
- 插入排序--直接插入排序
- 插入排序-直接插入排序
- 【插入排序】直接插入排序
- 插入排序-直接插入排序
- 插入排序:直接插入排序
- 插入排序-直接插入排序
- 插入排序--直接插入排序
- 插入排序-直接插入排序
- 插入排序-直接插入排序
- 排序:直接插入排序
- 排序---直接插入排序
- 排序 -- 直接插入排序
- 排序---直接插入排序
- 排序--直接插入排序
- 排序-直接插入排序
- JAVA 编码机制
- 静态加载问题
- 如何创建和使用无模式对话框
- 中间缓存变量机制
- PHP
- 插入排序--直接插入
- 交换机三种端口模式
- ORACLE日期,字符函数大全
- 折半查找
- Java初始化
- YII中CDetailView下使用CHtml image()显示图片的正确用法
- 使用Jmeter对mysql进行性能测试入门
- 第四周——数组作数据成员
- 飞机去哪了,李克强:中国调动21颗卫星搜寻失联客机