直接插入排序
2016-04-24 11:11
246 查看
package sun; public class Sort { /** * * @param a 待排序数组 */ public static void insertSort(int[] a){ int i , j; int temp; //第一个元素有序,从第二个开始比较 for(i = 1;i <= a.length-1;i++){ temp = a[i]; j = i-1; //待排序元素比前面的小,则前面的数组向后移动,j指针继续向前 while(j >= 0 && temp < a[j]){ a[j+1] = a[j]; --j; } //j指针确定最后待排元素的位置 a[j+1] = temp; } } public static void main(String[] args) { int[] arr = {49,38,65,97,76,13,27,49}; insertSort(arr); for(int a : arr) System.out.println(a); } }
相关文章推荐
- Error:Execution failed for task ':app:mergeDebugResources'. > Crunching Cruncher bg_btn.9.png
- motto11
- 数据挖掘数据集下载资源
- Javascript 面向对象编程(一):封装
- 继续Node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ
- 栈的运用-四则运算表达式求值
- java基础(九),File
- 常用纽扣电池的型号对照表
- word技巧
- 压缩与解压缩的原理
- [leetcode] House Robber III
- 一个页面弄懂 CSS 样式选择器
- fragment.setMenuVisibility setUserVisibleHint
- linux centos5.7(32bit) oracle 10g oracle11g
- MySQL查询优化
- C++对象模型
- API 升级
- PAT 1074 Reversing Linked List
- Elasticsearch查询过程
- Android UiAutomator UiObject API