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 bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
swapped = False # To detect whether the move has been made at this stage or not
# Comparing adjacent elements
for j in range(n - i - 1): # -i because the largest elements are fixed at the end
if arr[j] > arr[j + 1]:
# Swap
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# If no moves were made, the array was already sorted → Done!
if not swapped:
break
# Example:
nums = [5, 2, 4, 6, 1, 3]
print("Before sorting:", nums)
bubble_sort(nums)
print("After sorting:", nums)