-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
154 lines (125 loc) · 3.69 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from random import randint
from collections import deque
import time
start = time.time()
arr = [13, 7, 5, 17, 9, 12, 0, 16, 10, 3, 1, 11, 6, 2, 4, 8, 15, 19, 14, 18]
arr_rand = []
for _ in range(100):
arr_rand.append(randint(1, 100))
result = []
# selection sort : O(n2)
def selection_sort(iterable):
arr = iterable
for i in range(len(arr)):
min_index = i
for j in range(1 + i, len(arr)):
if arr[min_index] > arr[j]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr
# insertion sort : worst O(n2), best O(n)
def insertion_sort(iterable):
arr = iterable
for i in range(1, len(arr)):
for j in range(i, 0, -1):
if arr[j] < arr[j - 1]:
arr[j], arr[j - 1] = arr[j - 1], arr[j]
else:
break
return arr
# quick sort : normal O(nlogn), worst O(n2)
def quick_sort(arr, start, end):
if start >= end:
return
pivot = start
left = start + 1
right = end
while left <= right:
while left <= end and arr[left] <= arr[pivot]:
left += 1
while right > start and arr[right] >= arr[pivot]:
right -= 1
if left > right:
arr[pivot], arr[right] = arr[right], arr[pivot]
else:
arr[left], arr[right] = arr[right], arr[left]
quick_sort(arr, start, right - 1)
quick_sort(arr, right + 1, end)
return arr
# quick sort(python ver)
def quick_sort_py(arr):
if len(arr) <= 1:
return arr
pivot = arr[0]
tail = arr[1:]
left_side = [x for x in tail if x <= pivot]
right_side = [x for x in tail if x > pivot]
return quick_sort_py(left_side) + [pivot] + quick_sort_py(right_side)
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr)//2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
i, j = 0, 0
result = []
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
while i < len(left):
result.append(left[i])
i += 1
while j < len(right):
result.append(right[j])
j += 1
return result
def heapify(arr, i, n):
l = i*2+1
r = l+1
idx = i
if l < n and arr[l] > arr[i]: idx = l
if r < n and arr[r] > arr[idx]: idx = r
if idx != i:
arr[i], arr[idx] = arr[idx], arr[i]
heapify(arr, idx, n)
def heap_sort(arr):
n = len(arr)
for i in range(n-1, -1, -1):
heapify(arr, i, n-1)
for i in range(n-1, -1, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, 0, i)
return arr
# counting sort : O(n + k) -> k = max(array)
def counting_sort(arr):
count = [0] * (max(arr) + 1)
for i in range(len(arr)):
count[arr[i]] += 1
for i in range(len(count)):
for j in range(count[i]):
result.append(i)
return result
# radix sort : O(d * (n + k)) -> d = 최대 자리수, k = 10
def radix_sort(arr):
qs = [deque() for _ in range(len(arr))]
max_v = max(arr)
q = deque(arr)
digit = 1
while max_v >= digit:
while q:
num = q.popleft()
qs[(num//digit)%10].append(num)
for queue in qs:
while queue:
q.append(queue.popleft())
digit *= 10
return list(q)
# print
result = merge_sort(arr_rand)
end = time.time()
print(result)
print(end - start)