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
|
```js
|
||||||
const bubbleSort = arr => {
|
const bubbleSort = arr => {
|
||||||
let swapped = false;
|
let swapped = false;
|
||||||
|
|
||||||
const a = [...arr];
|
const a = [...arr];
|
||||||
|
|
||||||
for (let i = 1; i < a.length - 1; i++) {
|
for (let i = 1; i < a.length - 1; i++) {
|
||||||
swapped = false;
|
swapped = false;
|
||||||
|
|
||||||
for (let j = 0; j < a.length - i; j++) {
|
for (let j = 0; j < a.length - i; j++) {
|
||||||
if (a[j + 1] < a[j]) {
|
if (a[j + 1] < a[j]) {
|
||||||
[a[j], a[j + 1]] = [a[j + 1], a[j]];
|
[a[j], a[j + 1]] = [a[j + 1], a[j]];
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!swapped) return a;
|
||||||
if (!swapped) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user