Skip to content

Commit 05d48b8

Browse files
tricinelitsvinayak
andauthored
Fix selection sort and add tests; fixes #414 (#418)
* Fix selection sort and add tests; fixes #414 Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent 0dfb200 commit 05d48b8

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

Sorts/SelectionSort.js

+27-13
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,43 @@
88
*from the unsorted subarray is picked and moved to the sorted subarray.
99
*/
1010

11-
function selectionSort (items) {
12-
var length = items.length
13-
for (var i = 0; i < length - 1; i++) {
11+
const selectionSort = (list) => {
12+
if (!Array.isArray(list)) {
13+
throw new TypeError('Given input is not an array')
14+
}
15+
const items = [...list] // We don't want to modify the original array
16+
const length = items.length
17+
for (let i = 0; i < length - 1; i++) {
18+
if (typeof items[i] !== 'number') {
19+
throw new TypeError('One of the items in your array is not a number')
20+
}
1421
// Number of passes
15-
var min = i // min holds the current minimum number position for each pass; i holds the Initial min number
16-
for (var j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array
22+
let min = i // min holds the current minimum number position for each pass; i holds the Initial min number
23+
for (let j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array
1724
if (items[j] < items[min]) { // Compare the numbers
1825
min = j // Change the current min number position if a smaller num is found
1926
}
2027
}
2128
if (min !== i) {
2229
// After each pass, if the current min num != initial min num, exchange the position.
2330
// Swap the numbers
24-
[items[i], items[min]] = [items[min], [items[i]]]
31+
[items[i], items[min]] = [items[min], items[i]]
2532
}
2633
}
34+
return items
2735
}
2836

29-
// Implementation of Selection Sort
37+
/* Implementation of Selection Sort
38+
39+
(() => {
40+
let array = [5, 6, 7, 8, 1, 2, 12, 14]
41+
// Array before Sort
42+
console.log(array)
43+
array = selectionSort(array)
44+
// Array after sort
45+
console.log(array)
46+
})()
47+
48+
*/
3049

31-
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
32-
// Array before Sort
33-
console.log(ar)
34-
selectionSort(ar)
35-
// Array after sort
36-
console.log(ar)
50+
export { selectionSort }

Sorts/SelectionSort.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { selectionSort } from './SelectionSort'
2+
3+
describe('selectionSort', () => {
4+
it('expects to return the array sorted in ascending order', () => {
5+
var toSort = [5, 6, 7, 8, 1, 2, 12, 14]
6+
const expected = [1, 2, 5, 6, 7, 8, 12, 14]
7+
8+
expect(selectionSort(toSort)).toEqual(expected)
9+
})
10+
11+
it('expects to throw if it is not a valid array', () => {
12+
expect(() => selectionSort('abc')).toThrow('Given input is not an array')
13+
expect(() => selectionSort(123)).toThrow('Given input is not an array')
14+
expect(() => selectionSort({})).toThrow('Given input is not an array')
15+
expect(() => selectionSort(null)).toThrow('Given input is not an array')
16+
expect(() => selectionSort()).toThrow('Given input is not an array')
17+
})
18+
19+
it('expects to throw if one of the elements in the array is not a number', () => {
20+
expect(() => selectionSort([1, 'x', 2])).toThrow('One of the items in your array is not a number')
21+
})
22+
})

0 commit comments

Comments
 (0)