Answer
procedure binaryinsert$(x: $ Integer to be inserted; $a_1, a_2, a_3, ... a_n:$ A list of integers required to be sorted $)$
index:= binarysearchmodif$(x, a_1, a_2, a_3,...a_{n})$
{This would return the index of the first element which is greater than $x$}
if index is not equal to $n+1$
$\space\space\space$$\space\space\space$for $j:=$ index to $n$
$\space\space\space$$\space\space\space$$\space\space\space$$a_{j-1} := a_{j}$
$\space\space\space a_{index} := x$
else $a_{n+1}:=x$
procedure binarysearchmodif $(x:$ integer,$ a_1, a_2, . . . , a_n: $increasing integers$)$
$i := 1$ {i is left endpoint of search interval}
$j := n $ {j is right endpoint of search interval}
while $i < j$
$\space\space\space m := \left\lfloor \frac{(i + j)}{2}\right\rfloor$
$\space\space\space $if $x > a_m$ then $i := m + 1$
$\space\space\space $else $j := m$
if $a_i\le x$ then return $i+1$
else return $i$
Work Step by Step
This algorithm attempts to insert a value into a sorted list by using the binary search algorithm.
First we modify the binary search algorithm so it returns the index of the first element greater than $x$ . When doing this, we reach a point where we have one element. Depending on the original set of values, this element could either be less than, greater than, or the same as our target. We handle each case and return an appropriate index.
This approach is extended in exercise $\#49$ to implement a binary insertion sort.