-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathall sorting methods.py
176 lines (135 loc) · 4.35 KB
/
all sorting methods.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# BUBBLE SORT
def bubblesort(customList):
for i in range(len(customList)-1): #O(n)
for j in range(len(customList)-i-1): #O(n)
if customList[j]>customList[j+1]: #O(1)
customList[j],customList[j+1]=customList[j+1],customList[j]
print(customList)
# cList=[2,1,3,6,9,7,4,8,5]
# bubblesort(cList)
# SELECTION SORT
def selectionSort(customList):
for i in range(len(customList)):
min_index = i
for j in range(i+1,len(customList)):
if customList[min_index] > customList[j]:
min_index = j
customList[i],customList[min_index] = customList[min_index],customList[i]
print(customList)
# cList=[2,1,3,6,9,7,4,8,5]
# selectionSort(cList)
# INSERTION SORT
def insertionSort(customList):
for i in range(1,len(customList)):
key=customList[i]
j=i-1
while j>=0 and key < customList[j]:
customList[j+1] = customList[j]
j -= 1
customList[j+1] = key
return customList
# cList=[2,1,3,6,9,7,4,8,5]
# print(insertionSort(cList))
# BUCKET SORT
import math
from xmlrpc.server import MultiPathXMLRPCServer
def bucketSort(customList):
numberofBuckets = round(math.sqrt(len(customList)))
maxValue = max(customList)
arr =[]
for i in range(numberofBuckets):
arr.append([])
for j in customList:
index_b = math.ceil(j*numberofBuckets/maxValue)
arr[index_b-1].append(j)
for i in range(numberofBuckets):
arr[i] = insertionSort(arr[i])
k = 0
for i in range(numberofBuckets):
for j in range(len(arr[i])):
customList[k] = arr[i][j]
k+=1
return customList
# cList=[2,1,3,6,9,7,4,8,5]
# print(bucketSort(cList))
# MERGE SORT ALGORITHM
def merge(customList, l, m, r):
n1 = m - l + 1
n2 = r - m
L =[0]*(n1)
R =[0]*(n2)
for i in range(0,n1):
L[i] = customList[l+i]
for j in range(0,n2):
R[j] = customList[m+1+j]
i = 0
j = 0
k = l
while i < n1 and j <n2:
if L[i] <=R[j]:
customList[k] = L[i]
i += 1
else:
customList[k] = R[j]
j += 1
k += 1
while i < n1:
customList[k] = L[i]
i += 1
k += 1
while j < n2:
customList[k] = R[j]
j += 1
k += 1
# TIME COMPLEXITY : O(N)
def mergeSort(customList, l, r):
if l < r:
m = (l+(r-1))//2
mergeSort(customList, l, m) #T(n/2)
mergeSort(customList, m+1, r) #T(n/2)
merge(customList, l, m, r)
return customList
# cList=[2,1,3,6,9,7,4,8,5]
# print(mergeSort(cList ,0 ,8))
# QUICK SORT
def partition(customList,low,high):
i = low - 1
pivot = customList[high]
for j in range(low,high):
if customList[j] <= pivot:
i+=1
customList[i],customList[j] = customList[j],customList[i]
customList[i+1],customList[high] = customList[high] , customList[i+1]
return (i+1)
def quickSort(customList ,low ,high):
if low < high:
pi = partition(customList ,low ,high) #O(n)
quickSort(customList ,low ,pi-1) #T(n/2)
quickSort(customList ,pi+1 ,high) #T(n/2)
#combined = O(N LogN)
# cList=[2,1,3,6,9,7,4,8,5]
# quickSort(cList ,0 ,8)
# print(cList)
# HEAP SORT ALGORITHM
def heapify(customList,n,i):
smallest = i
l = 2*i + 1
r = 2*i + 2
if l < n and customList[l] < customList[smallest]:
smallest = l
if r < n and customList[r] < customList[smallest]:
smallest=r
if smallest != i:
customList[i], customList[smallest] = customList[smallest], customList[i]
heapify(customList ,n ,smallest)
def heapSort(customList):
n = len(customList)
for i in range(int(n/2)-1,-1,-1):
heapify(customList,n,i)
for i in range(n-1,0,-1):
customList[i],customList[0]=customList[0],customList[i]
heapify(customList,i,0)
customList.reverse()
cList=[2,1,3,6,9,7,4,8,5]
heapSort(cList)
print(cList)