Skip to content

Create bubblesort python file #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions sorting_algorithms.py
Original file line number Diff line number Diff line change
@@ -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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input Validation: None of the sorting functions validate their inputs. Consider adding basic validation to handle edge cases such as None inputs or non-list inputs:

if arr is None:
    raise ValueError("Input cannot be None")
if not isinstance(arr, list):
    raise TypeError("Input must be 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]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Organization: The demonstration code at the end of the file will execute whenever the module is imported, which could cause unintended side effects. Wrap this code in an if __name__ == "__main__": block to ensure it only runs when the script is executed directly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing Coverage: The test case uses only a very small array [6, 6, 2]. Consider adding more comprehensive test cases including empty arrays, already sorted arrays, reverse sorted arrays, arrays with all identical elements, and larger arrays to better demonstrate performance differences between algorithms.

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)