-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_RandSelect.cpp
More file actions
94 lines (86 loc) · 1.97 KB
/
4_RandSelect.cpp
File metadata and controls
94 lines (86 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* P149 随机选择算法 返回第 K 大的数
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
// 随机选取 pivot,进行划分
int randPartition(int array[], int left, int right)
{
// 生成 [left, right] 内的随机数
int p = (round(1.0 * rand() / RAND_MAX * (right - left)) + left);
// 把 p 下标的值和 left 下标的值进行互换,然后直接套用普通的 partition of QuickSort
swap(array[p], array[left]);
int i = left;
int j = right;
int temp = array[left];
while (i < j)
{
while (i < j && array[j] >= temp)
{
j--;
}
array[i] = array[j];
while (i < j && array[i] < temp)
{
i++;
}
array[j] = array[i];
}
array[i] = temp;
return i;
}
/**
* array[]: 要排序的数组
* left: 起始下标
* right: 结束下标
* return(int): 返回第 K 大的数
*/
int randSelect(int array[], int left, int right, int K)
{
// bound
if (left == right)
{
return array[left];
}
// pivot 的位置
int pivot = randPartition(array, left, right);
// array[pivot] 是 [left, right] 中的第 M 大的数
int M = pivot - left + 1;
// 找到了第 K 大的数
if (K == M)
{
return array[pivot];
}
// 在左边,则是第 K 大的数
if (K < M)
{
return randSelect(array, left, pivot - 1, K);
}
// 在右边,则是第 K - M 大的数
else
{
return randSelect(array, pivot + 1, right, K - M);
}
}
int main()
{
int num;
cout << "Input the number of the sequence: ";
cin >> num;
// dynamic array
int *array = new int[num];
// input
for (int i = 0; i < num; i++)
{
cin >> array[i];
}
int K;
cout << "Input K: ";
cin >> K;
cout << "The Kth largest number is: " << randSelect(array, 0, num - 1, K) << endl;
return 0;
}