-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathExercise_2.py
More file actions
44 lines (32 loc) · 1.11 KB
/
Copy pathExercise_2.py
File metadata and controls
44 lines (32 loc) · 1.11 KB
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
# Python program for implementation of Quicksort Sort
# give you explanation for the approach
def partition(arr,low,high):
# Choose the rightmost element as pivot
pivot = arr[high]
# Index of smaller element (indicates right position of pivot)
i = low - 1
for j in range(low, high):
# If current element is smaller than or equal to pivot
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
# Place pivot at its correct position
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
# Function to do Quick sort
def quickSort(arr,low,high):
#write your code here
if low < high:
# pi is partitioning index, arr[pi] is now at right place
pi = partition(arr, low, high)
# Recursively sort elements before and after partition
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)
# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr,0,n-1)
print("Sorted array is:")
for i in range(n):
print("%d" % arr[i], end=" ")
print()