Skip to content

Commit 8b24426

Browse files
authored
refactor: optimizing JavaScript examples (#642)
* refactor: optimizing JavaScript examples * style: adjust code indentation
1 parent d4fef06 commit 8b24426

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

basic/sorting/BubbleSort/README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,27 @@ public class BubbleSort {
4545

4646
```js
4747
function bubbleSort(inputArr) {
48-
let len = inputArr.length;
49-
let swapped = false;
50-
for (let i = 1; i <= len - 1; i++) {
51-
swapped = false;
52-
for (let j = 0; j < len - 1; j++) {
48+
for (let i = inputArr.length - 1; i > 0; i--) {
49+
let hasChange = false;
50+
for (let j = 0; j < i; j++) {
5351
if (inputArr[j] > inputArr[j + 1]) {
54-
let temp = inputArr[j];
52+
const temp = inputArr[j];
5553
inputArr[j] = inputArr[j + 1];
5654
inputArr[j + 1] = temp;
57-
swapped = true
55+
hasChange = true;
5856
}
5957
}
60-
if (swapped === false) break;
58+
59+
if (!hasChange) {
60+
break;
61+
}
6162
}
62-
return (inputArr)
63+
64+
return inputArr;
6365
}
6466

65-
let arr = [6, 3, 2, 1, 5];
66-
console.log(bubbleSort(arr))
67+
const arr = [6, 3, 2, 1, 5];
68+
console.log(bubbleSort(arr));
6769
```
6870

6971
### **Go**

0 commit comments

Comments
 (0)