【九月打卡】第28天 數(shù)據結構和算法
標簽:
算法與數(shù)據結構
JS实现选择排序
思路
- 选择数组中的最小值,并将其放到第一位
- 接着寻找第二小的值,放到第二位
- 依次执行n-1轮,选择排序完成
Array.prototype.selectSort = function () {
for (let i = 0; i < this.length - 1; i++) {
let minIndex = i;
for (let j = i; j < this.length; j++) {
if (this[j] < this[minIndex]) {
minIndex = j
}
}
if (i !== minIndex) {
const temp = this[i];
this[i] = this[minIndex];
this[minIndex] = temp;
}
}
}
arr.selectSort()
console.log(arr)
时间复杂度:O(n^2)
空间复杂度:O(1)
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦