From fc3e55f431e7807a24c6b8c4f53559fe3242e51d Mon Sep 17 00:00:00 2001 From: KRISH SONI <67964054+krishvsoni@users.noreply.github.com> Date: Sat, 28 Oct 2023 13:11:50 +0530 Subject: [PATCH 1/2] modified SelectionSort.js --- DSA/Arrays/SelectionSort.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/DSA/Arrays/SelectionSort.js b/DSA/Arrays/SelectionSort.js index 76f4cbb..b1809bf 100644 --- a/DSA/Arrays/SelectionSort.js +++ b/DSA/Arrays/SelectionSort.js @@ -1,18 +1,17 @@ function selectionSort(arr) { - for (let i = 0; i < arr.length - 1; i++) { let min_index = i; for (let j = i + 1; j < arr.length; j++) { - if (arr[j] > arr[min_index]) { + if (arr[j] < arr[min_index]) { // Changed the comparison to sort in ascending order min_index = j; } } - let temp = arr[min_index] - arr[min_index] = arr[i] + let temp = arr[min_index]; + arr[min_index] = arr[i]; arr[i] = temp; } - return arr + return arr; } -let arr = [4, 12, 10, 15, 2] -console.log(selectionSort(arr)) \ No newline at end of file +let arr = [4, 12, 10, 15, 2]; +console.log(selectionSort(arr)); // Output: [2, 4, 10, 12, 15] From 0582f5939fe09861d83dcbd913353cd4dd317150 Mon Sep 17 00:00:00 2001 From: KRISH SONI <67964054+krishvsoni@users.noreply.github.com> Date: Sat, 28 Oct 2023 13:19:57 +0530 Subject: [PATCH 2/2] added new functions --- ...alArrayWithXElementsGreaterThanEqualToX.js | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js b/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js index 5e995a2..7f83952 100644 --- a/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js +++ b/DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js @@ -2,18 +2,28 @@ * @param {number[]} nums * @return {number} */ - var specialArray = function(nums) { - for(let i=0; i<=nums.length; i++){ +var specialArray = function(nums) { + for (let i = 0; i <= nums.length; i++) { let count = 0; - for(let j=0; j= i){ - count++ + for (let j = 0; j < nums.length; j++) { + if (nums[j] >= i) { + count++; } } - if(count === i){ - return i + if (count === i) { + return i; } } - return -1 - -}; \ No newline at end of file + return -1; +}; + +// Define an additional function to find the maximum value in the array. +function findMaxValue(nums) { + return Math.max(...nums); +} + +// Export the specialArray function and the findMaxValue function. +module.exports = { + specialArray, + findMaxValue +};