Inserting a number into a sorted array!
- by Jay
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:
1. Find the right index where I should be putting this element using binary search
2. Create space for this element, by moving all the elements from that index down.
3. Put this element there.
Is there any other better way?