-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
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] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing Coverage: The test case uses only a very small array |
||
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) |
There was a problem hiding this comment.
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: