File tree 1 file changed +10
-3
lines changed
1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change 6
6
7
7
def quick_sort (arr , lower_index , upper_index ):
8
8
9
+ # already sorted
9
10
if lower_index >= upper_index :
10
11
return
11
-
12
+
13
+ # selecting the last element as the pivot
12
14
pivot = arr [- 1 ]
13
15
left = lower_index
14
16
right = upper_index - 1
15
17
16
18
while left <= right :
19
+ # keep increasing left until a number greater than pivot is found
17
20
while left <= right and arr [left ] < pivot :
18
21
left += 1
19
-
22
+
23
+ # keep decreasing right until a number lesser than pivot is found
20
24
while left <= right and arr [right ] > pivot :
21
25
right -= 1
22
26
23
27
if left <= right :
28
+ # swapping the two numbers
24
29
arr [left ], arr [right ] = arr [right ], arr [left ]
25
30
left += 1
26
31
right -= 1
27
-
32
+
33
+ # swap left element with pivot element,
34
+ # to bring the pivot element to its appropritate position
28
35
arr [left ], arr [upper_index ] = arr [upper_index ], arr [left ]
29
36
quick_sort (arr , 0 , left - 1 )
30
37
quick_sort (arr , left + 1 , upper_index )
You can’t perform that action at this time.
0 commit comments