Skip to content

Commit 712fedb

Browse files
committed
feat: Add Radix Sort Algorithm
Added an implementation of the Radix Sort algorithm. Radix Sort is a non-comparative sorting algorithm that sorts numbers digit by digit, typically using Counting Sort as a subroutine. This implementation sorts numbers starting from the least significant digit (LSD).
1 parent f34b7d6 commit 712fedb

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

docs/source/pydatastructs/linear_data_structures/algorithms.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ Algorithms
4545

4646
.. autofunction:: pydatastructs.jump_search
4747

48-
.. autofunction:: pydatastructs.intro_sort
48+
.. autofunction:: pydatastructs.intro_sort
49+
50+
.. autofunction:: pydatastructs.radix_sort

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
jump_search,
4848
selection_sort,
4949
insertion_sort,
50-
intro_sort
50+
intro_sort,
51+
radix_sort
5152
)
5253
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
'jump_search',
3131
'selection_sort',
3232
'insertion_sort',
33-
'intro_sort'
33+
'intro_sort',
34+
'radix_sort',
3435
]
3536

3637
def _merge(array, sl, el, sr, er, end, comp):
@@ -1850,3 +1851,71 @@ def partition(array, lower, upper):
18501851
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)
18511852

18521853
return array
1854+
1855+
def radix_sort(array, **kwargs):
1856+
"""
1857+
Implements Radix Sort.
1858+
Parameters
1859+
==========
1860+
array: Array
1861+
The array which is to be sorted.
1862+
comp: lambda/function
1863+
The comparator which is to be used
1864+
for sorting. Optional, by default, less than or
1865+
equal to is used for comparing two
1866+
values.
1867+
"""
1868+
raise_if_backend_is_not_python(radix_sort, kwargs.get('backend', Backend.PYTHON))
1869+
1870+
start = kwargs.get('start', 0)
1871+
end = kwargs.get('end', len(array) - 1)
1872+
1873+
sub_array = []
1874+
none_indices = []
1875+
max_val = 0
1876+
1877+
for i in range(start, end + 1):
1878+
if array[i] is not None:
1879+
sub_array.append(array[i])
1880+
max_val = max(max_val, array[i])
1881+
else:
1882+
none_indices.append(i)
1883+
1884+
exp = 1
1885+
while max_val // exp > 0:
1886+
n = len(sub_array)
1887+
output = [None] * n
1888+
count = [0] * 10
1889+
1890+
for i in range(n):
1891+
if sub_array[i] is not None:
1892+
index = (sub_array[i] // exp) % 10
1893+
count[index] += 1
1894+
1895+
for i in range(1, 10):
1896+
count[i] += count[i - 1]
1897+
1898+
i = n - 1
1899+
while i >= 0:
1900+
if sub_array[i] is not None:
1901+
index = (sub_array[i] // exp) % 10
1902+
output[count[index] - 1] = sub_array[i]
1903+
count[index] -= 1
1904+
i -= 1
1905+
1906+
for i in range(n):
1907+
sub_array[i] = output[i]
1908+
1909+
exp *= 10
1910+
1911+
sorted_array = sub_array[:]
1912+
for idx in none_indices:
1913+
sorted_array.insert(end, None)
1914+
1915+
for i in range(start, end + 1):
1916+
array[i] = sorted_array[i - start]
1917+
1918+
if _check_type(array, (DynamicArray, _arrays.DynamicOneDimensionalArray)):
1919+
array._modify(True)
1920+
1921+
return array

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
66
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
77
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
8-
selection_sort, insertion_sort, intro_sort, Backend)
8+
selection_sort, insertion_sort, intro_sort, radix_sort, Backend)
99

1010
from pydatastructs.utils.raises_util import raises
1111
import random
@@ -94,6 +94,9 @@ def test_heapsort():
9494
def test_bucket_sort():
9595
_test_common_sort(bucket_sort)
9696

97+
def test_radix_sort():
98+
_test_common_sort(radix_sort)
99+
97100
def test_counting_sort():
98101
random.seed(1000)
99102

0 commit comments

Comments
 (0)