數據結構JAVA-----直接插入排序

import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException{
       int a[]={ 49, 38, 65, 97, 76, 13, 27, 50};
       insertSort(a);
       for (int i:a) {
System.out.print(i+" ");
}
}


private static void insertSort(int[] a) {
// TODO Auto-generated method stub
int temp;
for (int i = 1; i < a.length; i++) {
for (int j = i; j >0; j--) {
if (a[j]<a[j-1]) {
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;

}
}

}
}

}

最好的情況下時間複雜度爲O(n),平均爲O(n*n),最壞O(n*n),空間複雜度O(1)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章