From 1a3af77f5675aca2c675b2def3f1aed82160bfc8 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sat, 18 Sep 2021 23:33:16 +0530 Subject: [PATCH 1/4] add QuickSortRecursive method --- Sorts/QuickSortRecursive.js | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Sorts/QuickSortRecursive.js diff --git a/Sorts/QuickSortRecursive.js b/Sorts/QuickSortRecursive.js new file mode 100644 index 0000000000..d326398e03 --- /dev/null +++ b/Sorts/QuickSortRecursive.js @@ -0,0 +1,55 @@ +/* + Quicksort is the most popular sorting algorithm and there have + lots of different implementations but the "recursive" or "Partition in place" + is one of the most efficient implementations below we have discussed how to + implement it. + + Partition in place => "in place" Partition in place indicates that we + do not need any other space to store the auxiliary array and the term + "partition" denotes that we split the list into two parts one is less + than the pivot and the other is greater than the pivot and repeats this + process recursively and breaks the problem into sub-problems and makes + it singular so that the behavior or "divide and conquer" get involved + too. + + Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html +*/ + +/** + * Partition in place QuickSort. + * @param {number[]} inputList list of values. + * @param {number} low lower index for partition. + * @param {number} high higher index for partition. + */ +const quickSort = (inputList, low, high) => { + if (low < high) { + // get the partition index. + const pIndex = partition(inputList, low, high) + // recursively call the quickSort method again. + quickSort(inputList, low, pIndex - 1) + quickSort(inputList, pIndex + 1, high) + } +} + +/** + * Partition In Place method. + * @param {number[]} partitionList list for partiting. + * @param {number} low lower index for partition. + * @param {number} high higher index for partition. + * @returns {number} `pIndex` pivot index value. + */ +const partition = (partitionList, low, high) => { + const pivot = partitionList[high] + let pIndex = low + for (let index = low; index <= high - 1; index++) { + if (partitionList[index] < pivot) { + // swap variables using array destructuring + [partitionList[index], partitionList[pIndex]] = [partitionList[pIndex], partitionList[index]] + pIndex += 1 + } + } + [partitionList[pIndex], partitionList[high]] = [partitionList[high], partitionList[pIndex]] + return pIndex +} + +export { quickSort } From c96e86474922d305f42a36d597a678946655dee1 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sun, 19 Sep 2021 16:18:50 +0530 Subject: [PATCH 2/4] added an exception for test cases --- Sorts/QuickSortRecursive.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sorts/QuickSortRecursive.js b/Sorts/QuickSortRecursive.js index d326398e03..850a277333 100644 --- a/Sorts/QuickSortRecursive.js +++ b/Sorts/QuickSortRecursive.js @@ -22,6 +22,9 @@ * @param {number} high higher index for partition. */ const quickSort = (inputList, low, high) => { + if (!Array.isArray(inputList)) { + throw new TypeError('Please input a valid list or array.') + } if (low < high) { // get the partition index. const pIndex = partition(inputList, low, high) From fc97205855d5071edff2be565c47a9f649f908b1 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sun, 19 Sep 2021 16:56:00 +0530 Subject: [PATCH 3/4] fix for test cases --- Sorts/QuickSortRecursive.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sorts/QuickSortRecursive.js b/Sorts/QuickSortRecursive.js index 850a277333..1ab3f4a8eb 100644 --- a/Sorts/QuickSortRecursive.js +++ b/Sorts/QuickSortRecursive.js @@ -32,6 +32,7 @@ const quickSort = (inputList, low, high) => { quickSort(inputList, low, pIndex - 1) quickSort(inputList, pIndex + 1, high) } + return inputList } /** From 2a257b071d523b17d423b7e3d059c9d683b1f144 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sun, 19 Sep 2021 16:59:24 +0530 Subject: [PATCH 4/4] added test cases for QuickSortRecursive --- Sorts/QuickSortRecursive.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Sorts/QuickSortRecursive.test.js diff --git a/Sorts/QuickSortRecursive.test.js b/Sorts/QuickSortRecursive.test.js new file mode 100644 index 0000000000..d82299aa35 --- /dev/null +++ b/Sorts/QuickSortRecursive.test.js @@ -0,0 +1,21 @@ +import { quickSort } from './QuickSortRecursive' + +describe('QuickSortRecursive | Partition In Place Method', () => { + it('Expectedly, throw some error if we pass a non-array input', () => { + expect(() => quickSort('xyz', 0, 2)).toThrow('Please input a valid list or array.') + expect(() => quickSort(null, 0, 4)).toThrow('Please input a valid list or array.') + expect(() => quickSort(55, 0, 2)).toThrow('Please input a valid list or array.') + }) + + it('Expectedly, the quickSort method will sort the unsorted list in ascending order', () => { + const unSortArray = [5, 9, 3, 4, 6, 2, 0, 1, 7, 8] + const sortedExpectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + expect(quickSort(unSortArray, 0, unSortArray.length - 1)).toEqual(sortedExpectedArray) + }) + + it('Expectedly, the quickSort method will arrange the list of character values in dictionary order.', () => { + const unSortList = ['d', 'e', 'c', 'a', 'f', 'b'] + const sortedExpectedList = ['a', 'b', 'c', 'd', 'e', 'f'] + expect(quickSort(unSortList, 0, unSortList.length - 1)).toEqual(sortedExpectedList) + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy