Answers for "minimum number of swaps to sort an array javascript"

-1

minimum number of swaps to sort an array javascript

function minimumSwaps(arr) {
    let swap = 0
    for(let i = 0;i<arr.length;i++) {
        while(arr[i] !== i+1) {
            let temp = arr[i]
            arr[i] = arr[temp - 1]
            arr[temp - 1] = temp
            swap++; 
        }
    }
    return swap

}
Posted by: Guest on January-21-2021

Code answers related to "minimum number of swaps to sort an array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language