插入排序

 1 /**
 2  * 功能:直接插入排序
 3  * 原理:将一个记录插入到已经排好的有序表中,从而得到一个新的,记录数增1的有序表
 4  * 直接插入排序时间复杂度:(1)最好的情况下,为o(n) (2)最坏的情况下为o(n^2)
 5  * */
 6 public class InsertSort {
 7 
 8     public int[] insertSort(int[] array){
 9         
10         for(int i=1; i<array.length; i++){
11             int temp = array[i];
12             int index = i;
13             
14             for(int j=i-1; j>=0; j--){
15                 if(array[j] > temp){
16                     array[j+1] = array[j];
17                     index = j;
18                 }
19             }
20             
21             array[index] = temp;
22         }
23         
24         return array;
25     }
26 }
原文地址:https://www.cnblogs.com/jiangyi-uestc/p/5883539.html