You are viewing a single comment's thread. Return to all comments →
I knew this required counting the no. of swaps done in a known sorting algorithm...
This is selection sort.
function minimumSwaps(arr) { let swapCount = 0; const swap = (i, j) => { [arr[i], arr[j]] = [arr[j], arr[i]]; } for (let i = 0; i < arr.length; i+=1) { let min = i; for (let j = i + 1; j < arr.length; j+=1) { if (arr[j] < arr[min]) min = j; } if (min !== i) { swap(i, min); swapCount += 1; } } return swapCount; }
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Swaps 2
You are viewing a single comment's thread. Return to all comments →
I knew this required counting the no. of swaps done in a known sorting algorithm...
This is selection sort.