Update selectionSort.md
This commit is contained in:
committed by
GitHub
parent
3b4f458782
commit
3e1e383386
@ -12,14 +12,12 @@ Sorts an array of numbers, using the selection sort algorithm.
|
|||||||
```js
|
```js
|
||||||
const selectionSort = arr => {
|
const selectionSort = arr => {
|
||||||
const a = [...arr];
|
const a = [...arr];
|
||||||
|
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
const min = a
|
const min = a
|
||||||
.slice(i + 1)
|
.slice(i + 1)
|
||||||
.reduce((acc, val, j) => (val < a[acc] ? j + i + 1 : acc), i);
|
.reduce((acc, val, j) => (val < a[acc] ? j + i + 1 : acc), i);
|
||||||
if (min !== i) [a[i], a[min]] = [a[min], a[i]];
|
if (min !== i) [a[i], a[min]] = [a[min], a[i]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user