-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_library.py
More file actions
219 lines (182 loc) · 5.9 KB
/
Copy pathalgorithm_library.py
File metadata and controls
219 lines (182 loc) · 5.9 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import math
from typing import List
# ==========================================
# SORTING ALGORITHMS
# ==========================================
def bubble_sort(arr: List[int]) -> List[int]:
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
def selection_sort(arr: List[int]) -> List[int]:
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[min_idx] > arr[j]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def insertion_sort(arr: List[int]) -> List[int]:
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def merge_sort(arr: List[int]) -> List[int]:
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
return arr
def quick_sort(arr: List[int]) -> List[int]:
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]
sorted_arr = quick_sort(left) + middle + quick_sort(right)
for i in range(len(arr)):
arr[i] = sorted_arr[i]
return arr
def counting_sort(arr: List[int]) -> List[int]:
if not arr: return arr
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
i = 0
for a in range(max_val + 1):
for _ in range(count[a]):
arr[i] = a
i += 1
return arr
def radix_sort(arr: List[int]) -> List[int]:
if not arr: return arr
max_val = max(arr)
exp = 1
while max_val // exp > 0:
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
for i in range(n):
arr[i] = output[i]
exp *= 10
return arr
# ==========================================
# SEARCHING ALGORITHMS
# ==========================================
def linear_search(arr: List[int], target: int) -> int:
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
def binary_search(arr: List[int], target: int) -> int:
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
def jump_search(arr: List[int], target: int) -> int:
n = len(arr)
step = int(math.sqrt(n))
prev = 0
while prev < n and arr[min(step, n) - 1] < target:
prev = step
step += int(math.sqrt(n))
if prev >= n:
return -1
while prev < min(step, n) and arr[prev] <= target:
if arr[prev] == target:
return prev
prev += 1
return -1
def fibonacci_search(arr: List[int], target: int) -> int:
n = len(arr)
if n == 0:
return -1
fibMMm2 = 0
fibMMm1 = 1
fibM = fibMMm2 + fibMMm1
while fibM < n:
fibMMm2 = fibMMm1
fibMMm1 = fibM
fibM = fibMMm2 + fibMMm1
offset = -1
while fibM > 1:
i = min(offset + fibMMm2, n - 1)
if arr[i] < target:
fibM = fibMMm1
fibMMm1 = fibMMm2
fibMMm2 = fibM - fibMMm1
offset = i
elif arr[i] > target:
fibM = fibMMm2
fibMMm1 = fibMMm1 - fibMMm2
fibMMm2 = fibM - fibMMm1
else:
return i
if fibMMm1 and offset < n - 1 and arr[offset + 1] == target:
return offset + 1
return -1
# ==========================================
# COMPLEXITY METADATA
# ==========================================
COMPLEXITIES = {
"Bubble Sort": {"best": "O(n)", "avg": "O(n^2)", "worst": "O(n^2)", "space": "O(1)"},
"Selection Sort": {"best": "O(n^2)", "avg": "O(n^2)", "worst": "O(n^2)", "space": "O(1)"},
"Insertion Sort": {"best": "O(n)", "avg": "O(n^2)", "worst": "O(n^2)", "space": "O(1)"},
"Merge Sort": {"best": "O(n log n)", "avg": "O(n log n)", "worst": "O(n log n)", "space": "O(n)"},
"Quick Sort": {"best": "O(n log n)", "avg": "O(n log n)", "worst": "O(n^2)", "space": "O(log n)"},
"Counting Sort": {"best": "O(n+k)", "avg": "O(n+k)", "worst": "O(n+k)", "space": "O(n+k)"},
"Radix Sort": {"best": "O(nk)", "avg": "O(nk)", "worst": "O(nk)", "space": "O(n+k)"},
"Linear Search": {"best": "O(1)", "avg": "O(n)", "worst": "O(n)", "space": "O(1)"},
"Binary Search": {"best": "O(1)", "avg": "O(log n)", "worst": "O(log n)", "space": "O(1)"},
"Jump Search": {"best": "O(1)", "avg": "O(√n)", "worst": "O(√n)", "space": "O(1)"},
"Fibonacci Search": {"best": "O(1)", "avg": "O(log n)", "worst": "O(log n)", "space": "O(1)"}
}