-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathselction_pro.cpp
81 lines (47 loc) · 1.4 KB
/
selction_pro.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SWAP(a,b) { int TEMP = a; a = b; b = TEMP; }
using namespace std;
int RandomIzed_Select(int Array[], int low, int high, int i)
{
if(low == high)
return Array[low];
if(i > high - low + 1 || i <= 0)
return -1;
srand(time(0));
int index = rand() % (high - low) + low;
SWAP(Array[index], Array[high]);
int first = low;
int last = high;
int key = Array[last];
while( first < last)
{
while( Array[first] <= key && first < last) first++;
Array[last] = Array[first];
while( Array[last] >= key && first < last) last--;
Array[first] = Array[last];
}
Array[first] = key;
int k = first - low + 1;
if(i == k)
return Array[first];
else if(i < k)
return RandomIzed_Select(Array, low, first - 1, i);
else return RandomIzed_Select(Array, last + 1, high, i - k);
}
int main()
{
int TempArray[10] = {4, 5, 7, 1, 3, 2, 6, 8, 9, 0};
int Index;
while( (cout << "请输入需要数组中第几小的数据:",cin >> Index) )
{
int TempData = RandomIzed_Select(TempArray, 0, 9, Index);
cout << "数组中第" << Index << "小的数据为" << TempData << endl;
cout << "数组为:" << endl;
for( int i = 0; i < 10; i++)
cout << TempArray[i] << " ";
cout << endl;
}
return 0;
}