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
63 lines (53 loc) · 1.49 KB
/
Exercise_5.py
File metadata and controls
63 lines (53 loc) · 1.49 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#//Time Complexity :O(nlogn)
#// Space Complexity : O(logn)
#// Did this code successfully run on Leetcode : No
#// Any problem you faced while coding this :Had difficulty in converting logic to code.
# Python program for implementation of Quicksort Sort
# This function is same in both iterative and recursive
def partition(arr, l, h):
#write your code here
i = l - 1
pivot = arr[h]
for j in range(l, h):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[h] = arr[h], arr[i + 1]
return i + 1
def quickSortIterative(arr, l, h):
#write your code here
# Create an auxiliary stack
size = h -l + 1
stack = [0] * size
top = -1
stack[top] = l
top += 1
stack[top] = h
#keep popping until it is empty
while top >= 0:
h = stack[top]
top -= 1
l = stack[top]
top -= 1
#setting pivot element position
p = partition(arr, l, h)
#elemnets on left side of pivot push left side of stack
if p -1 > l:
top += 1
stack[top] = l
top += 1
stack[top] = p - 1
#elemnts on right side of pivot push ot right sode of the stack
if p + 1 < h:
top += 1
stack[top] = p + 1
top +=1
stack [top] = h
if __name__ == "__main__":
# Sample array to test
data = [10, 7, 8, 9, 1, 5]
n = len(data)
print("Original array:", data)
# l is 0 (start index) and h is n-1 (last index)
quickSortIterative(data, 0, n - 1)
print("Sorted array: ", data)