Skip to content

Commit fe62b2f

Browse files
authored
modified SelectionSort.js (#95)
* modified SelectionSort.js * added new functions
1 parent 0258aa4 commit fe62b2f

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

DSA/Arrays/SelectionSort.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
function selectionSort(arr) {
2-
32
for (let i = 0; i < arr.length - 1; i++) {
43
let min_index = i;
54
for (let j = i + 1; j < arr.length; j++) {
6-
if (arr[j] > arr[min_index]) {
5+
if (arr[j] < arr[min_index]) { // Changed the comparison to sort in ascending order
76
min_index = j;
87
}
98
}
10-
let temp = arr[min_index]
11-
arr[min_index] = arr[i]
9+
let temp = arr[min_index];
10+
arr[min_index] = arr[i];
1211
arr[i] = temp;
1312
}
14-
return arr
13+
return arr;
1514
}
1615

17-
let arr = [4, 12, 10, 15, 2]
18-
console.log(selectionSort(arr))
16+
let arr = [4, 12, 10, 15, 2];
17+
console.log(selectionSort(arr)); // Output: [2, 4, 10, 12, 15]

DSA/Arrays/SpecialArrayWithXElementsGreaterThanEqualToX.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
var specialArray = function(nums) {
6-
for(let i=0; i<=nums.length; i++){
5+
var specialArray = function(nums) {
6+
for (let i = 0; i <= nums.length; i++) {
77
let count = 0;
8-
for(let j=0; j<nums.length; j++){
9-
if(nums[j] >= i){
10-
count++
8+
for (let j = 0; j < nums.length; j++) {
9+
if (nums[j] >= i) {
10+
count++;
1111
}
1212
}
13-
if(count === i){
14-
return i
13+
if (count === i) {
14+
return i;
1515
}
1616
}
17-
return -1
18-
19-
};
17+
return -1;
18+
};
19+
20+
// Define an additional function to find the maximum value in the array.
21+
function findMaxValue(nums) {
22+
return Math.max(...nums);
23+
}
24+
25+
// Export the specialArray function and the findMaxValue function.
26+
module.exports = {
27+
specialArray,
28+
findMaxValue
29+
};

0 commit comments

Comments
 (0)