functionselectSort(arr){if(!arr || arr.length <2)retrun
let n = arr.length
for(let i =0; i < n; i++){let minIndex = i
for(let j = i; j < n; j++){
minIndex = arr[j]< arr[minIndex]? j : minIndex
}swap(arr, i, minIndex)}
console.log(arr)}// 交换数组函数functionswap(arr, i, j){let temp = arr[i]
arr[i]= arr[j]
arr[j]= temp
}
2.冒泡排序
functionbundleSort(arr){if(!arr || arr.length <2)retrun
let n = arr.length
for(let end = n -1; end >=0; end--){for(let start =1; start <= end; start++){if(arr[start-1]> arr[start])swap(arr, start-1, start)}}
console.log(arr)}// 交换数组函数functionswap(arr, i, j){let temp = arr[i]
arr[i]= arr[j]
arr[j]= temp
}
3.插入排序
functioninsertSort(arr){if(!arr || arr.length <2)retrun
let n = arr.length
for(let end =1; end < n; end++){for(let pre = n -1; pre >=0&& arr[pre]> arr[pre +1]; pre--){swap(arr, pre, pre +1)}}
console.log(arr)}// 交换数组函数functionswap(arr, i, j){let temp = arr[i]
arr[i]= arr[j]
arr[j]= temp
}