Reputation: 1155
It is a coding interview question. We are given an array say random_arr
and we need to sort it using only the swap function.
Also the number of swaps for each element in random_arr
are limited. For this you are given an array parent_arr
, containing number of swaps for each element of random_arr
.
Constraints:
Now I will explain how parent_arr
is declared. If parent_arr
is like:
parent_arr[] = {a,b,c,d,...,z} then
a can be swapped at most one time.
b can be swapped at most two times.
if parent_arr[] = {c,b,a,....,z} then
c can be swapped at most one time.
b can be swapped at most two times.
a can be swapped at most three times
My solution:
For each element in random_arr[] store that how many elements are below it, if it is sorted. Now select element having minimum swap count from parent_arr[] and check whether it exist in random_arr[]. If yes and it if has occurred more than one time then it will have more than one location where it can be placed. Now choose the position(rather element at that position, preciously) with maximum swap count and swap it. Now decrease the swap count for that element and sort the parent_arr[] and repeat the process.
But it is quite inefficient and its correctness can't be proved. Any ideas?
Upvotes: 4
Views: 1159
Reputation: 726499
First, let's simplify your algorithm; then let's informally prove its correctness.
Observe that once you computed the number of elements below each number in the sorted sequence, you have enough information to determine for each group of equal elements x
their places in the sorted array. For example, if c
is repeated 7 times and has 21 elements ahead of it, then c
s will occupy the range [21..27]
(all indexes are zero-based; the range is inclusive of its ends).
Go through the parent_arr
in the order of increasing number of swaps. For each element x
, find the beginning of its target range rb
; also note the end of its target range re
. Now go through the elements of random_arr
outside of the [rb..re]
range. If you see x
, swap it into the range. After swapping, increment rb
. If you see that random_arr[rb]
is equal to x
, continue incrementing: these x
s are already in the right spot, you wouldn't need to swap them.
Now lets prove the correctness of the above. Observe that once an element is swapped into its place, it is never moved again. When you reach an element x
in the parent_arr
, all elements with lower number of swaps are already processed. By construction of the algorithm this means that these elements are already in place. Suppose that x
has k
number of allowed swaps. When you swap it into its place, you move another element out.
This replaced element cannot be x
, because the algorithm skips x
s when looking for a destination in the target range [rb..re]
. Moreover, the replaced element cannot be one of elements below x
in the parent_arr
, because all elements below x
are in their places already, and therefore cannot move. This means that the swap count of the replaced element is necessarily k+1
or more. Since by the time that we finish processing x
we have exhausted at most k
swaps on any element (which is easy to prove by induction), any element that we swap out to make room for x
will have at least one remaining swap that would allow us to swap it in place when we get to it in the order dictated by the parent_arr
.
Upvotes: 4