From 35f9f4540c075a2663b8f628fd4f14cc8d579a46 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 10:44:40 -0300 Subject: [PATCH 1/3] Create shellSort function --- src/sort/shell/shellSort.spec.ts | 9 +++++++++ src/sort/shell/shellSort.ts | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/sort/shell/shellSort.spec.ts create mode 100644 src/sort/shell/shellSort.ts diff --git a/src/sort/shell/shellSort.spec.ts b/src/sort/shell/shellSort.spec.ts new file mode 100644 index 0000000..ddd94b1 --- /dev/null +++ b/src/sort/shell/shellSort.spec.ts @@ -0,0 +1,9 @@ +import shellSort from './shellSort'; + +describe('shell 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(shellSort(arrayToSort)).toEqual(expectedArray); + }); +}); diff --git a/src/sort/shell/shellSort.ts b/src/sort/shell/shellSort.ts new file mode 100644 index 0000000..05483f3 --- /dev/null +++ b/src/sort/shell/shellSort.ts @@ -0,0 +1,19 @@ +export default function shellSort(array: number[]): number[] { + const introducedArray = array; + const n = introducedArray.length; + + for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) { + for (let i = gap; i < n; i += 1) { + const temp = introducedArray[i]; + + let j; + for (j = i; j >= gap && introducedArray[j - gap] > temp; j -= gap) { + introducedArray[j] = introducedArray[j - gap]; + } + + introducedArray[j] = temp; + } + } + + return introducedArray; +} From e0ba5b333f3bd81c77e581c57fdf6c7c8ec7d195 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 15:13:39 -0300 Subject: [PATCH 2/3] Improve tests of shellSort function --- src/sort/shell/shellSort.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sort/shell/shellSort.spec.ts b/src/sort/shell/shellSort.spec.ts index ddd94b1..6ba2187 100644 --- a/src/sort/shell/shellSort.spec.ts +++ b/src/sort/shell/shellSort.spec.ts @@ -1,7 +1,7 @@ import shellSort from './shellSort'; describe('shell 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(shellSort(arrayToSort)).toEqual(expectedArray); From 5439c1d70a5c41a1aedf0e46df4fc3d03f538df9 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 16:12:51 -0300 Subject: [PATCH 3/3] Add tests for shellsort function --- src/sort/shell/shellSort.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/sort/shell/shellSort.spec.ts b/src/sort/shell/shellSort.spec.ts index 6ba2187..d192374 100644 --- a/src/sort/shell/shellSort.spec.ts +++ b/src/sort/shell/shellSort.spec.ts @@ -1,6 +1,27 @@ import shellSort from './shellSort'; describe('shell sort', () => { + it('should return an empty array when the array for sorting is empty', () => { + const arrayToSort: number[] = []; + const expectedArray: number[] = []; + expect(shellSort(arrayToSort)) + .toEqual(expectedArray); + }); + + it('should return an array with the element', () => { + const arrayToSort: number[] = [14]; + const expectedArray: number[] = [14]; + expect(shellSort(arrayToSort)) + .toEqual(expectedArray); + }); + + it('should return an array with two elements sorted from lowest to highest', () => { + const arrayToSort = [65, 43]; + const expectedArray = [43, 65]; + expect(shellSort(arrayToSort)) + .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];