From 8289dcdc2b2abeb8316f1e393f673b9936886f80 Mon Sep 17 00:00:00 2001 From: atharvsabdeai Date: Wed, 23 Jul 2025 10:53:48 +0530 Subject: [PATCH 1/2] Update sorting_algorithms.py --- sorting_algorithms.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/sorting_algorithms.py b/sorting_algorithms.py index de944a5..5c275dc 100644 --- a/sorting_algorithms.py +++ b/sorting_algorithms.py @@ -1,12 +1,3 @@ -""" -Sorting Algorithms Comparison Script - -Includes: -- Bubble Sort -- Selection Sort -- Insertion Sort -- Native Python sort() and sorted() -""" def bubble_sort(arr): """ From 6d03bbeab057fd9fb4005f2deb89a8cbee644025 Mon Sep 17 00:00:00 2001 From: atharvsabdeai Date: Wed, 23 Jul 2025 10:59:04 +0530 Subject: [PATCH 2/2] Update sorting_algorithms.py --- sorting_algorithms.py | 56 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/sorting_algorithms.py b/sorting_algorithms.py index 5c275dc..60aa76e 100644 --- a/sorting_algorithms.py +++ b/sorting_algorithms.py @@ -1,4 +1,3 @@ - def bubble_sort(arr): """ Performs in-place Bubble Sort on a list. @@ -43,8 +42,45 @@ def insertion_sort(arr): arr[j + 1] = key +def merge_sort(arr): + """ + Performs Merge Sort (not in-place). + Recursively divides the list and merges sorted halves. + """ + if len(arr) <= 1: + return arr + + mid = len(arr) // 2 + left = merge_sort(arr[:mid]) + right = merge_sort(arr[mid:]) + + return merge(left, right) + + +def merge(left, right): + """ + Helper function to merge two sorted lists. + """ + result = [] + i = j = 0 + + # Merge the two halves + 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 + + # Append any remaining elements + result.extend(left[i:]) + result.extend(right[j:]) + return result + + # Sample input -original = [6, 6, 2] +original = [6, 6, 2, 3, 2] print("Original list:") print(original) @@ -67,13 +103,19 @@ def insertion_sort(arr): print("\nInsertion Sort result:") print(arr3) -# Python built-in sort (in-place) +# Merge Sort (not in-place) arr4 = original.copy() -arr4.sort() +arr4_sorted = merge_sort(arr4) +print("\nMerge Sort result:") +print(arr4_sorted) + +# Python built-in sort (in-place) +arr5 = original.copy() +arr5.sort() print("\nPython .sort() result:") -print(arr4) +print(arr5) # Python built-in sorted() (returns new list) -arr5 = sorted(original) +arr6 = sorted(original) print("\nPython sorted() result (non-in-place):") -print(arr5) +print(arr6)