-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizedSelection.py
59 lines (51 loc) · 1.95 KB
/
RandomizedSelection.py
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
import random
def rand_selection(array, start , stop, order_stat):
if(start < stop):
# Partition by random pivot and return pivot index
pivotindex = partitionrand(array, start, stop)
# Base case covered by len 1 and len 2 arrays as well as pivot = order stat
# Recursion
if(len(array) == 1):
return array[0]
elif(len(array) == 2):
if(pivotindex == order_stat-1):
return array[pivotindex]
else:
return array[pivotindex - 1]
elif(pivotindex == order_stat-1):
return array[pivotindex]
elif(order_stat-1 < pivotindex):
array = array[start:pivotindex]
# Handle edge case (len = 1) of left side
if(len(array) == 1):
return array[0]
return rand_selection(array , start , len(array)-1, order_stat)
elif(order_stat-1 > pivotindex):
array = array[pivotindex+1:len(array)]
# Handle edge case (len = 1) of right side
if(len(array) == 1):
return array[0]
return rand_selection(array, 0, len(array)-1, (order_stat - pivotindex - 1))
# divide function
def partitionrand(array , start, stop):
# Random Pivot
randpivot = random.randrange(start, stop)
array[start], array[randpivot] = array[randpivot], array[start]
return partition(array, start, stop)
# Partition
def partition(array,start,stop):
pivot = start
i = start + 1
# partition in the arrayay starts
for j in range(start + 1, stop + 1):
if array[j] <= array[pivot]:
array[i] , array[j] = array[j] , array[i]
i = i + 1
array[pivot] , array[i-1] = array[i-1] , array[pivot]
pivot = i - 1
return pivot
# main
array = [3,8,2,5,1,4,7,6]
n = len(array)
print(rand_selection(array,0,n-1,8))
# print(array[0:n])