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
40 lines (31 loc) · 1022 Bytes
/
Exercise_2.py
File metadata and controls
40 lines (31 loc) · 1022 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
# Python program for implementation of Quicksort Sort
# give you explanation for the approach
def partition(arr,low,high):
if low < high:
pivot = high
lowPointer = low
for i in range(low, high):
if arr[i] < arr[pivot]:
temp = arr[i]
arr[i] = arr[lowPointer]
arr[lowPointer] = temp
lowPointer += 1
temp = arr[pivot]
arr[pivot] = arr[lowPointer]
arr[lowPointer] = temp
return lowPointer
#write your code here
# Function to do Quick sort
def quickSort(arr,low,high):
if low < high:
partitionIndex = partition(arr, low, high)
quickSort(arr, low, partitionIndex - 1)
quickSort(arr,partitionIndex + 1, high)
#write your code here
# Driver code to test above
arr = [10, 7, 8, 9, 1, 5, 3, 2,16, 13]
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i], end = " "),