Skip to content

Commit b2ea437

Browse files
authored
Update Quick_sort.py
1 parent f3745ba commit b2ea437

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Searching_and_Sorting/Quick_sort.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,32 @@
66

77
def quick_sort(arr, lower_index, upper_index):
88

9+
# already sorted
910
if lower_index >= upper_index:
1011
return
11-
12+
13+
# selecting the last element as the pivot
1214
pivot = arr[-1]
1315
left = lower_index
1416
right = upper_index - 1
1517

1618
while left <= right:
19+
# keep increasing left until a number greater than pivot is found
1720
while left <= right and arr[left] < pivot:
1821
left += 1
19-
22+
23+
# keep decreasing right until a number lesser than pivot is found
2024
while left <= right and arr[right] > pivot:
2125
right -= 1
2226

2327
if left <= right:
28+
# swapping the two numbers
2429
arr[left], arr[right] = arr[right], arr[left]
2530
left += 1
2631
right -= 1
27-
32+
33+
# swap left element with pivot element,
34+
# to bring the pivot element to its appropritate position
2835
arr[left], arr[upper_index] = arr[upper_index], arr[left]
2936
quick_sort(arr, 0, left-1)
3037
quick_sort(arr, left+1, upper_index)

0 commit comments

Comments
 (0)