title: Minimum number of swaps to sort an array
body: <p>I need to do something like this: Let's say I have an array:</p>
  
  <pre><code> [3, 4, 1, 2]
  </code></pre>
  
  <p>I need to swap 3 and 4, and 1 and 2, so my array looks like <code>[4, 3, 2, 1]</code>. Now, I can just do the <code>sort()</code>. Here I need to count how many iterations I need, to change the initial array to the final output. Example: </p>
  
  <pre><code>// I can sort one pair per iteration
  let array = [3, 4, 1, 2, 5]
  let counter = 0;
  
  //swap 3 and 4
  counter++;
  // swap 1 and 2
  counter++;
  // 5 goes to first place
  counter++
  
  // now counter = 3 &lt;-- what I need
  </code></pre>
  
  <p>EDIT: Here is what I tried. doesn't work always tho... it is from this question: <a href="https://stackoverflow.com/questions/7502489/bubble-sort-algorithm-javascript">Bubble sort algorithm JavaScript</a></p>
  
  <pre><code>let counter = 0;
      let swapped;
      do {
          swapped = false;
          for (var i = 0; i &lt; array.length - 1; i++) {
              if (array[i] &lt; array[i + 1]) {
                  const temp = array[i];
                  array[i] = array[i + 1];
                  array[i + 1] = temp;
                  swapped = true;
                  counter++;
              }
          }
      } while (swapped);
  </code></pre>
  
  <p><strong>EDIT</strong>: It is not correct all the time because I can swap places from last to first, for example. Look at the example code above, it is edited now.</p>