From f715d353bfde5eeb09efc5346d51a9f31196b8d6 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 13:12:40 -0300 Subject: [PATCH 1/4] Create quickSort function --- src/sort/quick/quickSort.spec.ts | 15 +++++++++++++++ src/sort/quick/quickSort.ts | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/sort/quick/quickSort.spec.ts create mode 100644 src/sort/quick/quickSort.ts diff --git a/src/sort/quick/quickSort.spec.ts b/src/sort/quick/quickSort.spec.ts new file mode 100644 index 0000000..ebe087c --- /dev/null +++ b/src/sort/quick/quickSort.spec.ts @@ -0,0 +1,15 @@ +import quickSort from './quickSort'; + +describe('quick 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(quickSort(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(quickSort(arrayToSort, (a, b): boolean => (a > b))).toEqual(expectedArray); + }); +}); diff --git a/src/sort/quick/quickSort.ts b/src/sort/quick/quickSort.ts new file mode 100644 index 0000000..c44d3d8 --- /dev/null +++ b/src/sort/quick/quickSort.ts @@ -0,0 +1,25 @@ +export default function quickSort(array: number[], compareFunction:CompareFunction): number[] { + if (array.length < 2) { + return array; + } + const pivot = array[Math.floor(Math.random() * array.length)]; + + const left: number[] = []; + const right: number[] = []; + const equal: number[] = []; + + array.forEach((value) => { + if (compareFunction(value, pivot)) { + left.push(value); + } else if (!compareFunction(value, pivot)) { + right.push(value); + } else { + equal.push(value); + } + }); + return [ + ...quickSort(left, compareFunction), + ...equal, + ...quickSort(right, compareFunction), + ]; +} From 134e1bbc6c2e1a6b76166a453f8dbbc34c297301 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 13:19:34 -0300 Subject: [PATCH 2/4] Add interface --- src/sort/quick/quickSort.spec.ts | 6 ++++-- src/sort/quick/quickSort.ts | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sort/quick/quickSort.spec.ts b/src/sort/quick/quickSort.spec.ts index ebe087c..ed7e123 100644 --- a/src/sort/quick/quickSort.spec.ts +++ b/src/sort/quick/quickSort.spec.ts @@ -4,12 +4,14 @@ describe('quick 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(quickSort(arrayToSort, (a, b): boolean => (a < b))).toEqual(expectedArray); + expect(quickSort(arrayToSort, (a: number, b: number): 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(quickSort(arrayToSort, (a, b): boolean => (a > b))).toEqual(expectedArray); + expect(quickSort(arrayToSort, (a: number, b: number): boolean => (a > b))) + .toEqual(expectedArray); }); }); diff --git a/src/sort/quick/quickSort.ts b/src/sort/quick/quickSort.ts index c44d3d8..26f7a76 100644 --- a/src/sort/quick/quickSort.ts +++ b/src/sort/quick/quickSort.ts @@ -1,3 +1,7 @@ +interface CompareFunction { + (a: number, b: number): boolean; +} + export default function quickSort(array: number[], compareFunction:CompareFunction): number[] { if (array.length < 2) { return array; From 3f773ddb6a1640ed945699bef02b879564212a74 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 15:08:45 -0300 Subject: [PATCH 3/4] Fix uppercases in quicksort and improve tests --- src/sort/quick/quickSort.spec.ts | 18 ++++++++++++++---- src/sort/quick/quickSort.ts | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/sort/quick/quickSort.spec.ts b/src/sort/quick/quickSort.spec.ts index ed7e123..76f70c3 100644 --- a/src/sort/quick/quickSort.spec.ts +++ b/src/sort/quick/quickSort.spec.ts @@ -1,17 +1,27 @@ import quickSort from './quickSort'; +const sortLowToHigh = (a: number, b: number): boolean => (a < b); +const sortHighToLow = (a: number, b: number): boolean => (a > b); + describe('quick sort', () => { - it('It should return an array sorted from lowest to highest', () => { + it('should return an array with two elements sorted from lowest to highest', () => { + const arrayToSort = [65, 43]; + const expectedArray = [43, 65]; + expect(quickSort(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]; - expect(quickSort(arrayToSort, (a: number, b: number): boolean => (a < b))) + expect(quickSort(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(quickSort(arrayToSort, (a: number, b: number): boolean => (a > b))) + expect(quickSort(arrayToSort, sortHighToLow)) .toEqual(expectedArray); }); }); diff --git a/src/sort/quick/quickSort.ts b/src/sort/quick/quickSort.ts index 26f7a76..fe0fd36 100644 --- a/src/sort/quick/quickSort.ts +++ b/src/sort/quick/quickSort.ts @@ -2,7 +2,7 @@ interface CompareFunction { (a: number, b: number): boolean; } -export default function quickSort(array: number[], compareFunction:CompareFunction): number[] { +export default function quickSort(array: number[], compareFunction: CompareFunction): number[] { if (array.length < 2) { return array; } From 6ea0d7247bba7ec56dcc65648210fb3b2fe8d6d5 Mon Sep 17 00:00:00 2001 From: Hugo Venega Date: Thu, 18 Nov 2021 15:59:08 -0300 Subject: [PATCH 4/4] Add tests for quicksort function --- src/sort/quick/quickSort.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/sort/quick/quickSort.spec.ts b/src/sort/quick/quickSort.spec.ts index 76f70c3..c7c2fce 100644 --- a/src/sort/quick/quickSort.spec.ts +++ b/src/sort/quick/quickSort.spec.ts @@ -4,6 +4,20 @@ const sortLowToHigh = (a: number, b: number): boolean => (a < b); const sortHighToLow = (a: number, b: number): boolean => (a > b); describe('quick sort', () => { + it('should return an empty array when the array for sorting is empty', () => { + const arrayToSort: number[] = []; + const expectedArray: number[] = []; + expect(quickSort(arrayToSort, sortLowToHigh)) + .toEqual(expectedArray); + }); + + it('should return an array with the element', () => { + const arrayToSort: number[] = [14]; + const expectedArray: number[] = [14]; + expect(quickSort(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];