选择排序
<input type="button" onclick="go()" value="点击"> <h3 id="chu"></h3> <script> var d = [3, 9, 4, 2, 6, 8, 7, 1, 99, 9, 9, 9]; function xuanze(arr) { var index; // 循环到数组倒数第二个数结束 for (var a = 0; a < arr.length - 1; a++) { index = a; for (var b = a + 1 ; b < arr.length; b++) { // 对比,更改索引 if (arr[b] < arr[index]) { index = b; } } // 交换位置 [arr[index], arr[a]] = [arr[a], arr[index]]; } return arr; } function go() { document.getElementById('chu').innerHTML = xuanze(d); } </script>