feat: implement sorting algorithms in Python
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
# VS Code
|
||||||
|
.idea
|
||||||
@@ -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)
|
||||||
@@ -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)
|
||||||
|
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
def merge_sort(arr):
|
||||||
|
# If the array length is less than 2, no sorting is needed
|
||||||
|
if len(arr) <= 1:
|
||||||
|
return arr
|
||||||
|
|
||||||
|
# Find the middle of the array
|
||||||
|
mid = len(arr) // 2
|
||||||
|
|
||||||
|
# Divide the array into two halves
|
||||||
|
left_half = merge_sort(arr[:mid])
|
||||||
|
right_half = merge_sort(arr[mid:])
|
||||||
|
|
||||||
|
# Merge two sorted halves
|
||||||
|
return merge(left_half, right_half)
|
||||||
|
|
||||||
|
|
||||||
|
def merge(left, right):
|
||||||
|
result = []
|
||||||
|
i = j = 0
|
||||||
|
|
||||||
|
# As long as there are elements left in both lists
|
||||||
|
while i < len(left) and j < len(right):
|
||||||
|
if left[i] <= right[j]:
|
||||||
|
result.append(left[i])
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
result.append(right[j])
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
# Add the remaining elements (one of the lists may run out early)
|
||||||
|
result.extend(left[i:])
|
||||||
|
result.extend(right[j:])
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# Example:
|
||||||
|
|
||||||
|
nums = [5, 2, 4, 6, 1, 3]
|
||||||
|
print("Before sorting:", nums)
|
||||||
|
|
||||||
|
nums = merge_sort(nums)
|
||||||
|
print("After sorting:", nums)
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
def selection_sort(arr):
|
||||||
|
n = len(arr)
|
||||||
|
for i in range(n - 1):
|
||||||
|
# Assume the current element is the smallest
|
||||||
|
min_index = i
|
||||||
|
|
||||||
|
# Checking the rest of the array to find the smallest one
|
||||||
|
for j in range(i + 1, n):
|
||||||
|
if arr[j] < arr[min_index]:
|
||||||
|
min_index = j # Update the index of the smallest number
|
||||||
|
|
||||||
|
# If the smallest number is not the first, replace
|
||||||
|
if min_index != i:
|
||||||
|
arr[i], arr[min_index] = arr[min_index], arr[i]
|
||||||
|
|
||||||
|
# Example:
|
||||||
|
|
||||||
|
nums = [5, 2, 4, 6, 1, 3]
|
||||||
|
print("Before sorting:", nums)
|
||||||
|
|
||||||
|
selection_sort(nums)
|
||||||
|
print("After sorting:", nums)
|
||||||
Reference in New Issue
Block a user