diff --git a/src/listwiz/sorting.py b/src/listwiz/sorting.py index 0d379f8..a24744d 100644 --- a/src/listwiz/sorting.py +++ b/src/listwiz/sorting.py @@ -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 \ No newline at end of file + raise NotImplementedError + diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_sorting.py b/tests/test_sorting.py index f7d41d1..975c572 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -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():