亚洲精品国产精品乱码视色,下载应用成人电影AVapp,成人影院下载视频

      <track id="gfhue"><i id="gfhue"></i></track><input id="gfhue"></input>

      1. 更多精彩內(nèi)容,歡迎關(guān)注:

        視頻號
        視頻號

        抖音
        抖音

        快手
        快手

        微博
        微博

        快速排序c語言

        文檔

        快速排序c語言

        快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因為它的內(nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實現(xiàn)出來。
        推薦度:
        導(dǎo)讀快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因為它的內(nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實現(xiàn)出來。
        .example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px}

        排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是快速排序算法:

        快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因為它的內(nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實現(xiàn)出來。

        快速排序使用分治法(Divide and conquer)策略來把一個串行(list)分為兩個子串行(sub-lists)。

        快速排序又是一種分而治之思想在排序算法上的典型應(yīng)用。本質(zhì)上來看,快速排序應(yīng)該算是在冒泡排序基礎(chǔ)上的遞歸分治法。

        快速排序的名字起的是簡單粗暴,因為一聽到這個名字你就知道它存在的意義,就是快,而且效率高!它是處理大數(shù)據(jù)最快的排序算法之一了。雖然 Worst Case 的時間復(fù)雜度達到了 O(n?),但是人家就是優(yōu)秀,在大多數(shù)情況下都比平均時間復(fù)雜度為 O(n logn) 的排序算法表現(xiàn)要更好,可是這是為什么呢,我也不知道。好在我的強迫癥又犯了,查了 N 多資料終于在《算法藝術(shù)與信息學(xué)競賽》上找到了滿意的答案:

        快速排序的最壞運行情況是 O(n?),比如說順序數(shù)列的快排。但它的平攤期望時間是 O(nlogn),且 O(nlogn) 記號中隱含的常數(shù)因子很小,比復(fù)雜度穩(wěn)定等于 O(nlogn) 的歸并排序要小很多。所以,對絕大多數(shù)順序性較弱的隨機數(shù)列而言,快速排序總是優(yōu)于歸并排序。

        1. 算法步驟

        從數(shù)列中挑出一個元素,稱為 "基準"(pivot);

        重新排序數(shù)列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的后面(相同的數(shù)可以到任一邊)。在這個分區(qū)退出之后,該基準就處于數(shù)列的中間位置。這個稱為分區(qū)(partition)操作;

        遞歸地(recursive)把小于基準值元素的子數(shù)列和大于基準值元素的子數(shù)列排序;

        2. 動圖演示

        代碼實現(xiàn)JavaScript實例 function quickSort(arr, left, right) {? ? var len = arr.length,? ? ? ? partitionIndex,? ? ? ? left = typeof left != 'number' ? 0 : left,? ? ? ? right = typeof right != 'number' ? len - 1 : right;? ? if (left < right) {? ? ? ? partitionIndex = partition(arr, left, right);? ? ? ? quickSort(arr, left, partitionIndex-1);? ? ? ? quickSort(arr, partitionIndex+1, right);? ? }? ? return arr;}function partition(arr, left ,right) { ? ? // 分區(qū)操作? ? var pivot = left, ? ? ? ? ? ? ? ? ? ? ?// 設(shè)定基準值(pivot)? ? ? ? index = pivot + 1;? ? for (var i = index; i <= right; i++) {? ? ? ? if (arr[i] < arr[pivot]) {? ? ? ? ? ? swap(arr, i, index);? ? ? ? ? ? index++;? ? ? ? } ? ? ? ?? ? }? ? swap(arr, pivot, index - 1);? ? return index-1;}function swap(arr, i, j) {? ? var temp = arr[i];? ? arr[i] = arr[j];? ? arr[j] = temp;}function partition2(arr, low, high) {? let pivot = arr[low];? while (low < high) {? ? while (low < high && arr[high] > pivot) {? ? ? --high;? ? }? ? arr[low] = arr[high];? ? while (low < high && arr[low] <= pivot) {? ? ? ++low;? ? }? ? arr[high] = arr[low];? }? arr[low] = pivot;? return low;}function quickSort2(arr, low, high) {? if (low < high) {? ? let pivot = partition2(arr, low, high);? ? quickSort2(arr, low, pivot - 1);? ? quickSort2(arr, pivot + 1, high);? }? return arr;}Python實例 def quickSort(arr, left=None, right=None):? ? left = 0 if not isinstance(left,(int, float)) else left? ? right = len(arr)-1 if not isinstance(right,(int, float)) else right? ? if left < right:? ? ? ? partitionIndex = partition(arr, left, right)? ? ? ? quickSort(arr, left, partitionIndex-1)? ? ? ? quickSort(arr, partitionIndex+1, right)? ? return arrdef partition(arr, left, right):? ? pivot = left? ? index = pivot+1? ? i = index? ? while ?i <= right:? ? ? ? if arr[i] < arr[pivot]:? ? ? ? ? ? swap(arr, i, index)? ? ? ? ? ? index+=1? ? ? ? i+=1? ? swap(arr,pivot,index-1)? ? return index-1def swap(arr, i, j):? ? arr[i], arr[j] = arr[j], arr[i]Go實例 func quickSort(arr []int) []int {? ? ? ? return _quickSort(arr, 0, len(arr)-1)}func _quickSort(arr []int, left, right int) []int {? ? ? ? if left < right {? ? ? ? ? ? ? ? partitionIndex := partition(arr, left, right)? ? ? ? ? ? ? ? _quickSort(arr, left, partitionIndex-1)? ? ? ? ? ? ? ? _quickSort(arr, partitionIndex+1, right)? ? ? ? }? ? ? ? return arr}func partition(arr []int, left, right int) int {? ? ? ? pivot := left? ? ? ? index := pivot + 1? ? ? ? for i := index; i <= right; i++ {? ? ? ? ? ? ? ? if arr[i] < arr[pivot] {? ? ? ? ? ? ? ? ? ? ? ? swap(arr, i, index)? ? ? ? ? ? ? ? ? ? ? ? index += 1? ? ? ? ? ? ? ? }? ? ? ? }? ? ? ? swap(arr, pivot, index-1)? ? ? ? return index - 1}func swap(arr []int, i, j int) {? ? ? ? arr[i], arr[j] = arr[j], arr[i]}C++ 實例 //嚴蔚敏《數(shù)據(jù)結(jié)構(gòu)》標(biāo)準分割函數(shù)?Paritition1(int A[], int low, int high) {? ?int pivot = A[low];? ?while (low < high) {? ? ?while (low < high && A[high] >= pivot) {? ? ? ?--high;? ? ?}? ? ?A[low] = A[high];? ? ?while (low < high && A[low] <= pivot) {? ? ? ?++low;? ? ?}? ? ?A[high] = A[low];? ?}? ?A[low] = pivot;? ?return low;?}?void QuickSort(int A[], int low, int high) //快排母函數(shù)?{? ?if (low < high) {? ? ?int pivot = Paritition1(A, low, high);? ? ?QuickSort(A, low, pivot - 1);? ? ?QuickSort(A, pivot + 1, high);? ?}?}Java實例 public class QuickSort implements IArraySort {? ? @Override? ? public int[] sort(int[] sourceArray) throws Exception {? ? ? ? // 對 arr 進行拷貝,不改變參數(shù)內(nèi)容? ? ? ? int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);? ? ? ? return quickSort(arr, 0, arr.length - 1);? ? }? ? private int[] quickSort(int[] arr, int left, int right) {? ? ? ? if (left < right) {? ? ? ? ? ? int partitionIndex = partition(arr, left, right);? ? ? ? ? ? quickSort(arr, left, partitionIndex - 1);? ? ? ? ? ? quickSort(arr, partitionIndex + 1, right);? ? ? ? }? ? ? ? return arr;? ? }? ? private int partition(int[] arr, int left, int right) {? ? ? ? // 設(shè)定基準值(pivot)? ? ? ? int pivot = left;? ? ? ? int index = pivot + 1;? ? ? ? for (int i = index; i <= right; i++) {? ? ? ? ? ? if (arr[i] < arr[pivot]) {? ? ? ? ? ? ? ? swap(arr, i, index);? ? ? ? ? ? ? ? index++;? ? ? ? ? ? }? ? ? ? }? ? ? ? swap(arr, pivot, index - 1);? ? ? ? return index - 1;? ? }? ? private void swap(int[] arr, int i, int j) {? ? ? ? int temp = arr[i];? ? ? ? arr[i] = arr[j];? ? ? ? arr[j] = temp;? ? }}PHP實例 function quickSort($arr){? ? if (count($arr) <= 1)? ? ? ? return $arr;? ? $middle = $arr[0];? ? $leftArray = array();? ? $rightArray = array();? ? for ($i = 1; $i < count($arr); $i++) {? ? ? ? if ($arr[$i] > $middle)? ? ? ? ? ? $rightArray[] = $arr[$i];? ? ? ? else? ? ? ? ? ? $leftArray[] = $arr[$i];? ? }? ? $leftArray = quickSort($leftArray);? ? $leftArray[] = $middle;? ? $rightArray = quickSort($rightArray);? ? return array_merge($leftArray, $rightArray);}C實例 typedef struct _Range {? ? int start, end;} Range;Range new_Range(int s, int e) {? ? Range r;? ? r.start = s;? ? r.end = e;? ? return r;}void swap(int *x, int *y) {? ? int t = *x;? ? *x = *y;? ? *y = t;}void quick_sort(int arr[], const int len) {? ? if (len <= 0)? ? ? ? return; // 避免len等於負值時引發(fā)段錯誤(Segment Fault)? ? // r[]模擬列表,p為數(shù)量,r[p++]為push,r[--p]為pop且取得元素? ? Range r[len];? ? int p = 0;? ? r[p++] = new_Range(0, len - 1);? ? while (p) {? ? ? ? Range range = r[--p];? ? ? ? if (range.start >= range.end)? ? ? ? ? ? continue;? ? ? ? int mid = arr[(range.start + range.end) / 2]; // 選取中間點為基準點? ? ? ? int left = range.start, right = range.end;? ? ? ? do {? ? ? ? ? ? while (arr[left] < mid) ++left; ? // 檢測基準點左側(cè)是否符合要求? ? ? ? ? ? while (arr[right] > mid) --right; //檢測基準點右側(cè)是否符合要求? ? ? ? ? ? if (left <= right) {? ? ? ? ? ? ? ? swap(&arr[left], &arr[right]);? ? ? ? ? ? ? ? left++;? ? ? ? ? ? ? ? right--; ? ? ? ? ? ? ? // 移動指針以繼續(xù)? ? ? ? ? ? }? ? ? ? } while (left <= right);? ? ? ? if (range.start < right) r[p++] = new_Range(range.start, right);? ? ? ? if (range.end > left) r[p++] = new_Range(left, range.end);? ? }}

        遞歸法

        實例 void swap(int *x, int *y) {? ? int t = *x;? ? *x = *y;? ? *y = t;}void quick_sort_recursive(int arr[], int start, int end) {? ? if (start >= end)? ? ? ? return;? ? int mid = arr[end];? ? int left = start, right = end - 1;? ? while (left < right) {? ? ? ? while (arr[left] < mid && left < right)? ? ? ? ? ? left++;? ? ? ? while (arr[right] >= mid && left < right)? ? ? ? ? ? right--;? ? ? ? swap(&arr[left], &arr[right]);? ? }? ? if (arr[left] >= arr[end])? ? ? ? swap(&arr[left], &arr[end]);? ? else? ? ? ? left++;? ? if (left)? ? ? ? quick_sort_recursive(arr, start, left - 1);? ? quick_sort_recursive(arr, left + 1, end);}void quick_sort(int arr[], int len) {? ? quick_sort_recursive(arr, 0, len - 1);}C++

        函數(shù)法

        sort(a,a + n);// 排序a[0]-a[n-1]的所有數(shù).

        迭代法

        實例 // 參考:http://www.dutor.net/index.php/2011/04/recursive-iterative-quick-sort/struct Range {? ? int start, end;? ? Range(int s = 0, int e = 0) {? ? ? ? start = s, end = e;? ? }};template // 整數(shù)或浮點數(shù)皆可使用,若要使用物件(class)時必須設(shè)定"小於"(<)、"大於"(>)、"不小於"(>=)的運算子功能void quick_sort(T arr[], const int len) {? ? if (len <= 0)? ? ? ? return; // 避免len等於負值時宣告堆疊陣列當(dāng)機? ? // r[]模擬堆疊,p為數(shù)量,r[p++]為push,r[--p]為pop且取得元素? ? Range r[len];? ? int p = 0;? ? r[p++] = Range(0, len - 1);? ? while (p) {? ? ? ? Range range = r[--p];? ? ? ? if (range.start >= range.end)? ? ? ? ? ? continue;? ? ? ? T mid = arr[range.end];? ? ? ? int left = range.start, right = range.end - 1;? ? ? ? while (left < right) {? ? ? ? ? ? while (arr[left] < mid && left < right) left++;? ? ? ? ? ? while (arr[right] >= mid && left < right) right--;? ? ? ? ? ? std::swap(arr[left], arr[right]);? ? ? ? }? ? ? ? if (arr[left] >= arr[range.end])? ? ? ? ? ? std::swap(arr[left], arr[range.end]);? ? ? ? else? ? ? ? ? ? left++;? ? ? ? r[p++] = Range(range.start, left - 1);? ? ? ? r[p++] = Range(left + 1, range.end);? ? }}

        遞歸法

        實例 template void quick_sort_recursive(T arr[], int start, int end) {? ? if (start >= end)? ? ? ? return;? ? T mid = arr[end];? ? int left = start, right = end - 1;? ? while (left < right) { //在整個范圍內(nèi)搜尋比樞紐元值小或大的元素,然后將左側(cè)元素與右側(cè)元素交換? ? ? ? while (arr[left] < mid && left < right) //試圖在左側(cè)找到一個比樞紐元更大的元素? ? ? ? ? ? left++;? ? ? ? while (arr[right] >= mid && left < right) //試圖在右側(cè)找到一個比樞紐元更小的元素? ? ? ? ? ? right--;? ? ? ? std::swap(arr[left], arr[right]); //交換元素? ? }? ? if (arr[left] >= arr[end])? ? ? ? std::swap(arr[left], arr[end]);? ? else? ? ? ? left++;? ? quick_sort_recursive(arr, start, left - 1);? ? quick_sort_recursive(arr, left + 1, end);}template //整數(shù)或浮點數(shù)皆可使用,若要使用物件(class)時必須設(shè)定"小於"(<)、"大於"(>)、"不小於"(>=)的運算子功能void quick_sort(T arr[], int len) {? ? quick_sort_recursive(arr, 0, len - 1);}

        參考地址:

        https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/6.quickSort.md

        https://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F

        以下是熱心網(wǎng)友對快速排序算法的補充,僅供參考:

        熱心網(wǎng)友提供的補充1:

        上方?jīng)]有C#實現(xiàn),我補充一下,如下所示:

        //快速排序(目標(biāo)數(shù)組,數(shù)組的起始位置,數(shù)組的終止位置)
        static void QuickSort(int[] array, int left = 0, int right = -1)
        {
            if (right.Equals(-1)) right = array.Length - 1;
            try
            {
                int keyValuePosition;   //記錄關(guān)鍵值的下標(biāo)
                //當(dāng)傳遞的目標(biāo)數(shù)組含有兩個以上的元素時,進行遞歸調(diào)用。(即:當(dāng)傳遞的目標(biāo)數(shù)組只含有一個元素時,此趟排序結(jié)束)
                if (left < right)
                {
                    keyValuePosition = Partion(array, left, right);  //獲取關(guān)鍵值的下標(biāo)(快排的核心)
                    QuickSort(array, left, keyValuePosition - 1);    //遞歸調(diào)用,快排劃分出來的左區(qū)間
                    QuickSort(array, keyValuePosition + 1, right);   //遞歸調(diào)用,快排劃分出來的右區(qū)間
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex);
            }
        }
        
        ///快速排序的核心部分:確定關(guān)鍵值在數(shù)組中的位置,以此將數(shù)組劃分成左右兩區(qū)間,關(guān)鍵值游離在外。(返回關(guān)鍵值應(yīng)在數(shù)組中的下標(biāo))
        static int Partion(int[] array, int left, int right)
        {
            int leftIndex = left;        //記錄目標(biāo)數(shù)組的起始位置(后續(xù)動態(tài)的左側(cè)下標(biāo))
            int rightIndex = right;      //記錄目標(biāo)數(shù)組的結(jié)束位置(后續(xù)動態(tài)的右側(cè)下標(biāo))
            int keyValue = array[left];  //數(shù)組的第一個元素作為關(guān)鍵值
            int temp;
            //當(dāng) (左側(cè)動態(tài)下標(biāo) == 右側(cè)動態(tài)下標(biāo)) 時跳出循環(huán)
            while (leftIndex < rightIndex)
            {
                while (leftIndex < rightIndex && array[leftIndex] <= keyValue)  //左側(cè)動態(tài)下標(biāo)逐漸增加,直至找到大于keyValue的下標(biāo)
                {
                    leftIndex++;
                }
                while (leftIndex < rightIndex && array[rightIndex] > keyValue)  //右側(cè)動態(tài)下標(biāo)逐漸減小,直至找到小于或等于keyValue的下標(biāo)
                {
                    rightIndex--;
                }
                if (leftIndex < rightIndex)  //如果leftIndex < rightIndex,則交換左右動態(tài)下標(biāo)所指定的值;當(dāng)leftIndex==rightIndex時,跳出整個循環(huán)
                {
                    temp = array[leftIndex];
                    array[leftIndex] = array[rightIndex];
                    array[rightIndex] = temp;
                }
            }
        
            //當(dāng)左右兩個動態(tài)下標(biāo)相等時(即:左右下標(biāo)指向同一個位置),此時便可以確定keyValue的準確位置
            temp = keyValue;
            if (temp < array[rightIndex])   //當(dāng)keyValue < 左右下標(biāo)同時指向的值,將keyValue與rightIndex - 1指向的值交換,并返回rightIndex - 1
            {
                array[left] = array[rightIndex - 1];
                array[rightIndex - 1] = temp;
                return rightIndex - 1;
            }
            else //當(dāng)keyValue >= 左右下標(biāo)同時指向的值,將keyValue與rightIndex指向的值交換,并返回rightIndex
            {
                array[left] = array[rightIndex];
                array[rightIndex] = temp;
                return rightIndex;
            }
        }

        熱心網(wǎng)友提供的補充2:

        補充 scala 實現(xiàn)版本:

        /**  
        * @Auther: huowang 
        * @Date: 19:34:47 2020/12/10  
        * @DES:  分區(qū)交換算法(快速排序發(fā))  
        * @Modified By:  
        */
        object PartitionExchange {
        
          /**    
           * 分區(qū)內(nèi)切割    
           * @param arr    
           * @param left    
           * @param right    
           * @return    
          */  
        def partition(arr:Array[Int],left:Int,right: Int):Int={
            // 獲取基準元素 直接選取最右側(cè)一個元素為基準元素   
            val pv=arr(right)
        
            // 把最左邊一個索引作為堆疊索引   
             var storeIndex=left
            //操作數(shù)組 -1是因為 最右邊一個元素是基準元素  
           for (i <- left to right-1 ){
               if(arr(i)<=pv){
                 //把小于基準元素的元素 都堆到集合左端        
                  swap(arr,storeIndex,i)
                 // 把用于堆疊索引往前移動一個  
                  storeIndex=storeIndex+1 
              }
              //如果出現(xiàn)了比基準元素大的元素,那么則不會移動堆疊索引  
              // 但是如果之后又出現(xiàn)了比基準元素小的元素,那邊會與這個大的元素交換位置
              // 進而使大的元素永遠出現(xiàn)在堆疊索引右側(cè)
            }
            // 這里最有右的元素,其實是基準元素,我們把基準元素和最后堆疊索引對應(yīng)的元素調(diào)換位置
            // 這樣基準元素左邊就都是大于它的元素了  
             swap(arr,right,storeIndex)
            // 返回堆疊索引位置,目前堆疊索引指向的就是基準元素 
             storeIndex
          }
        
        def quicksort(arr:Array[Int],left: Int,right: Int):Array[Int]={
        
            if(right>left){
              // 左右索引不重合 
             // 隨便選擇一個元素作為基準 就選擇最左邊的吧 
             var pivotIndex=0 
             // 切割返回基準元素 
             pivotIndex= partition(arr,left,right)
              // 遞歸對切割形成的兩個子集進行排序 
              quicksort(arr,left,pivotIndex-1)
              quicksort(arr,pivotIndex,right)
            }
            arr
          }
        
        
          /**    
            * 調(diào)換 a b 元素在數(shù)組中的位置    
            * @param arr    
            * @param a    
            * @param b    
            */  
        def swap(arr:Array[Int],a:Int,b:Int)={
            val tmp=arr(a)
            arr(a)=arr(b)
            arr(b)=tmp
          }
        
        def main(args: Array[String]): Unit = {
            // 測試
            val arr=Array(5, 2, 9,11,3,6,8,4,0,0)
            val arrNew=quicksort(arr,0,arr.size-1)
            println(arrNew.toList.mkString(","))
        
          }
        }

        熱心網(wǎng)友提供的補充3:

        補充一下迭代法的 python 實現(xiàn):

        def _partition(array:list, start:int, end:int) -> int:
            """
            將數(shù)組指定片段進行左右劃分,首先選擇中位元素為中值。
        
            比中位元素小的置于其左,與中位元素相等或比中位元素大的置于其右,
        
            最后返回中位元素的下標(biāo)位置。
            """
            # 以中位元素為中值劃分,盡量避免極端情況
            mid = (start + end) >> 1
            array[start], array[mid] = array[mid], array[start]
            
            # 劃分的實現(xiàn)
            i, j = start, end
            x = array[start]
            while (i < j):
                if (i < j and array[j] >= x): j -= 1
                array[i] = array[j]
                if (i < j and array[i] < x): i += 1
                array[j] = array[i]
            array[i] = x
        
            return i
        
        
        def quickSort(array:list) -> list:
            """
            迭代法快速排序,隊列結(jié)構(gòu)輔助實現(xiàn)。
            """
            sorted_array = array.copy()
            length = len(sorted_array)
            # 使用隊列保存每次劃分的二元組:(起始下標(biāo),終止下標(biāo))
            queue = []
            queue.append((0, length - 1))
        
            # 隊列為空,則所有劃分操作執(zhí)行完畢
            while len(queue):
                left, right = queue.pop(0)
                pos = _partition(sorted_array, left, right)
                # 默認長度為 1 的序列有序,那么區(qū)間長度 > 1 才需要劃分,才需要保存到隊列中
                if (left < pos - 1): queue.append((left, pos - 1))
                if (pos + 1 < right): queue.append((pos + 1, right))
            
            return sorted_array
        
        
        if __name__ == "__main__":
            array = [21, -17, 1, -27, 41, 17, -5, -49]
            sorted_array = quickSort(array)
            print("排序前:{array1}
        排序后:{array2}".format(array1=array, array2=sorted_array))
        以上為快速排序算法詳細介紹,插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等排序算法各有優(yōu)缺點,用一張圖概括:

        關(guān)于時間復(fù)雜度

        平方階 (O(n2)) 排序 各類簡單排序:直接插入、直接選擇和冒泡排序。

        線性對數(shù)階 (O(nlog2n)) 排序 快速排序、堆排序和歸并排序;

        O(n1+§)) 排序,§ 是介于 0 和 1 之間的常數(shù)。 希爾排序

        線性階 (O(n)) 排序 基數(shù)排序,此外還有桶、箱排序。

        關(guān)于穩(wěn)定性

        穩(wěn)定的排序算法:冒泡排序、插入排序、歸并排序和基數(shù)排序。

        不是穩(wěn)定的排序算法:選擇排序、快速排序、希爾排序、堆排序。

        名詞解釋:

        n:數(shù)據(jù)規(guī)模

        k:"桶"的個數(shù)

        In-place:占用常數(shù)內(nèi)存,不占用額外內(nèi)存

        Out-place:占用額外內(nèi)存

        穩(wěn)定性:排序后 2 個相等鍵值的順序和排序之前它們的順序相同

        文檔

        快速排序c語言

        快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個項目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見。事實上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因為它的內(nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實現(xiàn)出來。
        推薦度:
        為你推薦
        資訊專欄
        熱門視頻
        相關(guān)推薦
        兩句關(guān)于動物的詩句 踏青的詩詞名句 含有燕子的詩句 簡述歸并排序算法的基本思路 希爾排序怎么排 直接選擇排序 基數(shù)排序流程圖 python冒泡排序算法 桶排序c語言 計數(shù)排序菜鳥教程 堆排序算法規(guī)則 描寫蘭花的詩句古詩 快速排序算法java 帶有動物的古詩 關(guān)于踏青的唯美詩句 關(guān)于描寫燕子的詩句 歸并排序算法python思想 希爾排序?qū)崿F(xiàn) 直接選擇排序穩(wěn)定嗎 基數(shù)排序算法代碼 關(guān)于蘭花的詩句古詩 堆排序法排序怎么排 計數(shù)排序算法c++實現(xiàn) 桶式排序 關(guān)于放風(fēng)箏的古詩 冒泡排序python 基數(shù)排序怎么排 選擇排序法 希爾排序 歸并排序python 描寫燕子的古詩絕句 踏青詩句最出名詩句 描寫夏天的詩句簡單 關(guān)于寫小動物的詩 java快速排序 關(guān)于蘭花的詩句兩句 堆排序怎么排 描寫元宵節(jié)的唯美詩詞 如何按照計數(shù)進行排序 桶排序
        Top