-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.py
More file actions
119 lines (97 loc) · 3.41 KB
/
Copy pathcounting_sort.py
File metadata and controls
119 lines (97 loc) · 3.41 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
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
def countsort(input_array):
# Finding the maximum element of input_array.
M = max(input_array)
# Initializing count_array with 0
count_array = [0] * (M + 1)
# Mapping each element of input_array as an index of count_array
for num in input_array:
count_array[num] += 1
# Calculating prefix sum at every index of count_array
for i in range(1, M + 1):
count_array[i] += count_array[i - 1]
# Creating output_array from count_array
output_array = [0] * len(input_array)
for i in range(len(input_array) - 1, -1, -1):
output_array[count_array[input_array[i]] - 1] = input_array[i]
count_array[input_array[i]] -= 1
return output_array
# selection sorting
def counting_sorting():
print(style.CYAN + "Create your array first.")
try:
counting_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: "))
counting_array.append(array_element)
print("Your unsorted array is " + str(counting_array) + " .")
# Traverse through all array elements
counting_array = countsort(counting_array)
print("your Sorted array is: ")
for i in range(len(counting_array)):
print("%d" % counting_array[i], end=" , ")
back_select = input(style.RED + "For review press 0 on any other key to close: ")
if back_select == '0':
counting_sorting()
else:
count_sort()
except:
print("invalid input")
counting_sorting()
# code review
def count_sort_code():
print(style.WHITE + """def count_sort(input_array):
# Finding the maximum element of input_array.
M = max(input_array)
# Initializing count_array with 0
count_array = [0] * (M + 1)
# Mapping each element of input_array as an index of count_array
for num in input_array:
count_array[num] += 1
# Calculating prefix sum at every index of count_array
for i in range(1, M + 1):
count_array[i] += count_array[i - 1]
# Creating output_array from count_array
output_array = [0] * len(input_array)
for i in range(len(input_array) - 1, -1, -1):
output_array[count_array[input_array[i]] - 1] = input_array[i]
count_array[input_array[i]] -= 1
return output_array
""")
back_selection = input(style.RED + "Enter any key to exit: ")
if back_selection == "0":
count_sort()
else:
count_sort()
def count_sort():
print(style.CYAN + """
[+] Select an option:
1: code review
2: implementaion
3: back
""")
selection = input("Enter youre selection: ")
if selection == '1':
count_sort_code()
elif selection == '2':
counting_sorting()
elif selection == '3':
sorting_array.array_sorting()
else:
print(style.RED + "[+] Invalid input try again")
count_sort()