-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.py
82 lines (33 loc) · 1.18 KB
/
methods.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#Binary search in list
def binary_search(arr, left, right, x):
if right >= left:
mid = (right + left) // 2
if arr[mid][1] == x:
return 1
elif arr[mid][1] > x:
return binary_search(arr, left, mid - 1, x)
else:
return binary_search(arr, mid + 1, right, x)
else:
return -1
#Sorting list (Quick sort)
# function to find the partition position
def partition(array, left, right):
pivot = array[right][1]
i = left - 1
for j in range(left, right):
if array[j][1] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[right]) = (array[right], array[i + 1])
return i + 1
# function to perform quicksort
def quickSort(array, left, right):
if left < right:
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, right, left)
# recursive call on the left of pivot
quickSort(array, left, pi - 1)
# recursive call on the right of pivot
quickSort(array, pi + 1, right)