mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
* Fix selection sort and add tests; fixes #414 Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
@ -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]
|
||||
// Array before Sort
|
||||
console.log(ar)
|
||||
selectionSort(ar)
|
||||
// Array after sort
|
||||
console.log(ar)
|
||||
(() => {
|
||||
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)
|
||||
})()
|
||||
|
||||
*/
|
||||
|
||||
export { selectionSort }
|
||||
|
22
Sorts/SelectionSort.test.js
Normal file
22
Sorts/SelectionSort.test.js
Normal 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')
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user