From 6bc975ecc7f239c3e29ef88b7f78ff46c508c9d8 Mon Sep 17 00:00:00 2001 From: Shree7676 Date: Tue, 27 Aug 2024 16:52:45 +0200 Subject: [PATCH 1/3] bubble sort --- src/listwiz/sorting.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/listwiz/sorting.py b/src/listwiz/sorting.py index 0d379f8..0c978f3 100644 --- a/src/listwiz/sorting.py +++ b/src/listwiz/sorting.py @@ -46,11 +46,20 @@ 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] def selection_sort(l): # We should provide selection sort. - raise NotImplementedError \ No newline at end of file + raise NotImplementedError + From 4c7fea55531f614f068fdd0ccce70bcfdd7ea07f Mon Sep 17 00:00:00 2001 From: Shree7676 Date: Tue, 27 Aug 2024 17:43:05 +0200 Subject: [PATCH 2/3] added test case for bubble sort --- src/listwiz/sorting.py | 1 + tests/__init__.py | 0 tests/test_sorting.py | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) delete mode 100644 tests/__init__.py diff --git a/src/listwiz/sorting.py b/src/listwiz/sorting.py index 0c978f3..a24744d 100644 --- a/src/listwiz/sorting.py +++ b/src/listwiz/sorting.py @@ -57,6 +57,7 @@ def bubble_sort(arr): # 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): 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..332479e 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -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] + + # 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(): From e9dc4f43f3996467b6a55f47f50903e30d97c192 Mon Sep 17 00:00:00 2001 From: Shree7676 Date: Thu, 29 Aug 2024 13:09:31 +0200 Subject: [PATCH 3/3] updated the test case code using pytest.mark.parametrize() --- tests/test_sorting.py | 50 +++++++++++++------------------------------ 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/tests/test_sorting.py b/tests/test_sorting.py index 332479e..975c572 100644 --- a/tests/test_sorting.py +++ b/tests/test_sorting.py @@ -20,41 +20,21 @@ def test_mergesort_empty(): pass -def test_bubble_sort(): - # 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] - - # 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] +@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():