diff --git a/lib/std/sort/countingsort.c3 b/lib/std/sort/countingsort.c3 index 19c2836fd..864cb9276 100644 --- a/lib/std/sort/countingsort.c3 +++ b/lib/std/sort/countingsort.c3 @@ -26,8 +26,8 @@ macro quicksort_indexed(list, start, end, cmp = EMPTY_MACRO_SLOT, context = EMPT module std::sort::cs(); -def Counts = ulong[256] @private; -def Ranges = ulong[257] @private; +def Counts = usz[256] @private; +def Ranges = usz[257] @private; def Indexs = char[256] @private; def ElementType = $typeof(Type{}[0]); @@ -107,11 +107,14 @@ fn void csort(Type list, usz low, usz high, KeyFn key_fn, uint byte_idx) total += count; } ranges[256] = total; - - for(ushort i = 0; i < 256; i++) { + + ushort remaining_indexs = 256 - (fallback0_count + recursion_count); + for(ushort i = 0; (i < 256) && remaining_indexs; i++) { indexs[fallback0_count + fallback1_count] = (char)i; - ushort count = ranges[i + 1] - ranges[i]; - fallback1_count += (ushort)(count > 32 && count <= 128); + usz count = ranges[i + 1] - ranges[i]; + ushort within_fallback1_range = (ushort)(count > 32 && count <= 128); + fallback1_count += within_fallback1_range; + remaining_indexs -= within_fallback1_range; } if (!keys_ordered)