This is a trivial algorithmic question, I believe, but I don’t seem to be able to find an efficient and elegant solution.
We have 3 arrays of int (Aa, Ab, Ac) and 3 cursors (Ca, Cb, Cc) that indicate an index in the corresponding array. I want to identify and increment the cursor pointing to the smallest value. If this cursor is already at the end of the array, I will exclude it and increment the cursor pointing to the second smallest value. If there is only 1 cursor that is not at the end of the array, we increment this one.
The only solutions I can come up are complicated and/or not optimal. For example, I always end up with a huge if…else…
Does anyone see a neat solution to this problem ?
I am programming in C++ but feel free to discuss it in pseudo-code or any language you like.
Thank you
Pseudo-java code:
Essentially, it does the following:
initialize an array
valueswithaa[ca],ab[cb]andac[cc]sort
valuesscan
valuesand increment if possible (i.e. not already at the end of the array) the index of the corresponding valueI know, sorting is at best O(n lg n), but I’m only sorting an array of 3 elements.