forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_5.py
More file actions
47 lines (34 loc) · 941 Bytes
/
Exercise_5.py
File metadata and controls
47 lines (34 loc) · 941 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
# Python program for implementation of Quicksort
# This function is same in both iterative and recursive
def partition(arr, l, h):
p= h # Pivot
small= l
for i in range(l, h+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
def quickSortIterative(arr, l, h):
#write your code here
stack = []
stack.append(l)
stack.append(h)
while stack :
h = stack.pop()
l = stack.pop()
p = partition(arr, l, h)
if l < p-1 :
stack.append(l)
stack.append(p-1)
if h > p+1 :
stack.append(p+1)
stack.append(h)
# Driver code to test above
arr = [4, 3, 5, 2, 1, 3, 2, 3]
n = len(arr) - 1
quickSortIterative(arr, 0, n)
print ("Sorted array is:")
for i in range(n+1):
print ("%d" %arr[i])