Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions DSA/Arrays/SelectionSort.js
Original file line number Diff line number Diff line change
@@ -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))
let arr = [4, 12, 10, 15, 2];
console.log(selectionSort(arr)); // Output: [2, 4, 10, 12, 15]
30 changes: 20 additions & 10 deletions DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<nums.length; j++){
if(nums[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

};
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
};