-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.py
More file actions
61 lines (50 loc) · 1.56 KB
/
selection.py
File metadata and controls
61 lines (50 loc) · 1.56 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
import numpy as np
import random
import randomized_quicksort
'''
Selection problem: Given a set of arr of n numbers and an integer i with 1<=i<=n, find the element x in arr that is larger than exactly i-1 other elements of arr.
Running time:
It takes O(n) comparisons to find the Minimum
It takes O(n) comparisons to find the Maximum
A brute-force approach: sort n elements and get the ith one in the sorted list, O(n log n) time.
The average running time of randomized_select is O(n)
'''
def randomized_select(arr,l,r,i):
if l == r:
return arr[l]
q = randomized_quicksort.randomized_partition(arr,l,r)
k = q-l+1
if i == k:
return arr[q]
elif i < k:
return randomized_select(arr,l,q-1,i)
else:
return randomized_select(arr,q+1,r,i-k)
def minimum(arr):
min_num = arr[0]
for i in range(1, len(arr)):
if min_num > arr[i]:
min_num = arr[i]
return min_num
def maximum(arr):
max_num = arr[0]
for i in range(1, len(arr)):
if max_num < arr[i]:
max_num = arr[i]
return max_num
def main():
arr = np.zeros(10)
for i in range(10):
arr[i] = random.randint(1,100)
print("Array")
print(arr)
min_num = minimum(arr)
max_num = maximum(arr)
print("Minimum from array is", min_num)
print("Maximum from array is", max_num)
print("4th smallest number in array is",randomized_select(arr,0,9,4))
print("Sorted array")
randomized_quicksort.randomized_quicksort(arr,0,9)
print(arr)
if __name__ == '__main__':
main()