-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.py
More file actions
32 lines (27 loc) · 763 Bytes
/
quicksort.py
File metadata and controls
32 lines (27 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
def sort(arr):
random.shuffle(arr) # shuffle the array to avoid worst case scenario for quicksort
quicksort(arr,0,len(arr)-1)
def quicksort(arr, low, high):
if low<high:
pivot = partition(arr,low,high)
quicksort(arr, low, pivot-1)
quicksort(arr, pivot+1, high)
def partition(arr, low, high):
pivot = arr[low]
#chunk1 less or equal to pivot
chunk1_last = low
for i in range(low+1,high+1):
if arr[i] < pivot:
swap(arr, chunk1_last+1,i)
chunk1_last+=1
swap(arr, low, chunk1_last)
return chunk1_last
def swap(arr, a, b):
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
arr=[random.randint(1,100) for i in range(10)]
print(arr)
sort(arr)
print(arr)