// 选择排序算法
// 时间复杂度为 O(n*n)
function selectSort(arr) {
var min = null,
temp, index = 1;
for (var i = 0; i < arr.length - 1; i++) {
min = arr[i];
for (var j = i + 1; j < arr.length; j++) {
if (arr[j] < min) {
min = arr[j];
index = j;
}
}
temp = arr[i];
arr[i] = min;
arr[index] = temp;
}
return arr;
}
function getRandomNumbers(min, max, count) {
count = count || 10;
min = min || 0;
max = max || 10;
var res = [];
var temp;
var cap = max - min;
for (var i = 0; i < count; i++) {
temp = Number((Math.random() * cap + min).toFixed(2));
res.push(temp)
}
return res;
}
console.time()
console.log(selectSort(getRandomNumbers(1, 30, 10)))
console.timeEnd()
网友评论