Skip to content

Commit 225ec0e

Browse files
committed
added quicksort and binary search
1 parent fb354d1 commit 225ec0e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Utils/sort.py

+90
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,70 @@ def merge(arr, left, mid, right):
8585

8686

8787
# quick sort
88+
def quickSort(arr):
89+
def partition(arr, left, right):
90+
pivotIndex = left
91+
pivot = arr[right]
92+
print("working on index {} to {}:".format(left, right))
93+
94+
for i in range(left, right):
95+
print("i={}: comparing {} and {}".format(i, arr[i], pivot))
96+
if arr[i] <= pivot:
97+
arr[pivotIndex], arr[i] = arr[i], arr[pivotIndex]
98+
pivotIndex += 1
99+
print("{}, pivotIndex={}".format(arr, pivotIndex))
100+
arr[pivotIndex], arr[right] = arr[right], arr[pivotIndex]
101+
return pivotIndex
102+
103+
def sort(arr, left, right):
104+
if left < right:
105+
pivotIndex = partition(arr, left, right)
106+
sort(arr, left, pivotIndex-1)
107+
sort(arr, pivotIndex+1, right)
108+
109+
sort(arr, 0, len(arr)-1)
110+
88111

89112
# binary search (recursive)
113+
def binarySearch_rec(arr, target, left=0, right=None):
114+
if right is None:
115+
right = len(arr)-1
116+
if left > right:
117+
return -1
118+
mid = (left+right)//2
119+
if arr[mid] == target:
120+
print("{} == {}".format(arr[mid], target))
121+
return mid
122+
elif arr[mid] > target:
123+
print("{} > {}. will now search in {}".format(
124+
arr[mid], target, arr[left:mid]))
125+
return binarySearch_rec(arr, target, left, mid-1)
126+
else:
127+
print("{} < {}. will now search in {}".format(
128+
arr[mid], target, arr[mid+1:right+1]))
129+
return binarySearch_rec(arr, target, mid+1, right)
130+
131+
90132
# binary search (iterative)
133+
def binarySearch_iter(arr, target):
134+
left = 0
135+
right = len(arr)-1
136+
while left < right:
137+
mid = (left+right) // 2
138+
if arr[mid] == target:
139+
print("{} == {}".format(arr[mid], target))
140+
return mid
141+
elif arr[mid] > target:
142+
print("{} > {}. will now search in {}".format(
143+
arr[mid], target, arr[left:mid]))
144+
right = mid
145+
else:
146+
print("{} < {}. will now search in {}".format(
147+
arr[mid], target, arr[mid+1:right+1]))
148+
left = mid+1
149+
return -1
150+
151+
91152
if __name__ == "__main__":
92153
# generate random array
93154
arr = list()
@@ -127,3 +188,32 @@ def merge(arr, left, mid, right):
127188
arr_copy = list(arr)
128189
mergeSort(arr_copy)
129190
print("Sorted:", arr_copy)
191+
192+
# quick sort test
193+
print()
194+
print("*** Quick Sort ***")
195+
print("Original Array:", arr)
196+
arr_copy = list(arr)
197+
quickSort(arr_copy)
198+
print("Sorted:", arr_copy)
199+
200+
# binary search test
201+
print()
202+
print("*** Binary Search (Recursive) ***")
203+
target = 32
204+
print("Sorted Array: {}, target = {}".format(arr_copy, target))
205+
index = binarySearch_rec(arr_copy, target)
206+
if index > 0:
207+
print("Index of {} = {}".format(target, index))
208+
else:
209+
print("{} not found in array".format(target))
210+
211+
print()
212+
print("*** Binary Search (Iterative) ***")
213+
target = 97
214+
print("Sorted Array: {}, target = {}".format(arr_copy, target))
215+
index = binarySearch_iter(arr_copy, target)
216+
if index > 0:
217+
print("Index of {} = {}".format(target, index))
218+
else:
219+
print("{} not found in array".format(target))

0 commit comments

Comments
 (0)