标签:代码 ext 分析 选择排序 turn img blog ati main
每次将一个待排序的记录按照关键码的大小插入到一个已经拍好序的有序序列中,直到全部记录拍好序。
排序方法 | 最差时间分析 | 最好时间分析 | 平均时间复杂度 | 空间复杂度 | 稳定性 |
选择排序 | O(n2) | O(n) | O(n2) | O(1) | 稳定 |
1 public void insertSort(int[] array){ 2 if(array.length == 0 || array == null) 3 return; 4 int temp = 0; 5 for(int i = 1; i < array.length; i++){ 6 int j = i-1; 7 temp = array[i]; 8 for(;j >= 0 && temp < array[j];j--){ 9 array[j+1] = array[j];//后移 10 } 11 array[j+1] = temp; 12 printArray(array,i); 13 } 14 } 15 public void printArray(int a[],int count){ 16 if(count != 0) 17 System.out.print("第" + count + "次 "); 18 for(int m = 0; m < a.length; m++){ 19 if(count == m && count != 0) 20 System.out.print("|"); 21 System.out.print(a[m] + " "); 22 } 23 System.out.println(); 24 } 25 public static void main(String[] args) { 26 int a[] = {11,7,6,1,8,4,3,2}; 27 InsertSort is = new InsertSort(); 28 is.insertSort(a); 29 Arrays.sort(a); 30 }
标签:代码 ext 分析 选择排序 turn img blog ati main
原文地址:http://www.cnblogs.com/fankongkong/p/7265033.html