选择在JavaScript中排序 [英] Selection Sort in JavaScript
问题描述
function newsort(arr, left, right){
for(var i= left; i < right; ++i){
var min = i;
for (var j = i; j < right; ++j){
if (arr[min] > arr[j]){
min = j;
}
}
var temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
return arr;
}
var arr = [3,5,66,78,23,44,11,32,58];
alert(newsort(arr, arr.length, 0));
上面是我编写的函数的代码.我对JS还是很陌生,因此有时在语法上会感到困惑.我目前只是返回原始数组,但是我正在尝试进行选择排序,即右/左/中类型.....我真的无法分辨当前发生了什么.我只是在尝试排序然后返回数组.
Above is the code for a function that I have written. I am still very new to JS, and as a result get confused at times when it comes to syntax. I currently just return the original array, but am trying to do the selection sort, the right/left/mid type.....I can't really tell what is going on at the moment. I am simply trying to sort and then return the array.
外面有人能指出我正确的方向吗?
Anyone out there able to point me in the right direction?
谢谢.....
推荐答案
您的代码的问题是左右参数传递的方式错误.这是工作代码:alert(newsort(arr, 0 ,arr.length));
The problem with your code is that the left and right parameters are passed in the wrong way round. Here is the working code:alert(newsort(arr, 0 ,arr.length));
这篇关于选择在JavaScript中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!