From 4ebe59345b590c7f71a51fef1bb473ff2c88632a Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:42:28 -0400 Subject: [PATCH 1/8] Add function documentation to combSort function --- Sorts/CombSort.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index 4091f4908..ce4829aed 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -16,7 +16,13 @@ * Wikipedia: https://en.wikipedia.org/wiki/Comb_sort */ -function combSort (list) { +/** + * combSort returns an array of numbers sorted in increasing order. + * + * @param {number[]} list The array of numbers to sort. + * @return {number[]} The array of numbers sorted in increasing order. + */ +function combSort(list) { if (list.length === 0) { return list } From ea4e816b5ea8d5cb0b626d9f2f64dd47a8c3fdf8 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:42:49 -0400 Subject: [PATCH 2/8] Add export of combSort for Jest testing --- Sorts/CombSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index ce4829aed..4dc7f0485 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -60,3 +60,5 @@ console.log(array) console.log('- After Sort | Implementation of Comb Sort -') console.log(combSort(array)) console.log('\n') + +export { combSort } From 7c0379c024c2b1032d08fca37f4fcb5d7d93263a Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:43:54 -0400 Subject: [PATCH 3/8] Add tests for CombSort --- Sorts/test/CombSort.test.js | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Sorts/test/CombSort.test.js diff --git a/Sorts/test/CombSort.test.js b/Sorts/test/CombSort.test.js new file mode 100644 index 000000000..ee4ec6961 --- /dev/null +++ b/Sorts/test/CombSort.test.js @@ -0,0 +1,67 @@ +import { combSort } from '../CombSort' + +it('should correctly sort an input list that is sorted backwards', () => { + const array = [5, 4, 3, 2, 1] + expect(combSort(array)).toEqual([1, 2, 3, 4, 5]) +}) + +it('should correctly sort an input list that is unsorted', () => { + const array = [15, 24, 3, 2224, 1] + expect(combSort(array)).toEqual([1, 3, 15, 24, 2224]) +}) + +describe('Variations of input array lengths', () => { + it('should return an empty list with the input list is an empty list', () => { + expect(combSort([])).toEqual([]) + }) + + it('should correctly sort an input list of length 1', () => { + expect(combSort([100])).toEqual([100]) + }) + + it('should correctly sort an input list of an odd length', () => { + expect(combSort([101, -10, 321])).toEqual([-10, 101, 321]) + }) + + it('should correctly sort an input list of an even length', () => { + expect(combSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56]) + }) +}) + +describe('Variations of input array elements', () => { + it('should correctly sort an input list that contains only positive numbers', () => { + expect(combSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) + }) + + it('should correctly sort an input list that contains only negative numbers', () => { + expect(combSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1]) + }) + + it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => { + expect(combSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56]) + }) + + it('should correctly sort an input list that contains only whole numbers', () => { + expect(combSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) + }) + + it('should correctly sort an input list that contains only decimal numbers', () => { + expect(combSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45]) + }) + + it('should correctly sort an input list that contains only a mix of whole and decimal', () => { + expect(combSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56]) + }) + + it('should correctly sort an input list that contains only fractional numbers', () => { + expect(combSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98]) + }) + + it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => { + expect(combSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12]) + }) + + it('should correctly sort an input list that contains duplicates', () => { + expect(combSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + }) +}) From 7076c2afbfdda1ecb87fc917a5ce3de536155f7d Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:44:12 -0400 Subject: [PATCH 4/8] Fix style per standard js rules --- Sorts/CombSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index 4dc7f0485..b56b1aa5c 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -22,7 +22,7 @@ * @param {number[]} list The array of numbers to sort. * @return {number[]} The array of numbers sorted in increasing order. */ -function combSort(list) { +function combSort (list) { if (list.length === 0) { return list } From c83bc5ba6ed31691cbc9619f012365dfe6f31123 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 5 Oct 2021 09:00:29 -0400 Subject: [PATCH 5/8] Remove testing code in function file --- Sorts/CombSort.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index b56b1aa5c..d22d5b54e 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -49,16 +49,4 @@ function combSort (list) { return list } -/** -* Implementation of Comb Sort -*/ -const array = [5, 6, 7, 8, 1, 2, 12, 14] -// Before Sort -console.log('\n- Before Sort | Implementation of Comb Sort -') -console.log(array) -// After Sort -console.log('- After Sort | Implementation of Comb Sort -') -console.log(combSort(array)) -console.log('\n') - export { combSort } From 801b0cd71e2b53ed780a3ceddb5a15ad3247631c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 5 Oct 2021 13:00:52 +0000 Subject: [PATCH 6/8] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6ae9a6d30..3c6cad5a3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -318,6 +318,7 @@ * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js) * [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js) * test + * [CombSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/CombSort.test.js) * [FisherYatesShuffle](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/FisherYatesShuffle.test.js) * [QuickSortRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/QuickSortRecursive.test.js) * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/SelectionSort.test.js) From 8f49b7894d60ef840851435e400969a59d7e229d Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 5 Oct 2021 13:06:02 +0000 Subject: [PATCH 7/8] Auto-update DIRECTORY.md --- DIRECTORY.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index f9440be4a..d63e09657 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -255,12 +255,6 @@ * [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js) * [WiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/WiggleSort.js) - * test - * [CombSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/CombSort.test.js) - * [FisherYatesShuffle](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/FisherYatesShuffle.test.js) - * [QuickSortRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/QuickSortRecursive.test.js) - * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/SelectionSort.test.js) - ## String * [AlternativeStringArrange](https://github.com/TheAlgorithms/Javascript/blob/master/String/AlternativeStringArrange.js) * [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js) From 6bba9da11866e5e0e2343862334eaee355873bc2 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 5 Oct 2021 17:23:36 -0400 Subject: [PATCH 8/8] Group tests in a describe --- Sorts/test/CombSort.test.js | 116 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/Sorts/test/CombSort.test.js b/Sorts/test/CombSort.test.js index ee4ec6961..dec1319ed 100644 --- a/Sorts/test/CombSort.test.js +++ b/Sorts/test/CombSort.test.js @@ -1,67 +1,69 @@ import { combSort } from '../CombSort' -it('should correctly sort an input list that is sorted backwards', () => { - const array = [5, 4, 3, 2, 1] - expect(combSort(array)).toEqual([1, 2, 3, 4, 5]) -}) - -it('should correctly sort an input list that is unsorted', () => { - const array = [15, 24, 3, 2224, 1] - expect(combSort(array)).toEqual([1, 3, 15, 24, 2224]) -}) - -describe('Variations of input array lengths', () => { - it('should return an empty list with the input list is an empty list', () => { - expect(combSort([])).toEqual([]) +describe('combSort function', () => { + it('should correctly sort an input list that is sorted backwards', () => { + const array = [5, 4, 3, 2, 1] + expect(combSort(array)).toEqual([1, 2, 3, 4, 5]) }) - it('should correctly sort an input list of length 1', () => { - expect(combSort([100])).toEqual([100]) + it('should correctly sort an input list that is unsorted', () => { + const array = [15, 24, 3, 2224, 1] + expect(combSort(array)).toEqual([1, 3, 15, 24, 2224]) }) - it('should correctly sort an input list of an odd length', () => { - expect(combSort([101, -10, 321])).toEqual([-10, 101, 321]) + describe('Variations of input array lengths', () => { + it('should return an empty list with the input list is an empty list', () => { + expect(combSort([])).toEqual([]) + }) + + it('should correctly sort an input list of length 1', () => { + expect(combSort([100])).toEqual([100]) + }) + + it('should correctly sort an input list of an odd length', () => { + expect(combSort([101, -10, 321])).toEqual([-10, 101, 321]) + }) + + it('should correctly sort an input list of an even length', () => { + expect(combSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56]) + }) }) - it('should correctly sort an input list of an even length', () => { - expect(combSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56]) - }) -}) - -describe('Variations of input array elements', () => { - it('should correctly sort an input list that contains only positive numbers', () => { - expect(combSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) - }) - - it('should correctly sort an input list that contains only negative numbers', () => { - expect(combSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1]) - }) - - it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => { - expect(combSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56]) - }) - - it('should correctly sort an input list that contains only whole numbers', () => { - expect(combSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) - }) - - it('should correctly sort an input list that contains only decimal numbers', () => { - expect(combSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45]) - }) - - it('should correctly sort an input list that contains only a mix of whole and decimal', () => { - expect(combSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56]) - }) - - it('should correctly sort an input list that contains only fractional numbers', () => { - expect(combSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98]) - }) - - it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => { - expect(combSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12]) - }) - - it('should correctly sort an input list that contains duplicates', () => { - expect(combSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + describe('Variations of input array elements', () => { + it('should correctly sort an input list that contains only positive numbers', () => { + expect(combSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) + }) + + it('should correctly sort an input list that contains only negative numbers', () => { + expect(combSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1]) + }) + + it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => { + expect(combSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56]) + }) + + it('should correctly sort an input list that contains only whole numbers', () => { + expect(combSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) + }) + + it('should correctly sort an input list that contains only decimal numbers', () => { + expect(combSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45]) + }) + + it('should correctly sort an input list that contains only a mix of whole and decimal', () => { + expect(combSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56]) + }) + + it('should correctly sort an input list that contains only fractional numbers', () => { + expect(combSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98]) + }) + + it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => { + expect(combSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12]) + }) + + it('should correctly sort an input list that contains duplicates', () => { + expect(combSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + }) }) })