Skip to content

Instantly share code, notes, and snippets.

@BelmuTM
Last active May 30, 2024 21:26
Show Gist options
  • Save BelmuTM/639634506468a93fa0c9a0054502d36b to your computer and use it in GitHub Desktop.
Save BelmuTM/639634506468a93fa0c9a0054502d36b to your computer and use it in GitHub Desktop.
Simple Python Sorting Algorithms
def quick_sort(s: list, start: int, end: int):
if start < end:
pos = start
for i in range(start, end):
if s[i] < s[end]:
s[i], s[pos] = s[pos], s[i]
pos += 1
s[pos], s[end] = s[end], s[pos]
quick_sort(s, start, pos - 1)
quick_sort(s, pos + 1, end)
return s
def merge_sort(s: list):
if len(s) <= 1:
return s
else:
half = len(s) // 2
s[:half], s[half:] = merge_sort(s[:half]), merge_sort(s[half:])
result = []
i, j = 0, 0
while i < len(s[:half]) and j < len(s[half:]):
result.append(s[:half][i] if s[:half][i] < s[half:][j] else s[half:][j])
i, j = (i + 1, j) if s[:half][i] < s[half:][j] else (i, j + 1)
result.extend(s[:half][i:])
result.extend(s[half:][j:])
return result
def minimum(s: list):
min_idx = 0
for i in range(len(s)):
if s[i] < s[min_idx]:
min_idx = i
return min_idx
def select_sort(s: list):
for i in range(0, len(s)):
idx = i + minimum(s[i:])
s[idx], s[i] = s[i], s[idx]
return s
def insertion_sort(s: list):
for i in range(1, len(s)):
next_val = s[i]
j = i - 1
while j >= 0 and s[j] > next_val:
s[j + 1] = s[j]
j -= 1
s[j + 1] = next_val
return s
def bubble_sort(s: list):
for i in range(len(s), 0, -1):
for j in range(len(s[:i]) - 1):
if s[j] > s[j + 1]:
s[j], s[j + 1] = s[j + 1], s[j]
return s
foo = [-3, -9, -18, -2, 21, -5, 7, 14, 3, 0, 16]
# print(merge_sort(foo))
# print(select_sort(foo))
# print(insertion_sort(foo))
# print(bubble_sort(foo))
# print(quick_sort(foo, 0, len(foo) - 1))
@MarioError
Copy link

Nan c'est du bon je recommande, en plus il vend de bon maccaron mach'Allah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment