java算法之直接插入排序
2017-08-05 21:54
183 查看
基本思想:
将一个记录插入到已排序好的有序数组中,从而得到一个新的有序数组,记录数增1的有序数组。即:先将数组的第1个记录看成是一个有序的子数组,然后从第2个记录逐个进行插入,直至整个数组有序为止。
代码
时间复杂度
O(n^2)—最坏
O(n^2)—平均
O(n) ——最好
将一个记录插入到已排序好的有序数组中,从而得到一个新的有序数组,记录数增1的有序数组。即:先将数组的第1个记录看成是一个有序的子数组,然后从第2个记录逐个进行插入,直至整个数组有序为止。
代码
public class InsertSort { public static void insertSort(int[] array) { int temp; for (int i = 1; i < array.length; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } } } } public static void main(String[] args) { int[] a = new int[] { 49, 38, 65, 97, 76, 13, 27, 50 }; insertSort(a); for (int i : a) System.out.print(i + " "); } }
时间复杂度
O(n^2)—最坏
O(n^2)—平均
O(n) ——最好
相关文章推荐
- Java算法 直接插入排序
- Java算法实现-直接插入排序
- java算法【直接插入排序】
- JAVA算法(直接插入排序)
- 插入排序----直接插入排序算法
- 直接插入排序
- 直接插入排序
- 数据结构排序之直接插入排序
- java之直接插入排序
- 第十六周项目1-验证算法(1)直接插入排序
- 直接插入排序中的监视哨问题
- 直接插入排序
- 直接插入排序 Java
- 南京邮电大学OJ——1062直接插入排序
- 数据结构之排序算法一冒泡排序,直接插入排序,简单选择排序
- 插入排序——直接插入排序和希尔排序
- js实现排序算法(冒泡排序,直接插入排序)
- 学习算法手记【原】- 直接插入排序
- 第十四周项目一(2)——验证算法之直接插入排序
- 八大排序之--直接插入排序