From 22399def5e83ca7a9a128a1236d0b6c01f880b1d Mon Sep 17 00:00:00 2001 From: MayankSharma-2812 Date: Thu, 22 Jan 2026 11:55:40 +0530 Subject: [PATCH 1/4] Improve N-Queens input validation and solution counting clarity --- Backtracking/NQueens.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Backtracking/NQueens.js b/Backtracking/NQueens.js index 307c66dc04..18daad5611 100644 --- a/Backtracking/NQueens.js +++ b/Backtracking/NQueens.js @@ -1,8 +1,9 @@ class NQueens { constructor(size) { - if (size < 0) { - throw RangeError('Invalid board size') + if (size <= 0) { + throw RangeError('Board size must be a positive integer') } + this.board = new Array(size).fill('.').map(() => new Array(size).fill('.')) this.size = size this.solutionCount = 0 @@ -40,7 +41,7 @@ class NQueens { solve(col = 0) { if (col >= this.size) { this.solutionCount++ - return true + return } for (let i = 0; i < this.size; i++) { @@ -50,8 +51,6 @@ class NQueens { this.removeQueen(i, col) } } - - return false } printBoard(output = (value) => console.log(value)) { From a52354a1d6d170d1e59fa041cd389aaa142fe81b Mon Sep 17 00:00:00 2001 From: MayankSharma-2812 Date: Fri, 23 Jan 2026 09:46:44 +0530 Subject: [PATCH 2/4] Add input validation for empty or invalid array in binary search --- Search/BinarySearch.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index c5477cb7b9..475718129c 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -8,44 +8,46 @@ */ function binarySearchRecursive(arr, x, low = 0, high = arr.length - 1) { + if (!Array.isArray(arr) || arr.length === 0) { + return -1 + } + const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { - // item found => return its index return mid } if (x < arr[mid]) { - // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid return binarySearchRecursive(arr, x, low, mid - 1) } else { - // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high return binarySearchRecursive(arr, x, mid + 1, high) } - } else { - // if low > high => we have searched the whole array without finding the item - return -1 } + + return -1 } + function binarySearchIterative(arr, x, low = 0, high = arr.length - 1) { + if (!Array.isArray(arr) || arr.length === 0) { + return -1 + } + while (high >= low) { const mid = Math.floor(low + (high - low) / 2) if (arr[mid] === x) { - // item found => return its index return mid } if (x < arr[mid]) { - // arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid high = mid - 1 } else { - // arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high low = mid + 1 } } - // if low > high => we have searched the whole array without finding the item + return -1 } From da50af39f6e4c814cf91ccb80f9312a4246f38f8 Mon Sep 17 00:00:00 2001 From: MayankSharma-2812 Date: Sat, 24 Jan 2026 10:40:17 +0530 Subject: [PATCH 3/4] Add Quick Sort implementation in JavaScript --- Sorts/QuickSort.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Sorts/QuickSort.js b/Sorts/QuickSort.js index 3885054e36..c76adb6dcd 100644 --- a/Sorts/QuickSort.js +++ b/Sorts/QuickSort.js @@ -1,30 +1,28 @@ /** - * @function QuickSort - * @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy. - * @param {Integer[]} items - Array of integers - * @return {Integer[]} - Sorted array. - * @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort) + * @function quickSort + * @description Quick Sort is a divide-and-conquer comparison-based sorting algorithm. + * @param {number[]} items - Array of integers + * @returns {number[]} - Sorted array + * @timecomplexity Average: O(n log n), Worst: O(n²) + * @spacecomplexity O(n) + * @see https://en.wikipedia.org/wiki/Quicksort */ function quickSort(items) { - const length = items.length + if (items.length <= 1) return items - if (length <= 1) { - return items - } - const PIVOT = items[0] - const GREATER = [] - const LESSER = [] + const pivotIndex = Math.floor(items.length / 2) + const pivot = items[pivotIndex] + + const lesser = [] + const greater = [] - for (let i = 1; i < length; i++) { - if (items[i] > PIVOT) { - GREATER.push(items[i]) - } else { - LESSER.push(items[i]) - } + for (let i = 0; i < items.length; i++) { + if (i === pivotIndex) continue + if (items[i] < pivot) lesser.push(items[i]) + else greater.push(items[i]) } - const sorted = [...quickSort(LESSER), PIVOT, ...quickSort(GREATER)] - return sorted + return [...quickSort(lesser), pivot, ...quickSort(greater)] } export { quickSort } From b135a5156652c5a8ad22324e065a8e8e73f40293 Mon Sep 17 00:00:00 2001 From: MayankSharma-2812 Date: Sun, 25 Jan 2026 18:24:53 +0530 Subject: [PATCH 4/4] Handle empty array input in selection sort --- Sorts/SelectionSort.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index 4e183be59a..b2b8f5749b 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -12,7 +12,13 @@ export const selectionSort = (list) => { if (!Array.isArray(list)) { throw new TypeError('Given input is not an array') } - const items = [...list] // We don't want to modify the original array + + if (list.length === 0) { + return [] + } + + const items = [...list] + const length = items.length for (let i = 0; i < length - 1; i++) { if (typeof items[i] !== 'number') {