幾種排序算法 持續更新

1 概述

    本文對比較常用且比較高效的排序算法進行了總結和解析,並貼出了比較精簡的實現代碼,包括選擇排序、插入排序、歸併排序、希爾排序、快速排序等。算法性能比較如下圖所示:

 

2 選擇排序

    選擇排序的第一趟處理是從數據序列所有n個數據中選擇一個最小的數據作爲有序序列中的第1個元素並將它定位在第一號存儲位置,第二趟處理從數據序列的n-1個數據中選擇一個第二小的元素作爲有序序列中的第2個元素並將它定位在第二號存儲位置,依此類推,當第n-1趟處理從數據序列的剩下的2個元素中選擇一個較小的元素作爲有序序列中的最後第2個元素並將它定位在倒數第二號存儲位置,至此,整個的排序處理過程就已完成。

    代碼如下:

複製代碼
public class SelectionSort {
    public void selectionSort(int[] array) {
        int temp;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j <= array.length - 1; j++) {// 第i個和第j個比較j可以取到最後一位,所以要用j<=array.length-1
                if (array[i] > array[j]) {// 注意和冒泡排序的區別,這裏是i和j比較。
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
            // 打印每趟排序結果
            for (int m = 0; m <= array.length - 1; m++) {
                System.out.print(array[m] + "\t");
            }
            System.out.println();
        }
    }
  
    public static void main(String[] args) {
        SelectionSort selectionSort = new SelectionSort();
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        selectionSort.selectionSort(array);
        for (int m = 0; m <= array.length - 1; m++) {
            System.out.print(array[m] + "\t");
        }
    }
}
複製代碼

 

3 快速排序

    快速排序(Quicksort)是對冒泡排序的一種改進。由C. A. R. Hoare在1962年提出。它的基本思想是:通過一趟排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然 後再按此方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。

    代碼如下:

複製代碼
public class QuickSort {
    public int partition(int[] sortArray, int low, int height) {
        int key = sortArray[low];// 剛開始以第一個數爲標誌數據
        while (low < height) {
            while (low < height && sortArray[height] >= key)
                height--;// 從後面開始找,找到比key值小的數爲止
            sortArray[low] = sortArray[height];// 將該數放到key值的左邊
            while (low < height && sortArray[low] <= key)
                low++;// 從前面開始找,找到比key值大的數爲止
            sortArray[height] = sortArray[low];// 將該數放到key值的右邊
        }
        sortArray[low] = key;// 把key值填充到low位置,下次重新找key值
        // 打印每次排序結果
        for (int i = 0; i <= sortArray.length - 1; i++) {
            System.out.print(sortArray[i] + "\t");
        }
        System.out.println();
        return low;
    }
  
    public void sort(int[] sortArray, int low, int height) {
        if (low < height) {
            int result = partition(sortArray, low, height);
            sort(sortArray, low, result - 1);
            sort(sortArray, result + 1, height);
        }
    }
  
    public static void main(String[] args) {
        QuickSort quickSort = new QuickSort();
        int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
        for (int i = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
        System.out.println();
        quickSort.sort(array, 0, 8);
        for (int i = 0; i <= array.length - 1; i++) {
            System.out.print(array[i] + "\t");
        }
    }
}
複製代碼

 

4 冒泡排序

    冒泡排序(Bubble Sort)是一種簡單的排序算法。它重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重複地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個算法的名字由來是因爲越小的元素會經由交換慢慢“浮”到數列的頂端。

    代碼如下:

 1 public class BubbleSort{
 2      public static void main(String[] args){
 3          int score[] = {67, 69, 75, 87, 89, 90, 99, 100};
 4        for (int i = 0; i < score.length -1; i++){    //最多做n-1趟排序
 5              for(int j = 0 ;j < score.length - i - 1; j++){    //對當前無序區間score[0......length-i-1]進行排序(j的範圍很關鍵,這個範圍是在逐步縮小的)
 6                  if(score[j] < score[j + 1]){    //把小的值交換到後面
 7                      int temp = score[j];
 8                      score[j] = score[j + 1];
 9                      score[j + 1] = temp;
10                  }
11              }            
12              System.out.print("第" + (i + 1) + "次排序結果:");
13              for(int a = 0; a < score.length; a++){
14                  System.out.print(score[a] + "\t");
15              }
16              System.out.println("");
17          }
18              System.out.print("最終排序結果:");
19              for(int a = 0; a < score.length; a++){
20                  System.out.print(score[a] + "\t");
21         }
22      }
23  }


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