Skip to content

feat: Add Radix Sort Algorithm #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ Algorithms

.. autofunction:: pydatastructs.jump_search

.. autofunction:: pydatastructs.intro_sort
.. autofunction:: pydatastructs.intro_sort

.. autofunction:: pydatastructs.radix_sort
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
jump_search,
selection_sort,
insertion_sort,
intro_sort
intro_sort,
radix_sort
)
__all__.extend(algorithms.__all__)
73 changes: 72 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
'jump_search',
'selection_sort',
'insertion_sort',
'intro_sort'
'intro_sort',
'radix_sort',
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -1850,3 +1851,73 @@ def partition(array, lower, upper):
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)

return array

def radix_sort(array, **kwargs):
"""
Implements Radix Sort.

Parameters
==========

array: Array
The array which is to be sorted.
comp: lambda/function
The comparator which is to be used
for sorting. Optional, by default, less than or
equal to is used for comparing two
values.
"""
raise_if_backend_is_not_python(radix_sort, kwargs.get('backend', Backend.PYTHON))

start = kwargs.get('start', 0)
end = kwargs.get('end', len(array) - 1)

sub_array = []
none_indices = []
max_val = 0

for i in range(start, end + 1):
if array[i] is not None:
sub_array.append(array[i])
max_val = max(max_val, array[i])
else:
none_indices.append(i)

exp = 1
while max_val // exp > 0:
n = len(sub_array)
output = [None] * n
count = [0] * 10

for i in range(n):
if sub_array[i] is not None:
index = (sub_array[i] // exp) % 10
count[index] += 1

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

i = n - 1
while i >= 0:
if sub_array[i] is not None:
index = (sub_array[i] // exp) % 10
output[count[index] - 1] = sub_array[i]
count[index] -= 1
i -= 1

for i in range(n):
sub_array[i] = output[i]

exp *= 10

sorted_array = sub_array[:]
for idx in none_indices:
sorted_array.insert(end, None)

for i in range(start, end + 1):
array[i] = sorted_array[i - start]

if _check_type(array, (DynamicArray, _arrays.DynamicOneDimensionalArray)):
array._modify(True)

return array
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
selection_sort, insertion_sort, intro_sort, Backend)
selection_sort, insertion_sort, intro_sort, radix_sort, Backend)

from pydatastructs.utils.raises_util import raises
import random
Expand Down Expand Up @@ -94,6 +94,9 @@ def test_heapsort():
def test_bucket_sort():
_test_common_sort(bucket_sort)

def test_radix_sort():
_test_common_sort(radix_sort)

def test_counting_sort():
random.seed(1000)

Expand Down
Loading