插入排序
2015-07-30 16:24
246 查看
public class ArrayUtil {
/**
*
* @param array
*/
public static void printArray(int [] array){
System.out.print("{");
for(int i = 0;i < array.length;i++){
System.out.print(array[i]);
if(i < array.length - 1){
System.out.print(",");
}
}
System.out.println("}");
}
}
public class InsertSort {
public static void insertSort(int[] array) {
if (array == null || array.length < 2) {
return;
}
for (int i = 0; i < array.length; i++) {
int currentValue = array[i];
int position = i;
for (int j = i - 1; j >= 0; j--) {
if (array[j] > currentValue) {
array[j + 1] = array[j];
position = position - 1;
} else {
break;
}
}
array[position] = currentValue;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
ArrayUtil.printArray(a);
insertSort(a);
ArrayUtil.printArray(a);
}
}
/**
*
* @param array
*/
public static void printArray(int [] array){
System.out.print("{");
for(int i = 0;i < array.length;i++){
System.out.print(array[i]);
if(i < array.length - 1){
System.out.print(",");
}
}
System.out.println("}");
}
}
public class InsertSort {
public static void insertSort(int[] array) {
if (array == null || array.length < 2) {
return;
}
for (int i = 0; i < array.length; i++) {
int currentValue = array[i];
int position = i;
for (int j = i - 1; j >= 0; j--) {
if (array[j] > currentValue) {
array[j + 1] = array[j];
position = position - 1;
} else {
break;
}
}
array[position] = currentValue;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
ArrayUtil.printArray(a);
insertSort(a);
ArrayUtil.printArray(a);
}
}
相关文章推荐
- 使用window live writer
- [Jquery]某宝图片轮播(无缝、带左右切换按钮)
- http://xiejianglei163.blog.163.com/blog/static/1247276201441910377144/
- epoll实现分析
- 中文linux安装oracle界面乱码解决方案
- 定时任务会重复执行俩次的解决方法
- 定时任务会重复执行俩次的解决方法
- 定时任务会重复执行俩次的解决方法
- 源码推荐(6.16):高仿新浪微博,支付宝(高仿)源码
- HDU 1272 小希的迷宫 并查集
- 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示。
- 我要娶你做我的CoreData!
- github 中文教程
- 相邻div实现一个跟着另一个自适应高度示例代码
- 资源模块句柄的切换 .
- 判断DataGridView滚动条是否滚动到当前已加载的数据行底部
- 委托、 Lambda表达式和事件——委托
- ARC机制
- 定时任务会重复执行俩次的解决方法
- FLex学习文档