Skip to content

Commit bdddc00

Browse files
initial algos
0 parents  commit bdddc00

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Search algorithms/binary search.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const arr = [
2+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
3+
];
4+
5+
const binarySearch = (arr, target) => {
6+
let left = 0;
7+
let right = arr.length - 1;
8+
while (left <= right) {
9+
let mid = left + (right - left / 2);
10+
if (arr[mid] === target) {
11+
return "Element found! " + arr[mid];
12+
} else if (target < arr[mid]) {
13+
right = mid - 1;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
return false;
19+
};
20+
21+
console.log(binarySearch(arr, 19));

Search algorithms/sumTwoNumbers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const arr = [
2+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
3+
];
4+
5+
const searchNumbers = (array, target) => {
6+
let left = 0;
7+
let rigth = array.length - 1;
8+
let temp = 0;
9+
let showcase;
10+
while (left <= rigth) {
11+
let mid = left + (rigth - left / 2);
12+
showcase = mid;
13+
if (array[mid] + array[temp] === target) {
14+
return [mid, temp];
15+
} else if (target < array[mid] + array[temp]) {
16+
rigth = mid - 1;
17+
temp = mid;
18+
} else {
19+
left = mid + 1;
20+
temp = mid;
21+
}
22+
}
23+
let possible = (temp + showcase);
24+
return "Not found combination " + array[possible] + " " + possible;
25+
};
26+
27+
console.log(searchNumbers(arr, 18));
28+
// let i = arr[4] + arr[4] - 1;
29+
// console.log(arr[i]);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const nums = [9231, 123123, 5674, 2344, 53, 457, 8982, 93, 3304];
2+
3+
//Ascending order
4+
5+
// console.log(nums);
6+
7+
// for (let i = 0; i < nums.length; i++) {
8+
// for (let j = i + 1; j < nums.length; j++) {
9+
// if (nums[j] < nums[i]) {
10+
// let temp = nums[i];
11+
// nums[i] = nums[j];
12+
// nums[j] = temp;
13+
// }
14+
// }
15+
// }
16+
17+
// console.log(nums);
18+
19+
//Descending order
20+
21+
// console.log(nums);
22+
23+
// for (let i = 0; i < nums.length; i++) {
24+
// for (let j = i + 1; j < nums.length; j++) {
25+
// if (nums[j] > nums[i]) {
26+
// let temp = nums[i];
27+
// nums[i] = nums[j];
28+
// nums[j] = temp;
29+
// }
30+
// }
31+
// }
32+
33+
// console.log(nums);

0 commit comments

Comments
 (0)