随机选择算法-来自 胡凡 曾磊的《算法笔记》
#include <cstdio> #include <cstdlib> #include <ctime> #include <algorithm> #include <math.h> using namespace std; const int maxn = 100010; int A[maxn], n; int randPartition(int A[], int left, int right){ int p = (round(1.0*rand()/RAND_MAX*(right - left) + left)); swap(A[p],A[left]); int temp = A[left]; while(left < right){ while(left<right&&A[right]>temp) right--; A[left] = A[right]; while(left<right&&A[left]<=temp) left++; A[right] = A[left]; } A[left] = temp; return left; } //随机选择算法 void randSelect(int A[], int left, int right, int K){ if(left==right) return; int p = randPartition(A,left