forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_2.py
54 lines (41 loc) · 1.29 KB
/
Exercise_2.py
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
48
49
50
51
52
53
54
# Python program for implementation of Quicksort Sort
"""
Approach --
1. Use a pivot element to swap rest pf the elements around it
2. Use a pointer element to keep track smaller elements
3. Swap elements smaller than pivot element in the front
4. Continue till the end of the list
Time complexity - 0(n log n)
Space complexity - O(n)
"""
# give you explanation for the approach
def partition(arr, low, high):
ptr = low
pivot = arr[high]
for i in range(low, high):
if pivot >= arr[i]:
# swap elements smaller than the pivot at the front
arr[i], arr[ptr] = arr[ptr], arr[i]
# increment position of pointer by 1
ptr += 1
# swap last elements with ptr
arr[ptr], arr[high] = arr[high], arr[ptr]
return ptr
# Function to do Quick sort
def quickSort(arr, low, high):
if len(arr) == 1: return arr
if low < high:
# partition function to swap elements around pivot
pi = partition(arr, low, high)
# sort left elements
quickSort(arr, low, pi - 1)
# sort right elements
quickSort(arr, pi + 1, high)
return arr
# 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]),