forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_2.py
More file actions
33 lines (27 loc) · 833 Bytes
/
Exercise_2.py
File metadata and controls
33 lines (27 loc) · 833 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
# Python program for implementation of RECURSIVE QUICKSORT.
# give you explanation for the approach
def partition(arr,low,high):
#print(high)
p= high # Pivot
small= low
for i in range(low, high+1):
if arr[i] < arr[p]:
arr[i], arr[small]= arr[small], arr[i]
small+=1
# Swap small and pivot.
arr[small], arr[p]= arr[p], arr[small]
return small
# Function to do Quick sort
def quickSort(arr,low,high):
if low <high:
pivot= partition(arr, low, high)
quickSort(arr, low, pivot-1)
quickSort(arr, pivot+1, high)
# Driver code to test above
arr = [10, 7, 8, 9, 1, 5]
#arr = [10, 80, 30, 90, 40, 50, 70]
n = len(arr)
quickSort(arr,0, n-1)
print ("Sorted array is:")
for i in range(n):
print (arr[i])