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:
- You should use swap function.
- Every element may repeat minimum 5 times and maximum 26 times.
- You cannot make elements of given array to 0.
- You should not write helper functions.
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?
First, let’s simplify your algorithm; then let’s informally prove its correctness.
Modified algorithm
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
xtheir places in the sorted array. For example, ifcis repeated 7 times and has 21 elements ahead of it, thencs will occupy the range[21..27](all indexes are zero-based; the range is inclusive of its ends).Go through the
parent_arrin the order of increasing number of swaps. For each elementx, find the beginning of its target rangerb; also note the end of its target rangere. Now go through the elements ofrandom_arroutside of the[rb..re]range. If you seex, swap it into the range. After swapping, incrementrb. If you see thatrandom_arr[rb]is equal tox, continue incrementing: thesexs are already in the right spot, you wouldn’t need to swap them.Informal proof of correctness
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
xin theparent_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 thatxhasknumber of allowed swaps. When you swap it into its place, you move another element out.This replaced element cannot be
x, because the algorithm skipsxs when looking for a destination in the target range[rb..re]. Moreover, the replaced element cannot be one of elements belowxin theparent_arr, because all elements belowxare in their places already, and therefore cannot move. This means that the swap count of the replaced element is necessarilyk+1or more. Since by the time that we finish processingxwe have exhausted at mostkswaps on any element (which is easy to prove by induction), any element that we swap out to make room forxwill 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 theparent_arr.