-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_select.py
More file actions
executable file
·69 lines (50 loc) · 1.47 KB
/
Copy pathrandom_select.py
File metadata and controls
executable file
·69 lines (50 loc) · 1.47 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Discussed in section 8
import numpy as np
import sys
import argparse
def partition(A, pivot):
'''
In-place partition in linear time
as specified by randomSelect algorthm
'''
# Put pivot element first
A[0], A[pivot] = A[pivot], A[0]
j = 1 # Boundary between halves of partition
for i in range(1, len(A)):
if A[i] < A[0]:
# Put A[i] at end of left half of partition
A[i], A[j] = A[j], A[i]
j += 1
# Put pivot element at boundary
A[j - 1], A[0] = A[0], A[j - 1]
return A, j - 1
def randomSelect(A, i):
'''
Use randomization of pivot point to find
ith element by magnitude
'''
if len(A) == 1:
return A
p = np.random.randint(0, len(A))
(A, p) = partition(A, p)
# Hone in without sorting unnecessary array regions
if p < i:
A = randomSelect(A[p + 1:], i - (p + 1))
elif p > i:
A = randomSelect(A[:p], i)
elif p == i:
A = A[p:p + 1]
return A
if __name__ == "__main__":
parser = argparse.ArgumentParser(
'randomSelect in-place sorting algorithm in O(n log n)')
parser.add_argument('-A', type=float, nargs='+',
help='Array to sort')
parser.add_argument('-i', type=float,
help='Order of statistic to return')
args = parser.parse_args()
A = np.array(args.A)
s = randomSelect(A, args.i)[0]
print s