js排序专场
1,冒泡排序
function bubble(arr) { var len = arr.length - 1; for (let i = 0 ; i < len; i++) { var bool = true; for (let j = 0; j < len - i; j++) { if (arr[j] > arr[j]+1) { [arr[j], arr[j+1]] = [arr[j+1], arr[j]]; bool = false; } } if (bool) break; } return arr; }
2,选择排序
function select(arr) { var len = arr.length - 1, temp; for (var i = 0; i < len; i++) { for (var j = 0; j < len - i; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; }
3,插入排序
function insert(arr) { var len = arr.length; for (var i = 1; i < len; i++) { var temp = arr[i]; for (var j = i -1; j >= 0; j--) { if (arr[j] > temp) { arr[j + 1] = arr[j]; } else { break; } } arr[j+1] = temp; } return arr; }