[英]How to find the Minimum number of swaps required for sorting an array of numbers in descending order in javascript
I'm trying to get my code to do this:我试图让我的代码做到这一点:
Original array = [1,2,3,4] swap once-> [4,2,3,1] swap again->[4,3,2,1]原始数组 = [1,2,3,4] 交换一次-> [4,2,3,1] 再次交换->[4,3,2,1]
Therefore result is 2因此结果是 2
But it's not working.但它不起作用。 Here's what I have so far:
这是我到目前为止所拥有的:
function check(arr){ var sarr = []; var cnt = 0; var arrL = arr.length; // Create a second copy of the array for reference var arrCopy = [...arr]; for(let i=0; i<arrL;i++){ var maxV = Math.max(...arr); sarr.push(maxV); let pos = arr.indexOf(maxV); // Remove the found number arr.splice(pos,1); // Check if the index of the number in the new array is same with the copy, if not then there was a swap let ai =arrCopy.indexOf(maxV); let si =sarr.indexOf(maxV); if (ai !== si && (i+1)!=arrL && pos !== 0){ cnt++; }; } console.log(cnt); } check([1, 2, 3, 4, 5, 6]);//Result should be 3 check([6,5,4,3,2,1]); //result should be 0 check([1,2,3,4]); //result should be 2 check([1,3,2,5,4,6]); //result should be 3 check([1,2,10,4,5,6,7,8,9,3,12,11]);//result should be 6 check([ 49, 37, 9, 19, 27, 3, 25, 11, 53, 42, 57, 50, 55, 56, 38, 48, 6, 33, 28, 8, 20, 31, 51, 14, 23, 4, 58, 52, 36, 22, 41, 47, 39, 2, 7, 13, 45, 1, 44, 32, 10, 15, 21, 30, 17, 60, 29, 5, 59, 12, 40, 24, 54, 46, 26, 43, 35, 34, 18, 16]);//result should be 54
Can someone please let me know what I'm doing wrong?有人可以让我知道我做错了什么吗?
I would start with a copy of the array in descending order for getting the right index of the items.我将从按降序排列的数组副本开始,以获得正确的项目索引。
For practical reasons, (or just a shorter conception of the loop with including check and decrement), I loop from the end of the array.出于实际原因,(或者只是包含检查和递减的循环的较短概念),我从数组的末尾开始循环。
Then I check the value of array
and reversed
at the dame index and go on with the iteration.然后我检查
array
的值并在夫人索引处reversed
并继续迭代。
If not the same value, the items at the wanted position i
and the actual position p
are swapped and the count incremented.如果不是相同的值,则交换所需位置
i
和实际位置p
项目并增加计数。
At the end the count is returned.最后返回计数。
function check(array) { var reversed = array.slice().sort((a, b) => b - a), count = 0, i = array.length, p; while (i--) { if (array[i] === reversed[i]) continue; p = array.indexOf(reversed[i]); [array[i], array[p]] = [array[p], array[i]]; count++; } console.log(...array); return count; } console.log(check([1, 2, 3, 4, 5, 6])); // 3 console.log(check([6, 5, 4, 3, 2, 1])); // 0 console.log(check([1, 2, 3, 4])); // 2 console.log(check([1, 3, 2, 5, 4, 6])); // 3 console.log(check([1, 2, 10, 4, 5, 6, 7, 8, 9, 3, 12, 11])); // 6 console.log(check([ 49, 37, 9, 19, 27, 3, 25, 11, 53, 42, 57, 50, 55, 56, 38, 48, 6, 33, 28, 8, 20, 31, 51, 14, 23, 4, 58, 52, 36, 22, 41, 47, 39, 2, 7, 13, 45, 1, 44, 32, 10, 15, 21, 30, 17, 60, 29, 5, 59, 12, 40, 24, 54, 46, 26, 43, 35, 34, 18, 16])); // 54
.as-console-wrapper { max-height: 100% !important; top: 0; }
function minimumSwaps(arr) {
var count = 0;
arr.sort((a, b) => {
if (a < b) {
count++;
}
});
return count;
}
console.log(minimumSwaps([1, 2, 3, 4, 7, 6, 5]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.