-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.py
150 lines (115 loc) · 3.61 KB
/
sorting.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
# Created by Elshad Karimov
# Copyright © 2021 AppMillers. All rights reserved.
import math
def bubbleSort(customList):
for i in range(len(customList) - 1):
for j in range(len(customList) - i - 1):
if customList[j] > customList[j + 1]:
customList[j], customList[j + 1] = customList[j + 1], customList[j]
print(customList)
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)
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
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
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
def mergeSort(customList, l, r):
if l < r:
m = (l + (r - 1)) // 2
mergeSort(customList, l, m)
mergeSort(customList, m + 1, r)
merge(customList, l, m, r)
return customList
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)
quickSort(customList, low, pi - 1)
quickSort(customList, pi + 1, high)
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, 7, 6, 5, 3, 4, 9, 8]
if __name__ == "__main__":
#print(bubbleSort(cList))
print(selectionSort(cList))