forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.py
43 lines (29 loc) · 816 Bytes
/
Quick_Sort.py
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
# Conquer
def Parition(array, left, right):
pivot = array[left]
index = right
for j in range(right, left - 1, -1):
if array[j] > pivot:
array[index], array[j] = array[j], array[index]
index -= 1
array[index], array[left] = array[left], array[index]
return index
# Divide array into halves
def Quick(array, left, right):
if left < right:
pivot = Parition(array, left, right)
Quick(array, left, pivot - 1)
Quick(array, pivot + 1, right)
def Quick_Sort(array):
Quick(array, 0, len(array) - 1)
# Function to print array
def Print_Array(array):
for i in range(0, len(array)):
print(array[i], end = " ")
print()
array = [2, 4, 3, 1, 6, 8, 4]
Quick_Sort(array)
Print_Array(array)
''' Output
1 2 3 4 4 6 8
'''