排序演算法之快速排序Java實現

來源:互聯網
上載者:User

標籤:main   swap   記憶體   ati   示範   情況   system   2016年   mil   

排序演算法之快速排序

舞蹈示範排序:

冒泡排序: http://t.cn/hrf58M

希爾排序:http://t.cn/hrosvb 

選擇排序:http://t.cn/hros6e 

插入排序:http://t.cn/hros0W 

快速排序:http://t.cn/ScTA1d 

歸併排序:http://t.cn/Sc1cGZ

  快速排序是一個就地排序,分而治之,大規模遞迴的演算法。從本質上來說,它是歸併排序的就地版本。

1、快速排序可以由下面四步組成:
(1) 如果不多於1個資料,直接返回。
(2) 一般選擇序列最左邊的值作為支點資料。
(3) 將序列分成2部分,一部分都大於支點資料,另外一部分都小於支點資料。
(4) 對兩邊利用遞迴排序數列。

快速排序比大部分排序演算法都要快。儘管我們可以在某些特殊的情況下寫出比快速排序快的演算法,但是就通常情況而言,沒有比它更快的了。快速排序是遞迴的,對於記憶體非常有限的機器來說,它不是一個好的選擇。 
2、快速排序代碼實現

package cn.com.zfc.lesson21.sort;

/**
*
* @title QuickSort
* @describe 快速排序
* @author 張富昌
* @date 2016年10月2日下午2:45:37
*/
public class QuickSort {
  // 快速排序是平均時間最快的排序演算法。
  // 基本思想:任選待排序列中的一個資料元素(通常選取第一個資料元素)作為樞軸,用它和所有的剩餘元素進行比較,將所有較它小的元素排在它的前面;
  // 將所有較它大的元素排在它的後面,經過一趟排序後,可按次資料元素所在位置為界,將可序列化分成兩個部分;
  // 再對這兩個部分重複上述過程直至每一個部分中只剩下一個資料元素為止。

  public static void main(String[] args) {
    // 聲明整型數組
    int[] array = new int[10];
    // 使用迴圈和隨機數初始化數組
    for (int i = 0; i < array.length; i++) {
      array[i] = (int) Math.round(Math.random() * 100);
    }
    System.out.println("原始數組為:");
    for (int i : array) {
      System.out.print(i + " ");
    }
    System.out.println();
    System.out.println("排序後的數組為:");
    for (int i : quickSort(array)) {
      System.out.print(i + " ");
    }
  }

  /**
  *
  * 功能:對數組進行快速排序,並且返回該數組
  *
  * 參數:int[] arr
  *
  * 傳回型別:int[]
  */
  public static int[] quickSort(int[] arr) {
    quickSortHelp(arr, 0, arr.length - 1);
    return arr;
  }

  /**
  *
  * 功能:對數組 arr[low...high] 中的記錄進行快速排序
  *
  * 參數:int[] arr, int low, int high
  *
  * 傳回型別:void
  */
  public static void quickSortHelp(int[] arr, int low, int high) {
    if (low < high) {
      // 子序列 elem[low...high] 的長度大於 1
      int pivotLoc = partition(arr, low, high);
      // 對子序列 arr[low...pivotLoc-1] 遞迴排序
      quickSortHelp(arr, low, pivotLoc - 1);
      // 對子序列 arr[pivotLoc+1...high] 遞迴排序
      quickSortHelp(arr, pivotLoc + 1, high);
    }
  }

  /**
  *
  * 功能:使樞軸元移到正確的位置,要求樞軸左邊的元素不大於樞軸,樞軸右邊的元素不小於樞軸,並返回樞軸的位置
  *
  * 參數:int[] arr, int low,int high
  *
  * 傳回型別:int
  */
  public static int partition(int[] arr, int low, int high) {
    while (low < high) {
      // arr[low] 為樞軸,使 high 右邊的元素不小於 elem[low]
      while (low < high && arr[high] >= arr[low]) {
        high--;
      }
      // 交換 arr[low] 和 arr[high]的值
      swap(arr, low, high);
      // arr[high] 為樞軸,使 low 左邊的元素不大於 arr[high]
      while (low < high && arr[low] <= arr[high]) {
        low++;
      }
      // 交換 arr[low]和 arr[high]的值
      swap(arr, low, high);
    }
    // 返回樞軸的位置
    return low;
  }

  /**
  *
  * 功能:交換兩個數的值
  *
  * 參數:int x,int y
  *
  * 傳回型別:void
  */
  public static void swap(int[] arr, int i, int j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}

排序演算法之快速排序Java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.