<body>
<input type="text" id="num" value="3,2,1,4" /> <button id="sub">快速确定</button>
<script>
document.getElementById('sub').onclick=function(){
var val =document.getElementById('num').value;
var arr=val.split(',');
console.log(selectSort(arr))
}
function selectSort(arr){
var len = arr.length;
var minIndex, temp;
for (var i = 0; i < len - 1; i++) {
minIndex = i;
for (var j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) { // 寻找最小的数
minIndex = j; // 将最小数的索引保存
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
</script>
</body>
JS 数组排序--选择排序
最新推荐文章于 2025-06-07 14:01:27 发布
