From 34058a570fce80592da34abd8cea6fba61d2fa2d Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 13:34:25 -0300 Subject: [PATCH 1/4] add insertionSort --- src/sort/insertion/insertionSort.spec.ts | 15 +++++++++++++++ src/sort/insertion/insertionSort.ts | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/sort/insertion/insertionSort.spec.ts create mode 100644 src/sort/insertion/insertionSort.ts diff --git a/src/sort/insertion/insertionSort.spec.ts b/src/sort/insertion/insertionSort.spec.ts new file mode 100644 index 0000000..fdd7597 --- /dev/null +++ b/src/sort/insertion/insertionSort.spec.ts @@ -0,0 +1,15 @@ +import insertionSort from './insertionSort'; + +describe('Insertion sort', () => { + it('It should return an array sorted from lowest to highest', () => { + const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; + const expectedArray = [1, 9, 12, 21, 33, 43, 44, 65, 67]; + expect(insertionSort(arrayToSort, (a, b): boolean => (a > b))).toEqual(expectedArray); + }); + + it('It should return an array sorted from highest to lowest', () => { + const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; + const expectedArray = [67, 65, 44, 43, 33, 21, 12, 9, 1]; + expect(insertionSort(arrayToSort, (a, b): boolean => (a < b))).toEqual(expectedArray); + }); +}); diff --git a/src/sort/insertion/insertionSort.ts b/src/sort/insertion/insertionSort.ts new file mode 100644 index 0000000..cfcac2b --- /dev/null +++ b/src/sort/insertion/insertionSort.ts @@ -0,0 +1,17 @@ +interface CompareFunction { + (a: number, b: number): boolean; +} + +export default function insertionSort(array: number[], comfunction: CompareFunction): number[] { + const myArray = array; + for (let j = 0; j < myArray.length; j += 1) { + const key = myArray[j]; + let i = j - 1; + while (i >= 0 && comfunction(myArray[i], key)) { + myArray[i + 1] = myArray[i]; + i -= 1; + } + myArray[i + 1] = key; + } + return myArray; +} From 5b91e988f39abac2c9541e8cf393945d4b0bf8f6 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 15:20:17 -0300 Subject: [PATCH 2/4] Fix name function and improve tests names --- src/sort/insertion/insertionSort.spec.ts | 11 +++++++---- src/sort/insertion/insertionSort.ts | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/sort/insertion/insertionSort.spec.ts b/src/sort/insertion/insertionSort.spec.ts index fdd7597..9ad1e37 100644 --- a/src/sort/insertion/insertionSort.spec.ts +++ b/src/sort/insertion/insertionSort.spec.ts @@ -1,15 +1,18 @@ import insertionSort from './insertionSort'; +const sortLowToHigh = (a: number, b: number): boolean => (a > b); +const sortHighToLow = (a: number, b: number): boolean => (a < b); + describe('Insertion sort', () => { - it('It should return an array sorted from lowest to highest', () => { + it('should return an array sorted from lowest to highest', () => { const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; const expectedArray = [1, 9, 12, 21, 33, 43, 44, 65, 67]; - expect(insertionSort(arrayToSort, (a, b): boolean => (a > b))).toEqual(expectedArray); + expect(insertionSort(arrayToSort, sortLowToHigh)).toEqual(expectedArray); }); - it('It should return an array sorted from highest to lowest', () => { + it('should return an array sorted from highest to lowest', () => { const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; const expectedArray = [67, 65, 44, 43, 33, 21, 12, 9, 1]; - expect(insertionSort(arrayToSort, (a, b): boolean => (a < b))).toEqual(expectedArray); + expect(insertionSort(arrayToSort,sortHighToLow)).toEqual(expectedArray); }); }); diff --git a/src/sort/insertion/insertionSort.ts b/src/sort/insertion/insertionSort.ts index cfcac2b..6f7883b 100644 --- a/src/sort/insertion/insertionSort.ts +++ b/src/sort/insertion/insertionSort.ts @@ -2,12 +2,12 @@ interface CompareFunction { (a: number, b: number): boolean; } -export default function insertionSort(array: number[], comfunction: CompareFunction): number[] { +export default function insertionSort(array: number[], comparefunction: CompareFunction): number[] { const myArray = array; for (let j = 0; j < myArray.length; j += 1) { const key = myArray[j]; let i = j - 1; - while (i >= 0 && comfunction(myArray[i], key)) { + while (i >= 0 && comparefunction(myArray[i], key)) { myArray[i + 1] = myArray[i]; i -= 1; } From 8449c08c99b32a2c391c9e2128d3883d15d2e526 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 16:10:03 -0300 Subject: [PATCH 3/4] Add test for insertion sort function --- src/sort/insertion/insertionSort.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/sort/insertion/insertionSort.spec.ts b/src/sort/insertion/insertionSort.spec.ts index 9ad1e37..504cc24 100644 --- a/src/sort/insertion/insertionSort.spec.ts +++ b/src/sort/insertion/insertionSort.spec.ts @@ -4,6 +4,27 @@ const sortLowToHigh = (a: number, b: number): boolean => (a > b); const sortHighToLow = (a: number, b: number): boolean => (a < b); describe('Insertion sort', () => { + it('should return an empty array when the array for sorting is empty', () => { + const arrayToSort: number[] = []; + const expectedArray: number[] = []; + expect(insertionSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array with the element', () => { + const arrayToSort: number[] = [14]; + const expectedArray: number[] = [14]; + expect(insertionSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array with two elements sorted from lowest to highest', () => { + const arrayToSort = [65, 43]; + const expectedArray = [43, 65]; + expect(insertionSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + it('should return an array sorted from lowest to highest', () => { const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; const expectedArray = [1, 9, 12, 21, 33, 43, 44, 65, 67]; From 86c9dcc86417b350f0f9f707781a9516188ee2ae Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 16:15:12 -0300 Subject: [PATCH 4/4] Fix eslint problems from insertionsort tests --- src/sort/insertion/insertionSort.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sort/insertion/insertionSort.spec.ts b/src/sort/insertion/insertionSort.spec.ts index 504cc24..7f65509 100644 --- a/src/sort/insertion/insertionSort.spec.ts +++ b/src/sort/insertion/insertionSort.spec.ts @@ -34,6 +34,6 @@ describe('Insertion sort', () => { it('should return an array sorted from highest to lowest', () => { const arrayToSort = [43, 65, 44, 12, 67, 1, 9, 33, 21]; const expectedArray = [67, 65, 44, 43, 33, 21, 12, 9, 1]; - expect(insertionSort(arrayToSort,sortHighToLow)).toEqual(expectedArray); + expect(insertionSort(arrayToSort, sortHighToLow)).toEqual(expectedArray); }); });