myarray = empty
n = 10000
range = 1000
loop 1 to n {
x = random number between 1 and range
if x not in myarray {
add x to myarray
sort myarray
do something
}
}
I considered insertion sort, but that would require elements to shift. And quicksort would be bad on already sorted lists. The best I could think of now is a Min Heap. Is there some lesser known sorting algorithm that’s better for this situation? Is it in C++’s STL?
You’re looking for
std::set. It keeps things sorted as you insert, and gives you a quick “not in” operation.