From 71f86fccb78cef32b17c7ed2dd6c375a95bc79df Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 11:41:34 +0530 Subject: [PATCH 1/4] Added Quick Sort --- Sorts/QuickSort.ts | 64 ++++++++++++++++++++++++++++++++++++ Sorts/test/QuickSort.test.ts | 26 +++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 Sorts/QuickSort.ts create mode 100644 Sorts/test/QuickSort.test.ts diff --git a/Sorts/QuickSort.ts b/Sorts/QuickSort.ts new file mode 100644 index 00000000..cfcc8907 --- /dev/null +++ b/Sorts/QuickSort.ts @@ -0,0 +1,64 @@ +/** + * @function quickSort + * @description is an algorithm based on divide and conquer approach in which an array is split into sub-arrays and these sub arrays are recursively sorted to get final array + * @see [Quick Sort](https://www.javatpoint.com/quick-sort) + * @example QuickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + */ + + export const partition = ( + array: Array, + left: number = 0, + right: number = array.length - 1 +) => { + const pivot = array[Math.floor((right + left) / 2)]; + let i = left; + let j = right; + + while (i <= j) { + while (array[i] < pivot) { + i++; + } + + while (array[j] > pivot) { + j--; + } + + if (i <= j) { + [array[i], array[j]] = [array[j], array[i]]; + i++; + j--; + } + } + + return i; +}; + +/** + * Quicksort implementation + * + * @param {Array} array + * @param {number} [left=0] + * @param {number} [right=array.length - 1] + * @returns {Array} + */ +export const QuickSort = ( + array: Array, + left: number = 0, + right: number = array.length - 1 +) => { + let index; + + if (array.length > 1) { + index = partition(array, left, right); + + if (left < index - 1) { + QuickSort(array, left, index - 1); + } + + if (index < right) { + QuickSort(array, index, right); + } + } + + return array; +}; diff --git a/Sorts/test/QuickSort.test.ts b/Sorts/test/QuickSort.test.ts new file mode 100644 index 00000000..971b9f6b --- /dev/null +++ b/Sorts/test/QuickSort.test.ts @@ -0,0 +1,26 @@ +import { QuickSort } from "../QuickSort"; + +/** + * Space complexity - O(nlogn) + * Time complexity + * Best case - O(nlogn) + * When pivot element lies in the middle of the list + * Worst case - O(n^2) + * When pivot element lies on the extreme ends + * Average case - O(nlogn) + * When the above two cases are not met + */ + +describe("Quick Sort", () => { + it("should return the correct value for average case", () => { + expect(QuickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + }); + + it("should return the correct value for worst case", () => { + expect(QuickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + }); + + it("should return the correct value for best case", () => { + expect(QuickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + }); + }); \ No newline at end of file From af18e09a8b84ca748bf84f6ffad40f23e8537b2d Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 15:52:13 +0530 Subject: [PATCH 2/4] Refactored code --- Sorts/QuickSort.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sorts/QuickSort.ts b/Sorts/QuickSort.ts index cfcc8907..cdea4cad 100644 --- a/Sorts/QuickSort.ts +++ b/Sorts/QuickSort.ts @@ -5,8 +5,8 @@ * @example QuickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] */ - export const partition = ( - array: Array, +export const partition = ( + array: number[], left: number = 0, right: number = array.length - 1 ) => { @@ -39,10 +39,10 @@ * @param {Array} array * @param {number} [left=0] * @param {number} [right=array.length - 1] - * @returns {Array} + * @returns {number[]} */ export const QuickSort = ( - array: Array, + array: number[], left: number = 0, right: number = array.length - 1 ) => { From fd5ce9e71d779f33addd0a6a05658da9c351e72f Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 15:54:15 +0530 Subject: [PATCH 3/4] Fixed small typo --- Sorts/QuickSort.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/QuickSort.ts b/Sorts/QuickSort.ts index cdea4cad..e3116ee2 100644 --- a/Sorts/QuickSort.ts +++ b/Sorts/QuickSort.ts @@ -36,7 +36,7 @@ export const partition = ( /** * Quicksort implementation * - * @param {Array} array + * @param {number[]} array * @param {number} [left=0] * @param {number} [right=array.length - 1] * @returns {number[]} From b41f936659eca4c01fad00a9114fbef030e30cf2 Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 16:56:17 +0530 Subject: [PATCH 4/4] Refactored code --- Sorts/QuickSort.ts | 10 ++++++++++ Sorts/test/QuickSort.test.ts | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Sorts/QuickSort.ts b/Sorts/QuickSort.ts index e3116ee2..c0fd192e 100644 --- a/Sorts/QuickSort.ts +++ b/Sorts/QuickSort.ts @@ -40,7 +40,17 @@ export const partition = ( * @param {number} [left=0] * @param {number} [right=array.length - 1] * @returns {number[]} + * @complexity_analysis + * Space complexity - O(nlogn) + * Time complexity + * Best case - O(nlogn) + * When pivot element lies in the middle of the list + * Worst case - O(n^2) + * When pivot element lies on the extreme ends + * Average case - O(nlogn) + * When the above two cases are not met */ + export const QuickSort = ( array: number[], left: number = 0, diff --git a/Sorts/test/QuickSort.test.ts b/Sorts/test/QuickSort.test.ts index 971b9f6b..fbbbb67d 100644 --- a/Sorts/test/QuickSort.test.ts +++ b/Sorts/test/QuickSort.test.ts @@ -1,16 +1,5 @@ import { QuickSort } from "../QuickSort"; -/** - * Space complexity - O(nlogn) - * Time complexity - * Best case - O(nlogn) - * When pivot element lies in the middle of the list - * Worst case - O(n^2) - * When pivot element lies on the extreme ends - * Average case - O(nlogn) - * When the above two cases are not met - */ - describe("Quick Sort", () => { it("should return the correct value for average case", () => { expect(QuickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);