*A sorted array is a great thing, as it is a base for numbers of efficient algorithms. However it may appear that you are given an array that is sorted, but not completely. Below you may find two interesting problems, that receive almost sorted array as an input.*

**Find an element in almost sorted array.**

To be more precise, in this case we consider an array, that was sorted, but some of its elements were swapped between adjacent cells. So the number lays in a desired position, or the distance to the desired position is equal 1. As an example let’s take a look at the array [1,4,2,5,7,6].

Obviously we could make an linear scan from left to right, and obtain the result in O(N) time. However we can go better. As we could expect, we gonna use binary search. Notice, that if we are looking for element E, it might be located in one of three cells. Let P(E) represents the position of E in a sorted array, then in our case we need to consider positions P(E) – 1, P(E), P(E) + 1. Position P(E) is going to be determined, by using regular binary search. Please find an example C++ implementation below:

int find(vector<int> array, int el){
if(!array.size())return -1;
int begin = 0, end = array.size() - 1;
while(begin < end){
int medium = b + (e - b) / 2;
// check three places instead of one
if(array == el)return medium;
else if(medium > begin && array == el)return medium - 1;
else if(medium < end && array == el)return medium + 1;
// choose which way to go
if(array < el)
begin = medium + 2; // as we checked medium + 1 already
else end = medium - 2; // as we checked medium - 1 already
}
}

**Sort almost sorted array.**

As you may remember, insertion sort has the best performance when the input array is almost sorted. It provides O(N * K) complexity, where K is the maximum distance between elements and they desired position. When K is small, then the performance would be very good, however when K grows near to N, that we are ending in O(N ^ 2) complexity. However if we know the K, we can proceed in a similar way to **smoothsort**. In the first step we would build a **min-heap** of K first elements from the array. Then we may scan the array from left to right and extract the minimum from the heap to insert it into the proper position into the array. We also need to remember, to update the heap with the next element. An example implementation could look like follows:

void sort(vector<int> & array, unsigned int k){
priority_queue<int, vector<int>, greater<int>>
heap(array.begin(), array.begin() + k);
for(unsigned int i = 0; i < array.size(); i++){
array[i] = heap.top(); heap.pop();
if(i + k < array.size())
heap.push(array[i + k]);
}
}

This solution performs better than insertion sort, as in this case, building the heap mainly adds the new element to the end of it. The same about the access and remove operation from the top of the heap. In the other words, the heap doesn’t require many rearrangements. It happens because we process almost sorted array, and only from time to time, we need to correct the heap structure. This leads to O(N * log K) algorithm, that behaves even better than insertion sort. However it requires extra memory O(K).

### Like this:

Like Loading...