日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

自用快排 C语言

發布時間:2023/12/31 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自用快排 C语言 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//劃分算法 嚴蔚敏版本 int Partition(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;//返回存放樞軸的最終位置 }// 獲得三個數中的中間數 int getSecond(int A[],int a, int b, int c) {int max = A[a] > A[b] ? A[a] : A[b];max = max > A[c] ? max : A[c];int min = A[a] < A[b] ? A[a] : A[b];min = min < A[c] ? min : A[c];int second = A[a] + A[b] + A[c] - max - min;if (A[a] == second)return a;else if (A[b] == second)return b;else if (A[c] == second)return c; }//劃分算法 改進版本 int PartitionInproved(int A[], int low, int high) {int median = (low + high) / 2;int pivotNum = getSecond(A, low, high, median);int pivot = A[pivotNum];//將當前表紅第一個元素設為樞軸,對表進行劃分A[pivotNum] = 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) {if (low < high) {//遞歸跳出條件//int pivotpos = PartitionInproved(A, low, high);//劃分 int pivotpos = Partition(A, low, high);//劃分/*printf(" Sort");for (int i = 0; i <= high; i++) {printf(" %d", A[i]);}printf("\n");*/QuickSort(A, low, pivotpos - 1);QuickSort(A, pivotpos + 1, high);} }

?

總結

以上是生活随笔為你收集整理的自用快排 C语言的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。