Problem:
Given $S$ — a sorted array where some of the elements were randomly changed, assuming that you're provided with an $S_1$ — an array of indexes of changed elements. Design an algorithm that will sort the array in $O(n)$ time.
Additionally, we know that $\text{length of }S_1\le\log_2(\text{length of }S)$.
Example:
$S$ — [3, 5, 7, (20), 15 ,(0) ,16]
$S_1$ — [3, 5]
() - Changed elements
Question:
I have some troubles designing the algorithm. My first idea was to just use Count or Radix sort algorithm, but as the values which are changed can be random I've ruled out count sort as potential candidate.
The second idea is to:
Load the values based on indexes of $S_1$ into new array $S_x$.
Rewrite $S$ without the elements of indexes provided in $S_1$.
Sort $S_x$.
Use "Merge" procedure from Merge Sort on $S_x$ and $S$.
However that still will leave me with $O(n\log n)$ as I'll need to sort $S_x$ array. I'll appreciate any advice on the approach.