forked from seeditsolution/pythonprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting Codes.py
More file actions
152 lines (124 loc) · 3.58 KB
/
Copy pathSorting Codes.py
File metadata and controls
152 lines (124 loc) · 3.58 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
# Selection Sort
import sys
a = [ 170, 40, 72, 90, 802, 24, 2, 66, 32]
string = "SUBSCRIBETOSEEDITSOLUTIONS"
for i in range(len(a)):
min_idx = i
for j in range(i+1, len(a)):
if a[min_idx] > a[j]:
min_idx = j
a[i], a[min_idx] = a[min_idx], a[i]
print ("Selection Sorted array: ")
for i in range(len(a)):
print("%d \t" %a[i], sep=' ', end='', flush=True)
# Bubble Sort
def bubbleSort(a):
n = len(a)
for i in range(n-1):
for j in range(0, n-i-1):
if a[j] > a[j+1] :
a[j], a[j+1] = a[j+1], a[j]
# Insertion Sort
def insertionSort(a):
for i in range(1, len(a)):
key = a[i]
j = i-1
while j >=0 and key < a[j] :
a[j+1] = a[j]
j -= 1
a[j+1] = key
# Heap Sort
def heapify(a, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and a[i] < a[l]:
largest = l
if r < n and a[largest] < a[r]:
largest = r
if largest != i:
a[i],a[largest] = a[largest],a[i]
heapify(a, n, largest)
def heapSort(a):
n = len(a)
for i in range(n // 2 - 1, -1, -1):
heapify(a, n, i)
for i in range(n-1, 0, -1):
a[i], a[0] = a[0], a[i]
heapify(a, i, 0)
# Radix Sort
def countingSort(a, exp1):
n = len(a)
output = [0] * (n)
count = [0] * (10)
for i in range(0, n):
index = (a[i]/exp1)
count[int((index)%10)] += 1
for i in range(1,10):
count[i] += count[i-1]
i = n-1
while i>=0:
index = (a[i]/exp1)
output[ count[ int((index)%10) ] - 1] = a[i]
count[int((index)%10)] -= 1
i -= 1
i = 0
for i in range(0,len(a)):
a[i] = output[i]
def radixSort(a):
max1 = max(a)
exp = 1
while max1/exp > 0:
countingSort(a,exp)
exp *= 10
# Shell Sort
def shellSort(a):
n = len(a)
gap = n/2
while int(gap) > 0:
for i in range(int(gap),int(n)):
temp = a[i]
j = int(i)
while int(j) >= int(gap) and a[int(j)-int(gap)] >temp:
a[j] = a[j-gap]
j -= gap
a[j] = temp
gap /= 2
# Counting sort
def countSort(string):
output = [0 for i in range(256)]
count = [0 for i in range(256)]
ans = ["" for _ in string]
for i in string:
count[ord(i)] += 1
for i in range(256):
count[i] += count[i-1]
for i in range(len(string)):
output[count[ord(string[i])]-1] = string[i]
count[ord(string[i])] -= 1
for i in range(len(string)):
ans[i] = output[i]
return ans
bubbleSort(a)
print ("\nBubble Sorted array:")
for i in range(len(a)):
print ("%d \t" %a[i], sep=' ', end='', flush=True)
insertionSort(a)
print ("\nInsertion Sorted array:")
for i in range(len(a)):
print ("%d\t" %a[i], sep=' ', end='', flush=True)
heapSort(a)
n = len(a)
print ("\nHeap Sorted array:")
for i in range(n):
print ("%d\t" %a[i], sep=' ', end='', flush=True)
radixSort(a)
print ("\nRadix Sorted array:")
for i in range(len(a)):
print("%d\t" %a[i], sep=' ', end='', flush=True)
shellSort(a)
print ("\nShell Sorted array:")
for i in range(n):
print("%d\t" %a[i], sep=' ', end='', flush=True)
b = countSort(string)
print("\nSorted character array is %s" %(" ".join(b)))