Skip to content

Commit b135a51

Browse files
Handle empty array input in selection sort
1 parent da50af3 commit b135a51

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Sorts/SelectionSort.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ export const selectionSort = (list) => {
1212
if (!Array.isArray(list)) {
1313
throw new TypeError('Given input is not an array')
1414
}
15-
const items = [...list] // We don't want to modify the original array
15+
16+
if (list.length === 0) {
17+
return []
18+
}
19+
20+
const items = [...list]
21+
1622
const length = items.length
1723
for (let i = 0; i < length - 1; i++) {
1824
if (typeof items[i] !== 'number') {

0 commit comments

Comments
 (0)