Skip to content
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

added bubble sort function and its test case #31

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions src/listwiz/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ def merge_sort(l):
return res


def bubble_sort(l):
# We should provide bubble sort as well!
raise NotImplementedError
def bubble_sort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already sorted
for j in range(0, n - i - 1):
# Traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr


def selection_sort(l):
# We should provide selection sort.
raise NotImplementedError
raise NotImplementedError

Copy link
Collaborator

Choose a reason for hiding this comment

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

Some projects might not like such changes (even if they are better). But I think most/may either auto-format anyway, or will ignore smaller things like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it
Thanks

Empty file removed tests/__init__.py
Empty file.
18 changes: 15 additions & 3 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ def test_mergesort_empty():
pass


def test_bubble_sort():
# Stub for basic bubble sort tests, see issue #9
pass
@pytest.mark.parametrize(
"input_list, expected",
[
([3, 2, 1, 5, 4, 6], [1, 2, 3, 4, 5, 6]), # Test even-sized list
([5, 4, 3, 2, 1], [1, 2, 3, 4, 5]), # Test odd-sized list
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), # Test already sorted list
([4, 2, 3, 2, 1, 3], [1, 2, 2, 3, 3, 4]), # Test list with duplicate elements
([7, 7, 7, 7], [7, 7, 7, 7]), # Test list with all identical elements
([], []), # Test empty list
([1], [1]), # Test single-element list
],
)
def test_bubble_sort(input_list, expected):
res = lws.bubble_sort(input_list)
assert res == expected


def test_selection_sort():
Expand Down