一、LintCode整數排序java
給一組整數,按照升序排序,使用選擇排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。面試
對於數組 [3, 2, 1, 4, 5]
, 排序後爲:[1, 2, 3, 4, 5]
。算法
(1)冒泡排序:從頭開始,比較相鄰的兩個元素,大的放在後面。一輪結束以後,最大的數沉底,不參與下一輪比較。重複 直至待排序的元素個數爲1。O(n2)數組
public class Test1 { public static void sort(int[] a){
if(a.size()!=0){ for(int i=0;i<a.length-1;i++){ for(int j=0;j<a.length-1-i;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } }
} } public static void main(String[] args) { int a[]={4,8,6,2,1}; sort(a); for(int i=0;i<a.length;i++){ System.out.println(a[i]); } } }
(2)插入排序:第一個元素是有序隊列,從第二個元素開始向有序隊列中插入,插入完成後將第三個元素向有序隊列中插入,依次進行,直到將最後一個元素插入完畢。markdown
public class Test1 { public static void InsertSort(int[] source) { int i, j; int insertNode;// 要插入的數據 // 從數組的第二個元素開始循環將數組中的元素插入 for (i = 1; i < source.length; i++) { // 設置數組中的第2個元素爲第一次循環要插入的數據 insertNode = source[i]; j = i - 1; // 若是要插入的元素小於第j個元素,就將第j個元素向後移 while ((j >= 0) && insertNode < source[j]) { source[j + 1] = source[j]; j--; } // 直到要插入的元素不小於第j個元素,將insertNote插入到數組中 source[j + 1] = insertNode; System.out.print("第" + i + "趟排序:"); printArray(source); } } private static void printArray(int[] source) { for (int i = 0; i < source.length; i++) { System.out.print("\t" + source[i]); } System.out.println(); } public static void main(String[] args) { int source[] = new int[] { 53, 27, 36, 15, 69, 42 }; System.out.print("初始關鍵字:"); printArray(source); System.out.println(""); InsertSort(source); System.out.print("\n\n排序後結果:"); printArray(source); } }
(3)快讀排序:app
對於一組給定的記錄,經過一趟排序後,將原序列分爲兩部分,其中前一部分的全部記錄均比後一部分的全部記錄小,而後再依次對先後兩部分的記錄進行快速排序,遞歸該過程,直到序列中的全部記錄均有序爲止。
public class Test2 { public static void main(String[] args) { int[] a = { 4,8,1,3,5 }; System.out.println("排序前:"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } quickSort(a); System.out.println("排序後:"); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } public static void quickSort(int[] a){ if(a.length>0){ sort(a,0,a.length-1); } } public static void sort(int[] a,int low,int high) { int start = low; int end = high; int key = a[low]; while (end > start) { // 從後往前比較 while (end > start && a[end] >= key) // 若是沒有比關鍵值小的,比較下一個,直到有比關鍵值小的交換位置,而後又從前日後比較 end--; if (a[end] <= key) { int temp = a[end]; a[end] = a[start]; a[start] = temp; } // 從前日後比較 while (end > start && a[start] <= key)// 若是沒有比關鍵值大的,比較下一個,直到有比關鍵值大的交換位置 start++; if (a[start] >= key) { int temp = a[start]; a[start] = a[end]; a[end] = temp; } // 此時第一次循環比較結束,關鍵值的位置已經肯定了。左邊的值都比關鍵值小,右邊的值都比關鍵值大,可是兩邊的順序還有多是不同的,進行下面的遞歸調用 } // 遞歸 if (start > low) sort(a, low, start - 1);// 左邊序列。第一個索引位置到關鍵值索引-1 if (end < high) sort(a, end + 1, high);// 右邊序列。從關鍵值索引+1到最後一個 }
(4)選擇排序:ui
對於給定的一組記錄,通過第一輪比較後獲得最小的記錄,而後將該記錄與第一個記錄的位置進行交換;接着對不包括第一個記錄之外的其餘記錄進行第二輪比較,獲得最小的記錄並與第二個記錄進行位置交換;重複該過程,直到進行比較的記錄只有一個時爲止。
public class Test2 { /** * 平均O(n^2),最好O(n^2),最壞O(n^2);空間複雜度O(1);不穩定;簡單 */ public static void selectSort(int[] a){ for(int i=0;i<a.length-1;i++){ int k=i; for(int j=k+1;j<a.length;j++){ if(a[j]<a[k]){ k=j;//記下目前找到的最小值所在的位置 } } if(i!=k){ int temp=a[i]; a[i]=a[k]; a[k]=temp; } } } public static void main(String[] args) { int a[]={7,55,8,4,2,5}; selectSort(a); for(int i=0;i<a.length;i++){ System.out.println(a[i]); } } }