-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubble-sort.js
58 lines (55 loc) · 1.13 KB
/
bubble-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 冒泡排序
*/
const arr = [5, 2, 324, 21, 34, 5, 2, 7, 5, 8, 9, 23, 3, 64, 25, 13];
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
function bubbleSort2(arr) {
for (let i = 0; i < arr.length; i++) {
let flag = true;
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
flag = false;
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (flag) {
break;
}
}
return arr;
}
function bubbleSort3(arr) {
let k = arr.length - 1;
for (let i = 0; i < arr.length; i++) {
let flag = true;
let pos = k;
for (let j = 0; j < k; j++) {
if (arr[j] > arr[j + 1]) {
flag = false;
pos = j;
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
k = pos;
if (flag) {
break;
}
}
return arr;
}
console.log(bubbleSort3(arr));