直接插入排序
2015-11-04 15:23
176 查看
直接插入排序:把一个无序的表进行排列成有序顺序的表。每次从无序表中选取第一个元素,插入到有序表中,使得有序表仍然有序。
源码:
package insertsort;
public class InsertSort {
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int j = i;
while (j>=1 && array[j]<array[j - 1]) {
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
}
public static void main(String[] args) {
int[] testArray = {23, 25, 12, 42, 35};
insertSort(testArray);
System.out.println("The result is:");
for (Integer item : testArray) {
System.out.print(item);
System.out.print(' ');
}
}
}
源码:
package insertsort;
public class InsertSort {
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int j = i;
while (j>=1 && array[j]<array[j - 1]) {
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
j--;
}
}
}
public static void main(String[] args) {
int[] testArray = {23, 25, 12, 42, 35};
insertSort(testArray);
System.out.println("The result is:");
for (Integer item : testArray) {
System.out.print(item);
System.out.print(' ');
}
}
}
相关文章推荐
- (八)jms topic与jms queue区别
- C++ redirect input
- 优惠券发放接口调优实战
- 存储过程简单例子
- oracle 密码忘记 找回密码
- jquery绑定事件时如何传递参数
- LeetCode OJ:Permutations II(排列II)
- Lambda表达式详解
- C++ redirect input
- 解决ADB server didn't ACK问题,连上手机问题
- Python实现周期性抓取网页内容的方法
- (七)jms activeMQ与spring的集成
- NSFileManager
- Android开发:Fragment不同操作的生命周期
- 解析html
- Xcode 升级后插件失效问题
- html Frame、Iframe、Frameset 的区别
- Inno Setup打包
- Linux内核源代码情景分析笔记---对照4.2.5内核
- springmvc+mybatis+sitemesh3主要配置