Skip to content

Commit a124066

Browse files
committed
Resolving error #603
1 parent 9253e29 commit a124066

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,17 +1858,19 @@ def _count_sort_for_radix(array, exp, comp):
18581858
count = [0] * 10
18591859

18601860
for i in range(n):
1861-
index = (array[i] // exp) % 10
1862-
count[index] += 1
1861+
if array[i] is not None:
1862+
index = (array[i] // exp) % 10
1863+
count[index] += 1
18631864

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

18671868
i = n - 1
18681869
while i >= 0:
1869-
index = (array[i] // exp) % 10
1870-
output[count[index] - 1] = array[i]
1871-
count[index] -= 1
1870+
if array[i] is not None:
1871+
index = (array[i] // exp) % 10
1872+
output[count[index] - 1] = array[i]
1873+
count[index] -= 1
18721874
i -= 1
18731875

18741876
for i in range(n):
@@ -1908,26 +1910,31 @@ def radix_sort(array, comp=lambda u, v: u <= v, **kwargs):
19081910

19091911
sub_array = []
19101912
max_val = 0
1911-
none_count = 0
1912-
for i in range(0,len(array)):
1913+
none_indices = [] # To track positions of None values
1914+
for i in range(0, len(array)):
19131915
if array[i] is not None:
19141916
sub_array.append(array[i])
1915-
if array[i]>max_val:
1917+
if array[i] > max_val:
19161918
max_val = array[i]
19171919
else:
1918-
none_count += 1
1920+
none_indices.append(i) # Track the index of None values
19191921

1922+
# Perform counting sort on the sub_array (without None values)
19201923
exp = 1
19211924
while max_val // exp > 0:
19221925
_count_sort_for_radix(sub_array, exp, comp)
19231926
exp *= 10
19241927

1925-
sub_array += [None] * none_count
1926-
index = 0
1927-
array = []
1928+
# Reintroduce None values at their original positions
1929+
for idx in none_indices:
1930+
sub_array.insert(idx, None)
1931+
1932+
# Slice sub_array to ensure we only modify the desired range (start to end)
1933+
sub_array = sub_array[start:end + 1]
1934+
1935+
# Ensure the final array is updated correctly with sorted values
19281936
for i in range(start, end + 1):
1929-
array.append(sub_array[index])
1930-
index += 1
1937+
array[i] = sub_array[i - start]
19311938

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

0 commit comments

Comments
 (0)