-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinAndRandomQuery.cpp
More file actions
75 lines (60 loc) · 1.29 KB
/
MinAndRandomQuery.cpp
File metadata and controls
75 lines (60 loc) · 1.29 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
#include <iostream>
#include <list>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef list<char> List;
typedef list<char>::iterator It;
typedef vector<It> vec;
void printList(const List& l)
{
for(auto each : l)
{
cout << '[' << each << ']' << "->";
}
cout << "[NULL]";
}
char getRandom(vec& R, List& l, int& start, int& end)
{
int pos = start + rand()%(end - start + 1);
char ch = *R[pos];
l.erase(R[pos]);
R[pos] = R[end];
end --;
return ch;
}
char getMin(vec& R, List& l, int& start, int& end)
{
char ch = l.front();
l.erase(R[start]);
start ++;
return ch;
}
int main() {
srand (time(NULL));
List l;
vec R;
string s = "GEEKSFORGEEKS";
std::sort(s.begin(), s.end());
for(int i = 0; i < s.size(); ++i)
{
l.push_back(s[i]);
}
for(It each = l.begin(); each != l.end(); ++each)
R.push_back(each);
int start = 0, end = s.size() - 1;
for(int i = 0; i < s.size(); ++ i)
{
printList(l);
if(rand()%2)
{
cout << "\nMinimum = " << getMin(R, l, start, end) << endl;
}
else
{
cout << "\nRandom = " << getRandom(R, l, start, end) << endl;
}
}
return 0;
}