Skip to content

Commit bf811b2

Browse files
committed
Add quick sort algorithm
1 parent 1d9e30c commit bf811b2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

HW3/QuickSort/src/quickSort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def set_pivot(arr, low, high):
2+
pivot = arr[high]
3+
i = low - 1
4+
5+
for j in range(low, high):
6+
if(arr[j] < pivot):
7+
i += 1
8+
arr[i], arr[j] = arr[j], arr[i]
9+
10+
arr[high], arr[i + 1] = arr[i + 1], arr[high]
11+
return i + 1
12+
13+
14+
def quick_sort(arr, low, high):
15+
if(low < high):
16+
pivot_index = set_pivot(arr, low, high)
17+
quick_sort(arr, low, pivot_index - 1)
18+
quick_sort(arr, pivot_index + 1, high)
19+
20+
def main():
21+
arr = [123, 512, -12, 25, 0, 324, -532, -532, 23, 4, 3]
22+
quick_sort(arr, 0, len(arr) - 1)
23+
print(arr)
24+
25+
if __name__ == "__main__":
26+
main()

0 commit comments

Comments
 (0)