冒泡排序
function sort(arr) { var temp; for (var i = 0; i < arr.length - 1; i++) { for (var j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } return arr; }
选择排序
function chooes(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; } } var temp = arr[i]; arr[i] = arr[index]; arr[index] = temp } return arr; }
相比之下 选择排序更快
标签:index,arr,temp,++,冒泡排序,选择,length,var,排序 From: https://www.cnblogs.com/liu521125/p/17849012.html