# 冒泡排序
# 冒泡排序的思路
比较所有相邻元素,如果第一个比第二个大,则交换他们。
一轮下来,可以保证最后一个数是最大的。
执行n - 1论,就可以完成排序。
# JS实现
Array.prototype.bubbleSort = function () {
for (let i = 0; i < this.length - 1; i++) {
for (let j = 0; j < this.length - 1 - i; j++) {
if (this[j] > this[j + 1]) {
const temp = this[j];
this[j] = this[j + 1];
this[j + 1] = temp;
}
}
}
}
const arr = [5, 4, 3, 2, 1];
arr.bubbleSort()
console.log(arr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 冒泡排序的时间复杂度
两个嵌套循环
时间复杂度 O(n^2)