diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 38a4bfff24..c4a11e354e 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -8,12 +8,19 @@ *from the unsorted subarray is picked and moved to the sorted subarray. */ -function selectionSort (items) { - var length = items.length - for (var i = 0; i < length - 1; i++) { +const selectionSort = (list) => { + if (!Array.isArray(list)) { + throw new TypeError('Given input is not an array') + } + const items = [...list] // We don't want to modify the original array + const length = items.length + for (let i = 0; i < length - 1; i++) { + if (typeof items[i] !== 'number') { + throw new TypeError('One of the items in your array is not a number') + } // Number of passes - var min = i // min holds the current minimum number position for each pass; i holds the Initial min number - for (var j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array + let min = i // min holds the current minimum number position for each pass; i holds the Initial min number + for (let j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array if (items[j] < items[min]) { // Compare the numbers min = j // Change the current min number position if a smaller num is found } @@ -21,16 +28,23 @@ function selectionSort (items) { if (min !== i) { // After each pass, if the current min num != initial min num, exchange the position. // Swap the numbers - [items[i], items[min]] = [items[min], [items[i]]] + [items[i], items[min]] = [items[min], items[i]] } } + return items } -// Implementation of Selection Sort +/* Implementation of Selection Sort + +(() => { + let array = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log(array) + array = selectionSort(array) + // Array after sort + console.log(array) +})() + +*/ -var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -selectionSort(ar) -// Array after sort -console.log(ar) +export { selectionSort } diff --git a/Sorts/SelectionSort.test.js b/Sorts/SelectionSort.test.js new file mode 100644 index 0000000000..57ad1e8ecf --- /dev/null +++ b/Sorts/SelectionSort.test.js @@ -0,0 +1,22 @@ +import { selectionSort } from './SelectionSort' + +describe('selectionSort', () => { + it('expects to return the array sorted in ascending order', () => { + var toSort = [5, 6, 7, 8, 1, 2, 12, 14] + const expected = [1, 2, 5, 6, 7, 8, 12, 14] + + expect(selectionSort(toSort)).toEqual(expected) + }) + + it('expects to throw if it is not a valid array', () => { + expect(() => selectionSort('abc')).toThrow('Given input is not an array') + expect(() => selectionSort(123)).toThrow('Given input is not an array') + expect(() => selectionSort({})).toThrow('Given input is not an array') + expect(() => selectionSort(null)).toThrow('Given input is not an array') + expect(() => selectionSort()).toThrow('Given input is not an array') + }) + + it('expects to throw if one of the elements in the array is not a number', () => { + expect(() => selectionSort([1, 'x', 2])).toThrow('One of the items in your array is not a number') + }) +})