From cf4e07941ca0d5088a2e5afc4aebd6fab4fcf1de Mon Sep 17 00:00:00 2001 From: Bogdan Lazar Date: Mon, 5 Oct 2020 14:27:45 +0200 Subject: [PATCH 1/4] Fix selection sort and add tests; fixes #414 --- Sorts/SelectionSort.js | 25 ++++++++++++++++++------- Sorts/SelectionSort.test.js | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 Sorts/SelectionSort.test.js diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 38a4bfff24..b7ea064744 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++) { +function selectionSort(list) { + if (!Array.isArray(list)) { + throw new TypeError('Given input is not an array') + } + let items = [...list]; // We don't want to modify the original array + let 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,12 +28,13 @@ 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 @@ -34,3 +42,6 @@ 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') + }) +}) From bfbdca9ebfe857ab75872a537fa029aa538b133b Mon Sep 17 00:00:00 2001 From: Bogdan Lazar Date: Mon, 5 Oct 2020 14:33:16 +0200 Subject: [PATCH 2/4] Fix standard rules; related to #414 --- Sorts/SelectionSort.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index b7ea064744..6181489c31 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -8,12 +8,12 @@ *from the unsorted subarray is picked and moved to the sorted subarray. */ -function selectionSort(list) { +function selectionSort (list) { if (!Array.isArray(list)) { throw new TypeError('Given input is not an array') } - let items = [...list]; // We don't want to modify the original array - let length = items.length + 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') @@ -31,7 +31,7 @@ function selectionSort(list) { [items[i], items[min]] = [items[min], items[i]] } } - return items; + return items } /* Implementation of Selection Sort From 7795343f34196c828d9a8a4a80fe351f8c69ebcb Mon Sep 17 00:00:00 2001 From: vinayak Date: Mon, 5 Oct 2020 23:23:30 +0530 Subject: [PATCH 3/4] Update SelectionSort.js --- Sorts/SelectionSort.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 6181489c31..49c4dec716 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -8,7 +8,7 @@ *from the unsorted subarray is picked and moved to the sorted subarray. */ -function selectionSort (list) { +const selectionSort = (list) => { if (!Array.isArray(list)) { throw new TypeError('Given input is not an array') } @@ -34,14 +34,14 @@ function selectionSort (list) { return items } -/* Implementation of Selection Sort +/* Implementation of Selection Sort */ +// testing -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 } +(() => { + 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) +})() From 4a14756e820df04fb05686a61f6f6ae59899669e Mon Sep 17 00:00:00 2001 From: vinayak Date: Mon, 5 Oct 2020 23:29:43 +0530 Subject: [PATCH 4/4] Update SelectionSort.js --- Sorts/SelectionSort.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 49c4dec716..c4a11e354e 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -34,8 +34,7 @@ const selectionSort = (list) => { return items } -/* Implementation of Selection Sort */ -// testing +/* Implementation of Selection Sort (() => { let array = [5, 6, 7, 8, 1, 2, 12, 14] @@ -45,3 +44,7 @@ const selectionSort = (list) => { // Array after sort console.log(array) })() + +*/ + +export { selectionSort } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy