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

Enhancement: Implement bubble sort #30

Closed
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
23 changes: 21 additions & 2 deletions src/listwiz/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,27 @@ def merge_sort(l):


def bubble_sort(l):
# We should provide bubble sort as well!
raise NotImplementedError
"""Bubble sort algorithm.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A bigger project might want more documentation, but this is great!

Parameters
----------
l : list
The list to sort

Returns
-------
sorted_list
"""
n = len(l)
if n <= 1:
return l

for i in range(n):
for j in range(n-1-i):
if l[j] > l[j+1]:
l[j], l[j+1] = l[j+1], l[j]

return l


def selection_sort(l):
Expand Down
11 changes: 9 additions & 2 deletions tests/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ 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]

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


def test_selection_sort():
Expand Down