-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSortAndBinarySearch.py
More file actions
53 lines (45 loc) · 1.34 KB
/
quickSortAndBinarySearch.py
File metadata and controls
53 lines (45 loc) · 1.34 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
import random
#arr = [random.randint(-3, 5) for i in range(6)]
arr = ['see', 'a', 'fish', 'joke', 'pass', 'egg', 'jack']
def quick_sort(array, lo_indx=None, hi_indx=None):
if lo_indx is None:
lo_indx = 0
if hi_indx is None:
hi_indx = len(array) - 1
if lo_indx >= hi_indx:
return None
p = partition(array, lo_indx, hi_indx)
quick_sort(array, lo_indx, p - 1)
quick_sort(array, p + 1, hi_indx)
def partition(array, lo_indx, hi_indx):
support_element = array[lo_indx]
i = lo_indx + 1
j = hi_indx
while True:
while array[i] < support_element and i < hi_indx:
i += 1
while array[j] > support_element:
j -= 1
if i >= j:
break
array[i], array[j] = array[j], array[i]
i += 1
j -= 1
array[lo_indx], array[j] = array[j], array[lo_indx]
return j
def binary_search(array, item):
start = 0
end = len(array) - 1
while start <= end:
middle = (start + end) // 2
if array[middle] == item:
return f'Index position of required item "{item}" is {middle}'
if array[middle] > item:
end = middle - 1
if array[middle] < item:
start = middle + 1
return f'Element {item} not found'
print(arr)
quick_sort(arr)
print(arr)
print(binary_search(arr, 'j'))