You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const TYPE = {
asc: Symbol('asc'),//升序
des: Symbol('des')//降序
};
let testArr = [8,5,2,6,9,3,1,4,0,7]
function selectSort(arr,type) {
const n = arr.length;
let tem = -Number.MAX_VALUE;
for (let i = 0; i < n - 1; i++) {
let flag = false;
for (let j = i + 1; j < n; j++) {
if (type===TYPE.asc?arr[j] < arr[i]:arr[i] < arr[j]) {
flag = true
tem = arr[j];
arr[j] = arr[i];
arr[i] = tem;
}
}
if(!flag) break; //如果没有发生过位置交换,之后就是有序了,不在遍历
}
return arr
}
The text was updated successfully, but these errors were encountered:
时间复杂度:O(n2
空间复杂度:O(1)
非稳定排序
原地排序
The text was updated successfully, but these errors were encountered: