I would like to write a piece of code for inserting a number into a sorted array at the appropriate position (i.e. the array should still remain sorted after insertion)
My data structure doesn't allow duplicates.
I am planning to do something like this:
- Find the right index where I should be putting this element using binary search
- Create space for this element, by moving all the elements from that index down.
- Put this element there.
Is there any other better way?