I would like to write a piece of code to insert a number into a sorted array at the appropriate position (i.e. the array will still remain sorted after insertion)
My data structure does not allow duplication.
I plan to do something like this:
- Find the desired index where I should put this element using binary search
- Create space for this item by moving all items from this index down.
- Put this item there.
Is there any other better way?
source
share