Fix selection sort and add tests; fixes #414 (#418)

* Fix selection sort and add tests; fixes #414 
Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
Bogdan Lazar
2020-10-05 18:03:52 +00:00
committed by GitHub
parent 0dfb200e64
commit 05d48b8ff9
2 changed files with 49 additions and 13 deletions

View File

@ -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
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
(() => {
let array = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
selectionSort(ar)
console.log(array)
array = selectionSort(array)
// Array after sort
console.log(ar)
console.log(array)
})()
*/
export { selectionSort }

View File

@ -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')
})
})