mirror of
https://github.com/wahyd4/wahyd4.github.com.git
synced 2026-08-09 21:36:22 +10:00
- 308 posts reverse-extracted from wahyd4.github.com (Hexo 6.3.0 + NexT 8.25) - Hexo project with NexT theme, reading-experience custom styles - publish-post.sh: write post -> hexo build -> PR (master untouched)
1.3 KiB
1.3 KiB
title, date, tags
| title | date | tags | |||
|---|---|---|---|---|---|
| Java 实现直接插入排序 | 2012-09-11 00:00:00 |
|
项目代码已经发布到github.欢迎各位下载:https://github.com/wahyd4/java-sorting
快到校招了,我还是把以前的数据结构的书拿出来复习了,不过这次我将用java 来实现数据结构中的一些算法。当然还是希望能够顺利通过这些公司的笔试。下面分享的是直接插入排序。
public class InsertionSort {
// 定义需要排序的数组
private static int[] array = { 1, 20, 6, 3, 19, 7, 14, 12, 10 };
public static void main(String args[]) {
for (int outer = 1; outer = 0 && temp < array[inner]; inner--) {
/**
* 将最大的赋值给目前比较的数组尾部 由于这里的数组下标需要
* 跟随循环而变化,所以只能使用 j来表示
*/
array[inner + 1] = array[inner];
// 重新赋值第二大的数
array[inner] = temp;
// 重新赋值参考值,接着下标-1
temp = array[inner];
}
}
// 便利排序后的数组
for (int flag : array) {
System.out.println(flag);
}
}
}
算法的时间复杂度:
直接排序最糟糕时需要比较的次数为(若数组长度为n):1+2+…+n-1=n(n-1)/2,复杂度为O(n2),一般情况下,复杂度也是如此。