Skip to content

Instantly share code, notes, and snippets.

View iamamit-107's full-sized avatar
🎯
Focusing

Ahmed Faisal Amit iamamit-107

🎯
Focusing
View GitHub Profile
function insertionSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
function bubbleSort(arr) {
//sort the array
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const lesser = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = lesser;
}
}
function binarySearch(arr, item) {
let startIndex = 0;
let endIndex = arr.length - 1;
while (startIndex < endIndex) {
let middleIndex = Math.floor((startIndex + endIndex) / 2);
if (arr[middleIndex] === item) {
return `Found at index ${middleIndex}`;
}
function linearSearch(arr, item) {
for (const element of arr) {
if (element === item) {
return "Found";
}
}
return "Not Found";
}
function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
console.log(fib(6));
function fib(n) {
const result = [0, 1];
for (let i = 2; i <= n; i++) {
let a = result[i - 1];
let b = result[i - 2];
result.push(a + b);
}
function iterativeFactorial(num) {
if (num === 0 || num === 1) {
return 1;
}
for (let i = num - 1; i >= 1; i--) {
num = num * i;
}
return num;
function recursiveFactorial(num) {
if (num === 0 || num === 1) {
return 1;
}
return num * recursiveFactorial(num - 1);
}
console.log(recursiveFactorial(6));