Skip to content

Adding Radix Sort #603 #619

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 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
2 changes: 2 additions & 0 deletions oryx-build-commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PlatformWithVersion=Python
BuildCommands=conda env create --file environment.yml --prefix ./venv --quiet
71 changes: 71 additions & 0 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,77 @@ def partition(array, lower, upper):

return array

def _count_sort_for_radix(array, exp):
n = len(array)
output = [None] * n
count = [0] * 10

for i in range(n):
if array[i] is not None:
index = (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 array[i] is not None:
index = (array[i] // exp) % 10
output[count[index] - 1] = array[i]
count[index] -= 1
i -= 1

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

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 error if not Python backend
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)

# Create a sub_array excluding None values, and track positions of None values
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) # Track positions of None

# Perform counting sort on the sub_array (without None values)
exp = 1
while max_val // exp > 0:
_count_sort_for_radix(sub_array, exp)
exp *= 10

# Insert None values back at their respective positions
sorted_array = sub_array[:]
for idx in none_indices:
sorted_array.insert(end, None) # Insert None back at the correct position

# Update the original array with the sorted values
for i in range(start, end + 1):
array[i] = sorted_array[i - start]
=======
def shell_sort(array, *args, **kwargs):
"""
Implements shell sort algorithm.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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, radix_sort, Backend)
selection_sort, insertion_sort, intro_sort, shell_sort, radix_sort, Backend)

from pydatastructs.utils.raises_util import raises
Expand Down
Loading