Last active
May 4, 2021 14:52
-
-
Save Jkker/2e611829cc5fbdaddf071bf7252f3c74 to your computer and use it in GitHub Desktop.
Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubble_sort(A): | |
for iteration in range(len(A)): | |
for i in range(iteration): | |
if A[i] > A[i + 1]: | |
A[i], A[i + 1] = A[i + 1], A[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insertion_sort(arr): | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def quick_sort(L, U, A): | |
if L >= U: return | |
if A[L] < A[U]: | |
A[L], A[U] = A[U], A[L] | |
P = A[U] | |
B = L | |
T = U | |
while B < T: | |
A[B], A[T] = A[T], A[B] | |
B += 1 | |
T -= 1 | |
while A[B] < P: B += 1 | |
while A[T] > P: T -= 1 | |
A[L], A[T] = A[T], A[L] | |
quick_sort(L, T - 1, A) | |
quick_sort(T + 1, U, A) | |
def lomuto_quick_sort(L, U, Data): | |
if L >= U: return | |
pivot = Data[L] | |
l = L | |
for i in range(L + 1, U + 1): | |
if Data[i] < pivot: | |
Data[l + 1], Data[i] = Data[i], Data[l + 1] | |
l += 1 | |
Data[L], Data[l] = Data[l], Data[L] | |
lomuto_quick_sort(L, l - 1, Data) | |
lomuto_quick_sort(l + 1, U, Data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment