Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's simplify your algorithm; then let's informally prove its correctness.</p> <h3>Modified algorithm</h3> <p>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 <code>x</code> their places in the sorted array. For example, if <code>c</code> is repeated 7 times and has 21 elements ahead of it, then <code>c</code>s will occupy the range <code>[21..27]</code> (all indexes are zero-based; the range is inclusive of its ends).</p> <p>Go through the <code>parent_arr</code> in the order of increasing number of swaps. For each element <code>x</code>, find the beginning of its target range <code>rb</code>; also note the end of its target range <code>re</code>. Now go through the elements of <code>random_arr</code> <em>outside</em> of the <code>[rb..re]</code> range. If you see <code>x</code>, swap it into the range. After swapping, increment <code>rb</code>. If you see that <code>random_arr[rb]</code> is equal to <code>x</code>, continue incrementing: these <code>x</code>s are already in the right spot, you wouldn't need to swap them.</p> <h3>Informal proof of correctness</h3> <p>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 <code>x</code> in the <code>parent_arr</code>, 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 <code>x</code> has <code>k</code> number of allowed swaps. When you swap it into its place, you move another element out.</p> <p>This replaced element cannot be <code>x</code>, because the algorithm skips <code>x</code>s when looking for a destination in the target range <code>[rb..re]</code>. Moreover, the replaced element cannot be one of elements <em>below</em> <code>x</code> in the <code>parent_arr</code>, because all elements below <code>x</code> are in their places already, and therefore cannot move. This means that the swap count of the replaced element is necessarily <code>k+1</code> or more. Since by the time that we finish processing <code>x</code> we have exhausted at most <code>k</code> swaps on any element (which is easy to prove by induction), any element that we swap out to make room for <code>x</code> 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 <code>parent_arr</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload