-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathRandomizedSelect.py
More file actions
38 lines (32 loc) · 864 Bytes
/
RandomizedSelect.py
File metadata and controls
38 lines (32 loc) · 864 Bytes
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
import random
def RandomizedPartition(A, p, r):
it = random.randint(p, r)
A[p], A[it] = A[it], A[p]
x = A[p] # To get a randomized elem.
i = p - 1
for j in range(p, r + 1):
if A[j] <= x:
i += 1
A[i], A[j] = A[j], A[i]
A[p], A[i] = A[i], A[p]
return i
def RandomizedSelect(A, p, r, i):
# if p == r:
# return A[p]
q = RandomizedPartition(A, p, r)
if q == i:
return A[q]
if q > i:
return RandomizedSelect(A, p, q - 1, i)
if q < i:
return RandomizedSelect(A, q + 1, r, i)
print "Now displaying Randomized Select"
A = []
s = random.randint(5, 100000)
for i in range(0, s):
A.append(random.randint(0, 100000000))
# print A
i = random.randint(0, s - 1)
print "The position of ", i, "is :", RandomizedSelect(A, 0, len(A) - 1, i)
A.sort()
print A[i]