1. 程式人生 > >08-Little prince's trip to Java-陣列排序

08-Little prince's trip to Java-陣列排序

常用的排序方法有以下幾種:

  • 1> 氣泡排序
  • 2> 選擇排序
  • 3> 插入排序
  • 4> shell排序(希爾排序)

1.氣泡排序

  • 時間複雜度:O(n^2)
  • 空間複雜度:O(1)
  • 穩定性:穩定

在這裡插入圖片描述
執行過程如下:
在這裡插入圖片描述
第一次迴圈過程如下:

   如圖:執行完第一次迴圈最大值 9 被換到了最後一位,剩餘迴圈同第一次迴圈依次將較大值換到相應位置。
在這裡插入圖片描述
完整程式碼:

public class TestArray_BubbleSort {
    public static void bubbleSort(int [] array) {
        int temp = 0;
        for
(int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; 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 [] array = {6,4,8,2,3,9,1,7,5}; bubbleSort(array); System.out.println(Arrays.toString(array)); } }

執行結果:
在這裡插入圖片描述

氣泡排序的優化

  • 時間複雜度: O(n)

  如果是一個有序陣列進行第一次for迴圈之後進行判斷count的真假。count為假,則跳出迴圈。

public class TestArray_BubbleSort
{ public static void bubbleSort(int [] array) { boolean count = false; int temp = 0; for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; count = true; } if (!count ) {//加上該語句後氣泡排序的時間複雜度變為O(n) break; } } } } public static void main(String[] args) { int [] array = {1,2,3,4,5,6,7,8,9}; bubbleSort(array); System.out.println(Arrays.toString(array)); } }

2.選擇性排序

  • 時間複雜度:O(n^2)
  • 空間複雜度:O(1)
  • 穩定性:不穩定

在這裡插入圖片描述

執行過程如下:
在這裡插入圖片描述
第一次迴圈過程如下:
在這裡插入圖片描述
完整程式碼:

public class TestSortDemo {

    public static void selectSort(int [] array) {
        int temp = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]) {
                    temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {
        int [] array = {12,4,3,6,1,7};
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }
}

執行結果:
在這裡插入圖片描述

3.直接插入排序

  • 時間複雜度:
      1> 無序:O(n^2)
      2> 有序:O(n)------越有序越快

在這裡插入圖片描述
執行過程:
在這裡插入圖片描述
執行一次迴圈過程:

  第一次迴圈較短不明顯故此處列舉第二次迴圈過程:
在這裡插入圖片描述
完整程式碼:

public class TestSortDemo {
    public static void insertSort(int [] array) {
        int tmp = 0;
        for (int i = 1; i < array.length; i++) {
            tmp = array[i];
            int j = 0;
            for (j = i - 1; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public static void main(String[] args) {
        int [] array = {12,4,3,6,1,7};
        insertSort(array);
        System.out.println(Arrays.toString(array));
    }
}

執行結果:
在這裡插入圖片描述

4.shell排序

  • 穩定性:不穩定
  • 採用分組思想,組內進行插入排序最後分為一組進行直接插入排序(越有序越快,此時大數字基本都在陣列末尾)

注:因為shell排序用到了直接插入排序如果不注意條件,可能程式碼也可執行而且結果正確。

在這裡插入圖片描述
完整程式碼:

public class TestDemoShell {
    public static void shellSort(int [] array) {
        int drr[] = {5,3,1};
        for (int i = 0; i < drr.length; i++) {
            shell(array,drr[i]);
        }
    }
    public static void shell(int [] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = 0;
            for (j = i - gap; j >= 0; j-=gap) {
                if (array[j] > temp) {
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = temp;
        }
    }
    public static void main(String[] args) {
        int [] array = {7,4,9,34,0,8,5,22,55,6,12,33,56,89,77};
        shellSort(array);
        System.out.println(Arrays.toString(array));
    }
}

執行結果:
在這裡插入圖片描述