minimum number of swaps to sort an array javascript

Solutions on MaxInterview for minimum number of swaps to sort an array javascript by the best coders in the world

showing results for - "minimum number of swaps to sort an array javascript"
Louis
19 Aug 2019
1function minimumSwaps(arr) {
2    let swap = 0
3    for(let i = 0;i<arr.length;i++) {
4        while(arr[i] !== i+1) {
5            let temp = arr[i]
6            arr[i] = arr[temp - 1]
7            arr[temp - 1] = temp
8            swap++; 
9        }
10    }
11    return swap
12
13}
14