Update bubbleSort.md
This commit is contained in:
committed by
GitHub
parent
3d6dc4bf40
commit
443b97a4ca
@ -14,24 +14,17 @@ Sorts an array of numbers, using the bubble sort algorithm.
|
||||
```js
|
||||
const bubbleSort = arr => {
|
||||
let swapped = false;
|
||||
|
||||
const a = [...arr];
|
||||
|
||||
for (let i = 1; i < a.length - 1; i++) {
|
||||
swapped = false;
|
||||
|
||||
for (let j = 0; j < a.length - i; j++) {
|
||||
if (a[j + 1] < a[j]) {
|
||||
[a[j], a[j + 1]] = [a[j + 1], a[j]];
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!swapped) {
|
||||
return a;
|
||||
}
|
||||
if (!swapped) return a;
|
||||
}
|
||||
|
||||
return a;
|
||||
};
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user