Skip to content

Commit fcd456e

Browse files
committed
Added Python Quick Sort Implementation
Rename quick_sort.py to Quick_sort.py
1 parent 4fda30e commit fcd456e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Sorting/Quick_sort.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def quick_sort(arr, left_index, right_index):
2+
if left_index < right_index:
3+
partition_index = partition(arr, left_index, right_index)
4+
5+
quick_sort(arr, left_index, partition_index-1)
6+
quick_sort(arr, partition_index+1, right_index)
7+
8+
def partition(arr, left_index, right_index):
9+
pivot = arr[right_index]
10+
partition_index = left_index
11+
12+
for i in range(left_index, right_index):
13+
if arr[i] < pivot:
14+
arr[i], arr[partition_index] = arr[partition_index], arr[i]
15+
partition_index += 1
16+
arr[partition_index], arr[right_index] = arr[right_index], arr[partition_index]
17+
18+
return partition_index
19+
20+
if __name__ == "__main__":
21+
arr = [1,3,5,4,2,6,8,9,7,10,13,12,14,16,15,17,19,18]
22+
23+
quick_sort(arr, 0, len(arr)-1)
24+
print(arr)

0 commit comments

Comments
 (0)