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

bubble sort #26

Closed
wants to merge 3 commits into from
Closed
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):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would need some documentation of course. At least for public facing functions.

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

Empty file removed tests/__init__.py
Empty file.
36 changes: 34 additions & 2 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,40 @@ def test_mergesort_empty():


def test_bubble_sort():
# Stub for basic bubble sort tests, see issue #9
pass
# Test even-sized list:
l = [3, 2, 1, 5, 4, 6]
res = lws.bubble_sort(l)
assert res == [1, 2, 3, 4, 5, 6]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This list of tests is good. One way one can write it more compact is for example pytest.mark.parametrize().
That way the input and expected results are parameter, and the repetitive code is a bit less (not much for the simple test). It also effectively splits it in multiple tests.


# Test odd-sized list:
l = [5, 4, 3, 2, 1]
res = lws.bubble_sort(l)
assert res == [1, 2, 3, 4, 5]

# Test already sorted list:
l = [1, 2, 3, 4, 5]
res = lws.bubble_sort(l)
assert res == [1, 2, 3, 4, 5]

# Test list with duplicate elements:
l = [4, 2, 3, 2, 1, 3]
res = lws.bubble_sort(l)
assert res == [1, 2, 2, 3, 3, 4]

# Test list with all identical elements:
l = [7, 7, 7, 7]
res = lws.bubble_sort(l)
assert res == [7, 7, 7, 7]

# Test empty list:
l = []
res = lws.bubble_sort(l)
assert res == []

# Test single-element list:
l = [1]
res = lws.bubble_sort(l)
assert res == [1]


def test_selection_sort():
Expand Down