快排
#include
using namespace std;
int Partition(int* ar, int left, int right) {int i &#61; left - 1, j &#61; left;int tmp &#61; ar[j];while (j <&#61; right) {if (ar[j] <&#61; tmp) {std::swap(ar[i &#43; 1], ar[j]);i &#43;&#61; 1;}j &#43;&#61; 1;}std::swap(ar[left], ar[i]);return i;
}
int Partition1(int* ar, int left, int right)
{int i &#61; left, j &#61; left &#43; 1;int tmp &#61; ar[i];while (i < right && j < right){while (j <&#61; right && ar[j] <&#61; tmp) j&#43;&#43;; if (j > right) break;i &#61; j &#43; 1;while (i <&#61; right && ar[i] > tmp) i&#43;&#43;; if (i > right) break;std::swap(ar[i], ar[j]);}std::swap(ar[j - 1], ar[left]);return j - 1;
}
int Partition2(int* ar, int left, int right)
{int i &#61; left, j &#61; right;int tmp &#61; ar[i];while (i < j){while (i < j && ar[j] > tmp) j--;if (i >&#61; j) break;while (i < j && ar[i] <&#61; tmp) i&#43;&#43;;if (i >&#61; j) break;std::swap(ar[i], ar[j]);}std::swap(ar[i], ar[left]);return i;
}
void QuickPass(int* ar, int left, int right)
{if (left < right){int pos &#61; Partition(ar, left, right);QuickPass(ar, 0, pos - 1);QuickPass(ar, pos &#43; 1, right);}
}
void QuickSort(int* ar, int n)
{if (nullptr &#61;&#61; ar || n < 1) return;QuickPass(ar, 0, n - 1);
}int main()
{int arr[] &#61; { 4,5,2,3,4,4,3,1,7,8,4,9,10,6 };QuickSort(arr, sizeof(arr) / sizeof(arr[0]));for (int a : arr)std::cout << a << &#39; &#39;;std::cout << std::endl;return 0;
}
有关快排优化&#xff1a;参考&#xff1a;万字长文带你走进快速排序的前世今生【拓跋阿秀】
- 非递归
- 优化基准选取位置&#xff0c;如三位取中法等
- 设定一个阈值&#xff0c;在阈值范围内使用插入排序&#xff08;插入排序在元素比较少时效率最高&#xff09;
- 三向切分。
使用快排的的一次划分&#xff0c;我们可以实现寻找无序数列中的第K小。
#include
using namespace std;
int OnePartition(int* ar, int left, int right)
{int midVal &#61; ar[left]; while (left < right){while (left < right){if (ar[right] >&#61; midVal) right--;else {swap(ar[right], ar[left&#43;&#43;]); break;}}while (left < right){if (ar[left] <&#61; midVal) left&#43;&#43;;else {swap(ar[left], ar[right--]); break;}}}return left;
}
int Select_K(int* ar, int left, int right, int pos)
{int i &#61; OnePartition(ar, left, right);if (i < pos - 1) i &#61; Select_K(ar, i &#43; 1, right, pos); else if (i > pos - 1) i &#61; Select_K(ar, left, i - 1, pos); else return ar[i];
}int main()
{int arr[] &#61; { 67,12,89,23,90,100 ,34,78,56,45 };int n &#61; sizeof(arr) / sizeof(arr[0]);
除此之外&#xff0c;我们可以使用优先级队列&#xff0c;或直接使用数据结构堆。
优先级队列&#xff0c;大数优先级高&#xff08;底层大根堆实现&#xff09;。使用小数优先级高的方式也可以实现下列算法。
#include
#include
#include
using namespace std;
int KthLargest(int arr[], int n, int k)
{if (nullptr &#61;&#61; arr || n <&#61; 0 || k > n) return -1;priority_queue<int, vector<int>, less<int>> pq;for (int i &#61; 0; i < n; i&#43;&#43;){pq.push(arr[i]); }while (k > 1){pq.pop();k--;}return pq.top();
}
int KthSmall(int arr[], int n, int k)
{if (nullptr &#61;&#61; arr || n <&#61; 0 || k > n) return -1;priority_queue<int> pq;for (int i &#61; 0; i < n; i&#43;&#43;){pq.push(arr[i]); }k &#61; n - k &#43; 1; while (k > 1){pq.pop();k--;}return pq.top();
}int main()
{int arr[] &#61; { 67,12,89,23,90,100 ,34,78,56,45 };int n &#61; sizeof(arr) / sizeof(arr[0]);for (int i &#61; 1; i <&#61; n; i&#43;&#43;)cout << i << ":\t small " << KthSmall(arr, n, i)<< "\t big" << KthLargest(arr, n, i) << endl;return 0;
}
直接使用数据结构堆。
#include
#include
#include
using namespace std;
int KthLargest(int arr[], int n, int k)
{if (nullptr &#61;&#61; arr || n <&#61; 0 || k > n) return -1;std::make_heap(arr, arr &#43; n, less<int>()); while (k > 1){pop_heap(arr, arr &#43; n); n--;make_heap(arr, arr &#43; n, less<int>()); k--;}return arr[0];
}
int KthSmall(int arr[], int n, int k)
{if (nullptr &#61;&#61; arr || n <&#61; 0 || k > n) return -1;std::make_heap(arr, arr &#43; n, greater<int>()); while (k > 1){pop_heap(arr, arr &#43; n); n--;make_heap(arr, arr &#43; n, greater<int>()); k--;}return arr[0];
}int main()
{int arr[] &#61; { 67,12,89,23,90,100 ,34,78,56,45 };int brr[] &#61; { 67,12,89,23,90,100 ,34,78,56,45 };int n &#61; sizeof(arr) / sizeof(arr[0]);for (int i &#61; 1; i <&#61; n; i&#43;&#43;)cout << i << ":\t small " << KthSmall(arr, n, i)<< "\t big" << KthLargest(arr, n, i) << endl;return 0;
}