-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquicksort.py
More file actions
47 lines (21 loc) · 730 Bytes
/
quicksort.py
File metadata and controls
47 lines (21 loc) · 730 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def partition (arr, low, high):
pivot = arr[high]; # pivot
i = (low - 1) # Index of smaller element
for j in range (low, high):
#If current element is smaller than or
# equal to pivot element
if arr[j] <= pivot:
#increment index of smaller element
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1)
def quicksort(arr, low, high):
if low < high:
p = partition(arr, low, high)
quicksort(arr, low, p - 1)
quicksort(arr, p + 1, high)
arr = [5, 10, 8, 7, 3, 6, 12, 2, 7]
quicksort(arr, 0, len(arr)-1)
print("Sorted array:")
print(arr)