File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Picks the random index as the pivot
3
+ """
4
+ import random
5
+
6
+ def partition (A , left_index , right_index ):
7
+ pivot = A [left_index ]
8
+ i = left_index + 1
9
+ for j in range (left_index + 1 , right_index ):
10
+ if A [j ] < pivot :
11
+ A [j ], A [i ] = A [i ], A [j ]
12
+ i += 1
13
+ A [left_index ], A [i - 1 ] = A [i - 1 ], A [left_index ]
14
+ return i - 1
15
+
16
+ def quick_sort_random (A , left , right ):
17
+ if left < right :
18
+ pivot = random .randint (left , right - 1 )
19
+ A [pivot ], A [left ] = A [left ], A [pivot ] #switches the pivot with the left most bound
20
+ pivot_index = partition (A , left , right )
21
+ quick_sort_random (A , left , pivot_index ) #recursive quicksort to the left of the pivot point
22
+ quick_sort_random (A , pivot_index + 1 , right ) #recursive quicksort to the right of the pivot point
23
+
24
+ def main ():
25
+ user_input = input ('Enter numbers separated by a comma:\n ' ).strip ()
26
+ arr = [int (item ) for item in user_input .split (',' )]
27
+
28
+ quick_sort_random (arr , 0 , len (arr ))
29
+
30
+ print (arr )
31
+
32
+ if __name__ == "__main__" :
33
+ main ()
You can’t perform that action at this time.
0 commit comments