-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathExercise_5.py
More file actions
40 lines (29 loc) · 778 Bytes
/
Copy pathExercise_5.py
File metadata and controls
40 lines (29 loc) · 778 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
# This function is same in both iterative and recursive
def partition(arr, low, high):
#write your code here
pivot = arr[low]
left = low
for i in range(low+1,high+1):
if arr[i] < pivot:
left+=1
arr[i],arr[left] = arr[left], arr[i]
arr[low],arr[left] = arr[left],arr[low]
return left
def quickSortIterative(arr):
#write your code here
size = len(arr)
stack = [(0,size-1)]
while stack:
low,high = stack.pop()
p = partition(arr,low,high)
if p-1 > low:
stack.append((low,p-1))
if p+1 < high:
stack.append((p+1,high))
arr = [10, 7, 8, 9, 1, 5]
quickSortIterative(arr)
print("Sorted array:", arr)
# stack = [(0,5)]
# low = 0, high = 5
# p = partition(arr,0,5)