I want to generate some test data to test a function that merges 'k sorted' lists (lists where each element is at most k positions away from it's correct sorted position) into a single fully sorted list. I have an approach that works but I'm not sure how well randomized it is and I feel there should be a simpler / more elegant way to do this. My current approach:
Like I say, this works but I'm interested in alternative / better approaches.
I think you could just fill an array with random integers and then run quicksort on it with a custom stopping condition.
If in a particular quicksort recursion your start
and end
indexes are less than k
apart, then just return instead of continuing to recur.
Because of how quicksort works, every number in the start..end
interval belongs somewhere in that region; worst case is that array[start]
might really belong at array[end]
(or vice versa) in truly sorted order. So, assuring that start
and end
are no more than k
apart should be sufficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With