From f4814ba143c6fe6d8eed5afcf97f02a9d27be438 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:49:31 -0400 Subject: [PATCH 01/10] Fix typo --- Sorts/CycleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index c17be63b3..e5e9c1319 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -20,7 +20,7 @@ function cycleSort (list) { position++ } } - // if its the same continue + // if it is the same, continue if (position === cycleStart) { continue } From 720b7b054bf146bc669022d54096ce46ad43b645 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:49:54 -0400 Subject: [PATCH 02/10] Add function documentation to CycleSort --- Sorts/CycleSort.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index e5e9c1319..5e0f6eab5 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -8,6 +8,12 @@ * Wikipedia: https://en.wikipedia.org/wiki/Cycle_sort */ +/** + * cycleSort takes an input array of numbers and returns the array sorted in increasing order. + * + * @param {number[]} list An array of numbers to be sorted. + * @return {number[]} An array of numbers sorted in increasing order. + */ function cycleSort (list) { let writes = 0 for (let cycleStart = 0; cycleStart < list.length; cycleStart++) { From b3e6eed3cef3d82b5bb4989c0a2406698e847832 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:50:14 -0400 Subject: [PATCH 03/10] Add export to CycleSort for Jest testing --- Sorts/CycleSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index 5e0f6eab5..c70737364 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -71,3 +71,5 @@ console.log(array) console.log('- After Sort | Implementation of Cycle Sort -') console.log(cycleSort(array)) console.log('\n') + +export { cycleSort } From 0da3880955e0641b4d6b34782bd3892cb4b08802 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:53:03 -0400 Subject: [PATCH 04/10] Fix bug where cycleSort was not returning the sorted array --- Sorts/CycleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index c70737364..2ef689fcf 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -57,7 +57,7 @@ function cycleSort (list) { writes++ } } - return writes + return list } /** From 519f557899edb10580057afa8cd6b050e6c645cd Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:53:49 -0400 Subject: [PATCH 05/10] Add tests for CycleSort --- Sorts/test/CycleSort.test.js | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Sorts/test/CycleSort.test.js diff --git a/Sorts/test/CycleSort.test.js b/Sorts/test/CycleSort.test.js new file mode 100644 index 000000000..70acd57af --- /dev/null +++ b/Sorts/test/CycleSort.test.js @@ -0,0 +1,67 @@ +import { cycleSort } from '../CycleSort' + +it('should correctly sort an input list that is sorted backwards', () => { + const array = [5, 4, 3, 2, 1] + expect(cycleSort(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(cycleSort(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(cycleSort([])).toEqual([]) + }) + + it('should correctly sort an input list of length 1', () => { + expect(cycleSort([100])).toEqual([100]) + }) + + it('should correctly sort an input list of an odd length', () => { + expect(cycleSort([101, -10, 321])).toEqual([-10, 101, 321]) + }) + + it('should correctly sort an input list of an even length', () => { + expect(cycleSort([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(cycleSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) + }) + + it('should correctly sort an input list that contains only negative numbers', () => { + expect(cycleSort([-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(cycleSort([-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(cycleSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) + }) + + it('should correctly sort an input list that contains only decimal numbers', () => { + expect(cycleSort([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(cycleSort([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(cycleSort([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(cycleSort([-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(cycleSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + }) +}) From 8e5743f8cbb0b2667ab1ae447558faff51a96f82 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:54:59 -0400 Subject: [PATCH 06/10] Fix standardjs style errors --- Sorts/CycleSort.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index 2ef689fcf..eb612d9c3 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -15,7 +15,6 @@ * @return {number[]} An array of numbers sorted in increasing order. */ function cycleSort (list) { - let writes = 0 for (let cycleStart = 0; cycleStart < list.length; cycleStart++) { let value = list[cycleStart] let position = cycleStart @@ -30,7 +29,6 @@ function cycleSort (list) { if (position === cycleStart) { continue } - while (value === list[position]) { position++ } @@ -38,7 +36,6 @@ function cycleSort (list) { const oldValue = list[position] list[position] = value value = oldValue - writes++ // rotate the rest while (position !== cycleStart) { @@ -54,7 +51,6 @@ function cycleSort (list) { const oldValueCycle = list[position] list[position] = value value = oldValueCycle - writes++ } } return list From 932599d37b71f64c17579fcda1cd98033293987a Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 5 Oct 2021 08:54:13 -0400 Subject: [PATCH 07/10] Remove testing code in function file --- Sorts/CycleSort.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index eb612d9c3..1120b47ef 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -56,16 +56,4 @@ function cycleSort (list) { return list } -/** -* Implementation of Cycle Sort -*/ -const array = [5, 6, 7, 8, 1, 2, 12, 14] -// Before Sort -console.log('\n- Before Sort | Implementation of Cycle Sort -') -console.log(array) -// After Sort -console.log('- After Sort | Implementation of Cycle Sort -') -console.log(cycleSort(array)) -console.log('\n') - export { cycleSort } From 6bb075df925f2ce760885e85dbc57ca1a2af35f9 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 5 Oct 2021 17:25:22 -0400 Subject: [PATCH 08/10] Group tests in a describe --- Sorts/test/CycleSort.test.js | 116 ++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/Sorts/test/CycleSort.test.js b/Sorts/test/CycleSort.test.js index 70acd57af..d622875d0 100644 --- a/Sorts/test/CycleSort.test.js +++ b/Sorts/test/CycleSort.test.js @@ -1,67 +1,69 @@ import { cycleSort } from '../CycleSort' -it('should correctly sort an input list that is sorted backwards', () => { - const array = [5, 4, 3, 2, 1] - expect(cycleSort(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(cycleSort(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(cycleSort([])).toEqual([]) +describe('cycleSort function', () => { + it('should correctly sort an input list that is sorted backwards', () => { + const array = [5, 4, 3, 2, 1] + expect(cycleSort(array)).toEqual([1, 2, 3, 4, 5]) }) - it('should correctly sort an input list of length 1', () => { - expect(cycleSort([100])).toEqual([100]) + it('should correctly sort an input list that is unsorted', () => { + const array = [15, 24, 3, 2224, 1] + expect(cycleSort(array)).toEqual([1, 3, 15, 24, 2224]) }) - it('should correctly sort an input list of an odd length', () => { - expect(cycleSort([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(cycleSort([])).toEqual([]) + }) + + it('should correctly sort an input list of length 1', () => { + expect(cycleSort([100])).toEqual([100]) + }) + + it('should correctly sort an input list of an odd length', () => { + expect(cycleSort([101, -10, 321])).toEqual([-10, 101, 321]) + }) + + it('should correctly sort an input list of an even length', () => { + expect(cycleSort([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(cycleSort([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(cycleSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) - }) - - it('should correctly sort an input list that contains only negative numbers', () => { - expect(cycleSort([-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(cycleSort([-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(cycleSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) - }) - - it('should correctly sort an input list that contains only decimal numbers', () => { - expect(cycleSort([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(cycleSort([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(cycleSort([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(cycleSort([-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(cycleSort([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(cycleSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) + }) + + it('should correctly sort an input list that contains only negative numbers', () => { + expect(cycleSort([-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(cycleSort([-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(cycleSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) + }) + + it('should correctly sort an input list that contains only decimal numbers', () => { + expect(cycleSort([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(cycleSort([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(cycleSort([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(cycleSort([-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(cycleSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + }) }) }) From 5bb16d76aa2f28c8c4879cce1fca0a05a0085731 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 5 Oct 2021 21:25:52 +0000 Subject: [PATCH 09/10] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6ae9a6d30..a0650bbe3 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 + * [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/CycleSort.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 ee54c7176a5e5085ba2a8e37252cab0855d6db29 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 6 Oct 2021 11:45:15 +0000 Subject: [PATCH 10/10] Auto-update DIRECTORY.md --- DIRECTORY.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6d477ab28..b78ca5990 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -256,11 +256,6 @@ * [TimSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimSort.js) * [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js) * [WiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/WiggleSort.js) - * test - * [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/test/CycleSort.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)