Note that there are some explanatory texts on larger screens.

plurals
  1. POminimum switch to sorted permutation
    primarykey
    data
    text
    <p>Suppose I have an array like this: [5 4 1 2 3]</p> <p>And I want to compute the minimum switch I have to make to sort the unsorted permutation. </p> <p>Now the answer is 7 in this case. Just move 4 and 5 to the right, or move 1, 2, 3 to the left.</p> <p>The irony though, is that I used [4 5 1 2 3] in my notes, which gives 6, and mislead myself and make a fool of myself.</p> <p>Steps:</p> <p>[5 1 4 2 3] // step 1</p> <p>[1 5 4 2 3] // step 2</p> <p>[1 5 2 4 3] // step 3</p> <p>[1 2 5 4 3] // step 4</p> <p>[1 2 5 3 4] // step 5</p> <p>[1 2 3 5 4] // step 6</p> <p>[1 2 3 4 5] // step 7</p> <p>I've thought of things like having an array that keep the offset needed, and for each loop, just look for the switch that moves the whole thing closer to goal. </p> <p>But that just seem too slow, any ideas?</p> <p>EDIT: from comment: are the members of the array guaranteed to completely belong to {1..N} set for an array of size N, without repeating numbers? </p> <p>Nope. It's not guaranteed not to repeat or being in [1...n] for array sized N.</p> <p>UPDATE:</p> <p>There are two solutions to this particular problem, once is slower but more straightforward bubblesort, another is the faster but less straightforward mergesort. </p> <p>With bubblesort, you basically count the number of switches when running the algorithm.</p> <p>With mergesort, it's a bit more trickier, but the counting happens when merging. When the array is already merged, the count should yield 0 as no switches will be needed to sort this array. With bubblesort, you count the switches when you push the largest or the smallest number to the left or right. With mergesort, you count switches when merging. I bit of hand writing brute forcing will get you there. </p>
    singulars
    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.
 

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