-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathExercise_5.py
More file actions
37 lines (29 loc) · 809 Bytes
/
Exercise_5.py
File metadata and controls
37 lines (29 loc) · 809 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
# Python program for implementation of Quicksort
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
pivot=arr[h]
start=l
for end in range(l,h):
if arr[end]<=pivot:
arr[start],arr[end]=arr[end],arr[start]
start+=1
arr[start],arr[h]=arr[h],arr[start]
return start
def quickSortIterative(arr, l, h):
#write your code here
callstack=[]
callstack.append((l,h))
while callstack:
l,h=callstack.pop()
if l<h:
pivot = partition(arr,l,h)
if pivot-1>l:
callstack.append((l,pivot-1))
if pivot+1<h:
callstack.append((pivot+1,h))
if __name__ == "__main__":
arr = [10, 7, 8, 9, 1, 5]
print("Before:", arr)
quickSortIterative(arr, 0, len(arr) - 1)
print("After: ", arr)