@@ -1858,17 +1858,19 @@ def _count_sort_for_radix(array, exp, comp):
1858
1858
count = [0 ] * 10
1859
1859
1860
1860
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
1863
1864
1864
1865
for i in range (1 , 10 ):
1865
1866
count [i ] += count [i - 1 ]
1866
1867
1867
1868
i = n - 1
1868
1869
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
1872
1874
i -= 1
1873
1875
1874
1876
for i in range (n ):
@@ -1908,26 +1910,31 @@ def radix_sort(array, comp=lambda u, v: u <= v, **kwargs):
1908
1910
1909
1911
sub_array = []
1910
1912
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 )):
1913
1915
if array [i ] is not None :
1914
1916
sub_array .append (array [i ])
1915
- if array [i ]> max_val :
1917
+ if array [i ] > max_val :
1916
1918
max_val = array [i ]
1917
1919
else :
1918
- none_count += 1
1920
+ none_indices . append ( i ) # Track the index of None values
1919
1921
1922
+ # Perform counting sort on the sub_array (without None values)
1920
1923
exp = 1
1921
1924
while max_val // exp > 0 :
1922
1925
_count_sort_for_radix (sub_array , exp , comp )
1923
1926
exp *= 10
1924
1927
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
1928
1936
for i in range (start , end + 1 ):
1929
- array .append (sub_array [index ])
1930
- index += 1
1937
+ array [i ] = sub_array [i - start ]
1931
1938
1932
1939
if _check_type (array , (DynamicArray , _arrays .DynamicOneDimensionalArray )):
1933
1940
array ._modify (True )
0 commit comments