插入排序
来源:互联网 发布:nes30 pro软件 编辑:程序博客网 时间:2024/06/06 00:20
思想:从第二个数开始,插到前面的有序数列中的合适位置(第一个数视为一个有序数列)。
即如: 1 3 4 6 2 5 -1
遍历到2时发现 2 < 3,故将2插入3前,即3到6向后移动一位,2放到3三前面
同理5插到6前,-1插到1前
package sort;public class Sort { public static void main(String[] args) { int[] a = {1,1,5,4,3,3,-1}; insertSort(a); for (int i : a) { System.out.print(i+" "); } } //插入排序 public static void insertSort(int[] arr) { int size = arr.length; int temp = 0 ; int j = 0; for(int i = 0 ; i < size ; i++){ temp = arr[i]; //假如temp比前面的值小,则将前面的值后移 for(j = i ; j > 0 && temp < arr[j-1] ; j --){ arr[j] = arr[j-1]; } arr[j] = temp; } } }
阅读全文
0 0
- 插入排序-【插入排序】
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- Krpano学习:Type of panorama image? Spherical/Cylindrical
- Android应用流量测试
- (转载)基于Mysql的Sequence实现
- JSONP跨域请求数据报错 “Unexpected token :”的解决办法
- 40亿个整数,求上中位数
- 插入排序
- 聚能聊 | 人工智能技术百问——机器真的能取代人类吗
- 复制构造函数在什么情况下被调用
- 算法基础:排序(一)——选择排序、插入排序、Shell排序——Python实现
- PHP错误处理:die()Vs trigger_error()Vs throw异常
- Zxing 生成二维码嵌套logo,并输出到浏览器
- sql 获取24小时列表
- 电商订单技术方案梳理-续
- android与js 的简单交互