-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathrandomized_algorithms.h
56 lines (46 loc) · 1.18 KB
/
randomized_algorithms.h
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
//
// randomized_algorithms.h
// ProbabilisticAnalysis_RandomizedAlgorithms
//
// Created by Xiaohang Su on 2/10/17.
// Copyright © 2017 Xiaohang Su. All rights reserved.
//
#ifndef randomized_algorithms_h
#define randomized_algorithms_h
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cmath>
struct sortType {
int *sortKey;
int val;
sortType() {
sortKey = NULL;
val = NULL;
}
bool operator < (const sortType& t_sortType) const {
return *sortKey < *(t_sortType.sortKey);
}
};
int random(int lowBound, int upBound) {
return rand() % (upBound - lowBound) + lowBound;
}
void permute_by_sorting(std::vector<int> &A) {
int n = static_cast<int>(A.size());
std::vector<sortType> P(n, sortType());
for (int i = 0; i < n; i++) {
P[i].sortKey = new int(random(1, pow(n, 3)));
P[i].val = A[i];
}
sort(P.begin(), P.end()); // already sort A according P
for (int i = 0; i < n; i++) {
A[i] = P[i].val;
}
}
template <typename T>
void randomize_in_place(std::vector<T>& vec) {
int n = vec.size();
for (int i = 0; i < n; i++) {
}
}
#endif /* randomized_algorithms_h */