feat: implement sorting algorithms in Python

This commit is contained in:
2026-04-12 03:00:46 +03:30
commit d0b7968cb2
5 changed files with 114 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
def insertion_sort(arr):
# starting from second index
for i in range (1, len(arr)):
key = arr[i] # The element we want to insert
j = i - 1 # Last house of sorted section
# Shift it right until the left member is greater than key
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
# Finally, put the key in the right place
arr[j+1] = key
# Example:
nums = [5, 2, 4, 6, 1, 3]
print("Before sorting:", nums)
insertion_sort(nums)
print("After sorting:", nums)