You are viewing a single comment's thread. Return to all comments →
JavaScript by comparing array to presorted as strings
function minimumSwaps(arr) { let count = 0; let sorted = [...arr].sort((a,b) => a-b) let temp = 0; while(arr.join() !== sorted.join()){ for(let i =0; i < arr.length; i++){ if(arr[i] !== i + 1){ temp = arr[i] arr[i] = arr[arr[i] - 1] arr[temp -1] = temp count = count + 1 } } } return count;
function minimumSwaps(arr) { let count = 0; let sorted = [...arr].sort((a,b) => a-b) let temp = 0; while(arr.join() !== sorted.join()){ for(let i =0; i < arr.length; i++){ if(arr[i] !== i + 1){ temp = arr[i] arr[i] = arr[arr[i] - 1] arr[temp -1] = temp count = count + 1 } } }
}
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 →
JavaScript by comparing array to presorted as strings
function minimumSwaps(arr) { let count = 0; let sorted = [...arr].sort((a,b) => a-b) let temp = 0; while(arr.join() !== sorted.join()){ for(let i =0; i < arr.length; i++){ if(arr[i] !== i + 1){ temp = arr[i] arr[i] = arr[arr[i] - 1] arr[temp -1] = temp count = count + 1 } } }
return count;}