Skip to content

Commit

Permalink
Merge branch 'shree'
Browse files Browse the repository at this point in the history
  • Loading branch information
Shree7676 committed Aug 29, 2024
2 parents 13ee4f1 + e9dc4f4 commit efdecf8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
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

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

0 comments on commit efdecf8

Please sign in to comment.