-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
157 lines (127 loc) · 4.44 KB
/
quick_sort.py
File metadata and controls
157 lines (127 loc) · 4.44 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import sorting_array
import sys
class style():
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
UNDERLINE = '\033[4m'
RESET = '\033[0m'
# implementation of the code
# Function to find the partition position
def partition(array, low, high):
# Choose the rightmost element as pivot
pivot = array[high]
# Pointer for greater element
i = low - 1
# Traverse through all elements
# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:
# If element smaller than pivot is found
# swap it with the greater element pointed by i
i = i + 1
# Swapping element at i with element at j
(array[i], array[j]) = (array[j], array[i])
# Swap the pivot element with
# the greater element specified by i
(array[i + 1], array[high]) = (array[high], array[i + 1])
# Return the position from where partition is done
return i + 1
# Function to perform quicksort
def quicksort(array, low, high):
if low < high:
# Find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)
# Recursive call on the left of pivot
quicksort(array, low, pi - 1)
# Recursive call on the right of pivot
quicksort(array, pi + 1, high)
# selection sorting
def quick_sorting():
print(style.CYAN + "Create your array first.")
try:
quick_array = []
array_size = eval(input("Enter the size of your array: "))
for i in range(array_size):
array_element = eval(input("Enter the " + str(i) + " element of your array: "))
quick_array.append(array_element)
print("Your unsorted array is " + str(quick_array) + " .")
N = len(quick_array)-1
quicksort(quick_array, 0, N)
print("your Sorted array is: ")
for i in range(len(quick_array)):
print("%d" % quick_array[i], end=" , ")
back_select = input(style.RED + "For review press 0 on any other key to close: ")
if back_select == '0':
quick_sorting()
else:
quick_sort()
except:
print("invalid input")
quick_sorting()
# code review
def quick_sort_code():
print(style.WHITE + """
# implementation of the code
# Function to find the partition position
def partition(array, low, high):
# Choose the rightmost element as pivot
pivot = array[high]
# Pointer for greater element
i = low - 1
# Traverse through all elements
# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:
# If element smaller than pivot is found
# swap it with the greater element pointed by i
i = i + 1
# Swapping element at i with element at j
(array[i], array[j]) = (array[j], array[i])
# Swap the pivot element with
# the greater element specified by i
(array[i + 1], array[high]) = (array[high], array[i + 1])
# Return the position from where partition is done
return i + 1
# Function to perform quicksort
def quicksort(array, low, high):
if low < high:
# Find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)
# Recursive call on the left of pivot
quicksort(array, low, pi - 1)
# Recursive call on the right of pivot
quicksort(array, pi + 1, high)
""")
back_selection = input(style.RED + "Enter any key to exit: ")
if back_selection == "0":
quick_sort()
else:
quick_sort()
def quick_sort():
print(style.CYAN + """
[+] Select an option:
1: code review
2: implementaion
3: back
""")
selection = input("Enter youre selection: ")
if selection == '1':
quick_sort_code()
elif selection == '2':
quick_sorting()
elif selection == '3':
sorting_array.array_sorting()
else:
print(style.RED + "[+] Invalid input try again")
quick_sort()