Skip to content

Commit 0e11cf4

Browse files
committed
resolved test errors for radix sort and shell sort
1 parent 1e72409 commit 0e11cf4

File tree

2 files changed

+19
-58
lines changed

2 files changed

+19
-58
lines changed

pydatastructs/linear_data_structures/algorithms.py

+17-20
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ def partition(array, lower, upper):
18531853

18541854
return array
18551855

1856-
def shell_sort(array, **kwargs):
1856+
def shell_sort(array, *args, **kwargs):
18571857
"""
18581858
Implements shell sort algorithm.
18591859
@@ -1906,9 +1906,10 @@ def shell_sort(array, **kwargs):
19061906
19071907
.. [1] https://en.wikipedia.org/wiki/Shellsort
19081908
"""
1909-
start = kwargs.get('start', 0)
1910-
end = kwargs.get('end', len(array) - 1)
1909+
start = int(kwargs.get('start', 0))
1910+
end = int(kwargs.get('end', len(array) - 1))
19111911
comp = kwargs.get('comp', lambda u, v: u <= v)
1912+
19121913
n = end - start + 1
19131914
gap = n // 2
19141915
while gap > 0:
@@ -1920,12 +1921,13 @@ def shell_sort(array, **kwargs):
19201921
j -= gap
19211922
array[j] = temp
19221923
gap //= 2
1924+
19231925
if _check_type(array, (DynamicArray, _arrays.DynamicOneDimensionalArray)):
19241926
array._modify(True)
19251927

19261928
return array
19271929

1928-
def radix_sort(array, **kwargs):
1930+
def radix_sort(array, *args, **kwargs):
19291931
"""
19301932
Implements radix sort algorithm for non-negative integers.
19311933
@@ -1977,36 +1979,31 @@ def radix_sort(array, **kwargs):
19771979
"""
19781980
start = int(kwargs.get('start', 0))
19791981
end = int(kwargs.get('end', len(array) - 1))
1980-
1981-
if start >= end:
1982-
return array
1982+
comp = kwargs.get('comp', lambda u, v: u <= v)
19831983

19841984
n = end - start + 1
1985-
if n <= 0:
1986-
return array
1987-
19881985
max_val = array[start]
19891986
for i in range(start + 1, end + 1):
1990-
if array[i] > max_val:
1987+
if array[i] is not None and array[i] > max_val:
19911988
max_val = array[i]
1992-
if max_val < 0:
1993-
raise ValueError("Radix sort requires non-negative integers")
1994-
19951989
exp = 1
19961990
while max_val // exp > 0:
19971991
count = [0] * 10
1998-
output = [0] * n
1992+
output = [None] * n
1993+
19991994
for i in range(start, end + 1):
2000-
digit = (array[i] // exp) % 10
2001-
count[digit] += 1
1995+
if array[i] is not None:
1996+
digit = (array[i] // exp) % 10
1997+
count[digit] += 1
20021998

20031999
for i in range(1, 10):
20042000
count[i] += count[i - 1]
20052001

20062002
for i in range(end, start - 1, -1):
2007-
digit = (array[i] // exp) % 10
2008-
count[digit] -= 1
2009-
output[count[digit]] = array[i]
2003+
if array[i] is not None:
2004+
digit = (array[i] // exp) % 10
2005+
count[digit] -= 1
2006+
output[count[digit]] = array[i]
20102007

20112008
for i in range(n):
20122009
array[start + i] = output[i]

pydatastructs/linear_data_structures/tests/test_algorithms.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -416,43 +416,7 @@ def test_jump_search():
416416
_test_common_search(jump_search, backend=Backend.CPP)
417417

418418
def test_shell_sort():
419-
assert shell_sort([]) == []
420-
421-
assert shell_sort([42]) == [42]
422-
423-
input_data = [1, 2, 3, 4, 5]
424-
expected = [1, 2, 3, 4, 5]
425-
assert shell_sort(input_data) == expected
426-
427-
input_data = [5, 4, 3, 2, 1]
428-
expected = [1, 2, 3, 4, 5]
429-
assert shell_sort(input_data) == expected
430-
431-
input_data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
432-
expected = [1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
433-
assert shell_sort(input_data) == expected
434-
435-
input_data = [-5, 3, -10, 7, 0, -2]
436-
expected = [-10, -5, -2, 0, 3, 7]
437-
assert shell_sort(input_data) == expected
419+
_test_common_sort(shell_sort)
438420

439421
def test_radix_sort():
440-
assert radix_sort([]) == []
441-
442-
assert radix_sort([42]) == [42]
443-
444-
input_data = [1, 2, 3, 4, 5]
445-
expected = [1, 2, 3, 4, 5]
446-
assert radix_sort(input_data) == expected
447-
448-
input_data = [5, 4, 3, 2, 1]
449-
expected = [1, 2, 3, 4, 5]
450-
assert radix_sort(input_data) == expected
451-
452-
input_data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
453-
expected = [1, 1, 2, 3, 3, 4, 5, 5, 6, 9]
454-
assert radix_sort(input_data) == expected
455-
456-
input_data = [1000, 10, 100, 1, 10000]
457-
expected = [1, 10, 100, 1000, 10000]
458-
assert radix_sort(input_data) == expected
422+
_test_common_sort(radix_sort)

0 commit comments

Comments
 (0)