From dc674a14abff01545be76eefc365a61c82824562 Mon Sep 17 00:00:00 2001 From: Vishwam Talnikar <131583570+visz11@users.noreply.github.com> Date: Tue, 15 Jul 2025 15:05:30 +0530 Subject: [PATCH 1/2] Create bubblesort.py --- bubblesort.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bubblesort.py diff --git a/bubblesort.py b/bubblesort.py new file mode 100644 index 0000000..67a9f4e --- /dev/null +++ b/bubblesort.py @@ -0,0 +1,32 @@ +def bubble_sort(arr): + + # Outer loop to iterate through the list n times + for n in range(len(arr) - 1, 0, -1): + + # Initialize swapped to track if any swaps occur + swapped = False + + # Inner loop to compare adjacent elements + for i in range(n): + if arr[i] > arr[i + 1]: + + # Swap elements if they are in the wrong order + arr[i], arr[i + 1] = arr[i + 1], arr[i] + + # Mark that a swap has occurred + swapped = True + + # If no swaps occurred, the list is already sorted + if not swapped: + break + + +# Sample list to be sorted +arr = [6,6,2] +print("Unsorted list is:") +print(arr) + +bubble_sort(arr) + +print("Sorted list is:") +print(arr) From 5b3d6c3a4f4333c2179822d3f90610efc9a650de Mon Sep 17 00:00:00 2001 From: Arvind Shelke Date: Tue, 15 Jul 2025 16:19:39 +0530 Subject: [PATCH 2/2] Update and rename bubblesort.py to sorting_algorithms.py --- bubblesort.py | 32 ---------------- sorting_algorithms.py | 88 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 32 deletions(-) delete mode 100644 bubblesort.py create mode 100644 sorting_algorithms.py diff --git a/bubblesort.py b/bubblesort.py deleted file mode 100644 index 67a9f4e..0000000 --- a/bubblesort.py +++ /dev/null @@ -1,32 +0,0 @@ -def bubble_sort(arr): - - # Outer loop to iterate through the list n times - for n in range(len(arr) - 1, 0, -1): - - # Initialize swapped to track if any swaps occur - swapped = False - - # Inner loop to compare adjacent elements - for i in range(n): - if arr[i] > arr[i + 1]: - - # Swap elements if they are in the wrong order - arr[i], arr[i + 1] = arr[i + 1], arr[i] - - # Mark that a swap has occurred - swapped = True - - # If no swaps occurred, the list is already sorted - if not swapped: - break - - -# Sample list to be sorted -arr = [6,6,2] -print("Unsorted list is:") -print(arr) - -bubble_sort(arr) - -print("Sorted list is:") -print(arr) diff --git a/sorting_algorithms.py b/sorting_algorithms.py new file mode 100644 index 0000000..de944a5 --- /dev/null +++ b/sorting_algorithms.py @@ -0,0 +1,88 @@ +""" +Sorting Algorithms Comparison Script + +Includes: +- Bubble Sort +- Selection Sort +- Insertion Sort +- Native Python sort() and sorted() +""" + +def bubble_sort(arr): + """ + Performs in-place Bubble Sort on a list. + Stops early if no swaps occur in a pass. + """ + n = len(arr) + for end in range(n - 1, 0, -1): + swapped = False + for i in range(end): + if arr[i] > arr[i + 1]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + swapped = True + if not swapped: + break + + +def selection_sort(arr): + """ + Performs in-place Selection Sort. + Repeatedly selects the minimum element and puts it in place. + """ + n = len(arr) + for i in range(n): + min_idx = i + for j in range(i + 1, n): + if arr[j] < arr[min_idx]: + min_idx = j + arr[i], arr[min_idx] = arr[min_idx], arr[i] + + +def insertion_sort(arr): + """ + Performs in-place Insertion Sort. + Builds the sorted array one item at a time. + """ + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and arr[j] > key: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + +# Sample input +original = [6, 6, 2] + +print("Original list:") +print(original) + +# Bubble Sort +arr1 = original.copy() +bubble_sort(arr1) +print("\nBubble Sort result:") +print(arr1) + +# Selection Sort +arr2 = original.copy() +selection_sort(arr2) +print("\nSelection Sort result:") +print(arr2) + +# Insertion Sort +arr3 = original.copy() +insertion_sort(arr3) +print("\nInsertion Sort result:") +print(arr3) + +# Python built-in sort (in-place) +arr4 = original.copy() +arr4.sort() +print("\nPython .sort() result:") +print(arr4) + +# Python built-in sorted() (returns new list) +arr5 = sorted(original) +print("\nPython sorted() result (non-in-place):") +print(arr5)