From d0b7968cb28b7263dd12af383780812068b8752e Mon Sep 17 00:00:00 2001 From: AryanGh-imp Date: Sun, 12 Apr 2026 03:00:46 +0330 Subject: [PATCH] feat: implement sorting algorithms in Python --- .gitignore | 2 ++ bubble_sort.py | 24 ++++++++++++++++++++++++ insertion_sort.py | 24 ++++++++++++++++++++++++ merge_sort.py | 42 ++++++++++++++++++++++++++++++++++++++++++ selection_sort.py | 22 ++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 bubble_sort.py create mode 100644 insertion_sort.py create mode 100644 merge_sort.py create mode 100644 selection_sort.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85936a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# VS Code +.idea \ No newline at end of file diff --git a/bubble_sort.py b/bubble_sort.py new file mode 100644 index 0000000..205f9bf --- /dev/null +++ b/bubble_sort.py @@ -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) diff --git a/insertion_sort.py b/insertion_sort.py new file mode 100644 index 0000000..9f55ec6 --- /dev/null +++ b/insertion_sort.py @@ -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) + diff --git a/merge_sort.py b/merge_sort.py new file mode 100644 index 0000000..c3bc299 --- /dev/null +++ b/merge_sort.py @@ -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) \ No newline at end of file diff --git a/selection_sort.py b/selection_sort.py new file mode 100644 index 0000000..5e3caf1 --- /dev/null +++ b/selection_sort.py @@ -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)