<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>choosesort</title> <h1>选择排序:假定一个最小值,依次比较如果发现了比这个还小的值那么交换数字;</h1> </head> <body> <script> var arr = [6,3,5,12,44,4,9,8,7,1]; function choosesort(arr) { for(var i = 0; i < arr.length; i++){ var min = arr[i]; var index = i; for(var j = i + 1; j < arr.length; j++){ if(min > arr[j]){ min = arr[j]; index = j; } } arr[index] = arr[i]; arr[i] = min; } return arr; } console.log(choosesort(arr)) </script> </body> </html>
选择排序
最新推荐文章于 2024-03-22 17:14:14 发布