Skip to content

Commit 286d1e5

Browse files
committed
refactor(buble-sort): update sort
1 parent 6a22abc commit 286d1e5

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
}],
1515
"camelcase": "off",
1616
"no-param-reassign": "off",
17-
"no-plusplus": "off"
17+
"no-plusplus": "off",
18+
"class-methods-use-this": "off"
1819
}
1920
}

algorithms/sorts/bubble.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
const sort = (array) => {
2-
let swapped;
3-
do {
4-
swapped = false;
5-
for (let i = 0; i < array.length; i++) {
6-
if (array[i] > array[i + 1]) {
7-
const temp = array[i];
8-
array[i] = array[i + 1];
9-
array[i + 1] = temp;
10-
swapped = true;
1+
const Sort = require('./sort');
2+
3+
class BubbleSort extends Sort {
4+
sort(originalArray) {
5+
let swapped = false;
6+
const array = [...originalArray];
7+
for (let i = 1; i < array.length; i += 1) {
8+
swapped = false;
9+
this.callbacks.visitingCallback(array[i]);
10+
11+
for (let j = 0; j < array.length - i; j += 1) {
12+
this.callbacks.visitingCallback(array[j]);
13+
if (this.comparator.lessThan(array[j + 1], array[j])) {
14+
[array[j], array[j + 1]] = [array[j + 1], array[j]];
15+
swapped = true;
16+
}
1117
}
12-
}
13-
} while (swapped);
1418

15-
return array;
16-
};
19+
if (!swapped) {
20+
return array;
21+
}
22+
}
23+
return array;
24+
}
25+
}
1726

18-
module.exports = sort;
27+
module.exports = BubbleSort;

0 commit comments

Comments
 (0)