ProAlgos / ProAlgos-Cpp

C++ implementations of well-known (and some rare) algorithms, while following good software development practices
MIT License
507 stars 363 forks source link

Fix radix sort #431

Open faheel opened 2 years ago

faheel commented 2 years ago

Radix sort seems to be broken as pointed out in #430

pm100 commented 1 year ago

there are two issues here.

I can post the fix to the first one, this makes code like this work

vector<int> arr{ 1, 8, 5, 12, 3 };
radix_sort(arr, 1, true);
return 0;

but crashes with (negative array indexes get generated by get_digit)

vector<int> arr{ 1, 8, -5, 12, 3 };
radix_sort(arr, 1, true);
return 0;

the second one is tougher. Two choices

I have verified that if I change the test harness to only generate >=0 values it works fine

pm100 commented 1 year ago

one way to fix would be

I have coded this up, works fine