-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_algorithm.py
More file actions
161 lines (126 loc) · 3.68 KB
/
sort_algorithm.py
File metadata and controls
161 lines (126 loc) · 3.68 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
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
def quick_sort(alist):
if len(alist) <= 1:
return alist
else:
base = alist[0]
i = 1
j = len(alist)-1
found_i = False
found_j = False
while i <= j:
if alist[i] < base:
i += 1
else:
found_i = True
if alist[j] > base:
j -= 1
else:
found_j = True
if found_i and found_j:
alist[i], alist[j] = alist[j], alist[i]
i += 1
j -= 1
found_i = False
found_j = False
alist[0], alist[j] = alist[j], alist[0]
left = quick_sort(alist[:j])
right = quick_sort(alist[i:])
return left + [alist[j]] + right
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
def partition(arr, low, high):
pivot = low
for i in range(low+1, high+1):
if arr[i] < arr[low]:
pivot += 1
arr[pivot], arr[i] = arr[i], arr[pivot]
arr[pivot], arr[low] = arr[low], arr[pivot]
return pivot
def quicksort_inplace(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quicksort_inplace(arr, low, pi-1)
quicksort_inplace(arr, pi+1, high)
def bubble_sort(alist):
for j in range(len(alist)-1, 0, -1):
for i in range(j):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
def short_bubble_sort(alist):
exchanges = True
n = len(alist)-1
while n > 0 and exchanges:
exchanges = False
for i in range(n):
if alist[i] > alist[i+1]:
exchanges = True
alist[i], alist[i+1] = alist[i+1], alist[i]
n -= 1
def select_sort(alist):
for j in range(len(alist)-1, 0, -1):
max_index = 0
for i in range(1, j+1):
if alist[i] > alist[max_index]:
max_index = i
alist[j], alist[max_index] = alist[max_index], alist[j]
def insert_sort(alist):
for i in range(1, len(alist)):
current_value = alist[i]
while i > 0 and alist[i-1] > current_value:
alist[i] = alist[i-1]
i -= 1
alist[i] = current_value
def gap_insert_sort(alist, start, gap):
for i in range(start+gap, len(alist), gap):
current_value = alist[i]
while i >= gap and alist[i-gap] > current_value:
alist[i] = alist[i-gap]
i -= gap
alist[i] = current_value
def shell_sort(alist):
gap = len(alist) // 2
while gap > 0:
for i in range(gap):
gap_insert_sort(alist, i, gap)
gap = gap // 2
def merge_sort(alist):
if len(alist) <= 1:
return alist
else:
s = len(alist) // 2
left = merge_sort(alist[:s])
right = merge_sort(alist[s:])
return merge(left, right)
def merge(l, r):
a = []
while len(l) > 0 and len(r) > 0:
if l[0] <= r[0]:
a.append(l[0])
l.pop(0)
else:
a.append(r[0])
r.pop(0)
for i in l:
a.append(i)
for i in r:
a.append(i)
return a
if __name__ == '__main__':
a = [3, 1, 1, 5, 9, 7, 11, 9]
b = [3, 6, 8, 19, 1, 5]
# aa = quick_sort(a)
# aa = quicksort(b)
# quicksort_inplace(a, 0, len(a)-1)
# short_bubble_sort(a)
# select_sort(a)
# insert_sort(a)
# shell_sort(a)
a = merge_sort(b)
print(a)