-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.py
140 lines (92 loc) · 2.6 KB
/
sort.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
''' BUBBLE SORT '''
array = [1, 5, 8, 6, 9, 14, 16, 711, 5123, 125156]
for i in range(len(array)):
for j in range(len(array) - 1):
if array[i] < array[j]:
array[i], array[j] = array[j], array[i]
print(array)
''' BUBBLE SORT '''
''' INSERTION SORT '''
array = [1, 78, 8, 6, 9, 14, 16, 711, 3, 5123, 125156]
for i in range(1, len(array)):
temp = array[i]
j = i - 1
while array[j] > temp:
array[j + 1] = array[j]
j -= 1
array[j + 1] = temp
print(array)
''' INSERTION SORT '''
''' SELECTION SORT '''
array = [1, 78, 8, 6, 9, 14, 16, 711, 3, 5123, 125156]
for i in range(len(array)):
min_index = i
for j in range(i + 1, len(array)):
if array[j] < array[min_index]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
print(array)
''' SELECTION SORT '''
''' MERGE SORT '''
array = [1, 78, 8, 6, 9, 14, 16, 711, 3, 5123, 125156]
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
def sort(array):
if len(array) < 2:
return array
mid = len(array) // 2
left = sort(array[:mid])
right = sort(array[mid:])
return merge(left, right)
print(sort(array))
''' MERGE SORT '''
''' QUICK SORT '''
array = [1, 78, 8, 6, 9, 14, 16, 711, 3, 5123, 125156]
def quick(array):
if len(array) < 2:
return array
pivot = array[0]
left = [x for x in array[1:] if x <= pivot]
right = [x for x in array[1:] if x > pivot]
return quick(left) + [pivot] + quick(right)
''' QUICK SORT '''
''' HEAP SORT '''
array = [1, 78, 8, 6, 9, 14, 16, 711, 3, 5123, 125156]
def heapify(arr, n, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# One by one extract elements
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)
heapSort(array)
n = len(array)
print(array)
''' HEAP SORT '''
© 2022 GitHub, Inc.
Terms
Privacy
Security