Skip to content

Instantly share code, notes, and snippets.

View MohdSaifulM's full-sized avatar
😎

Mohammad Saiful Bin Mohammad MohdSaifulM

😎
  • Singapore
View GitHub Profile
@MohdSaifulM
MohdSaifulM / insertionSort.js
Created December 19, 2022 16:59
Algorithms - Sorting (Insertion Sort)
/*
Insertion sort
- Time complexity O(n^2)
- Best use case if data is nearly sorted and if new random data is added to the array to be sorted
*/
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
// Pick second element in the array
let currentVal = arr[i];
@MohdSaifulM
MohdSaifulM / selectionSort.js
Created December 19, 2022 16:15
Algorithms - Sorting (Selection Sort)
/*
Selection sort
- Time complexity O(n^2)
- Best use case if we want to limit the swapping of elements
*/
function selectionSort(arr) {
for (let i = 0; i < arr.length; i++) {
// Store first element as the smallest value
let minIndex = i;
@MohdSaifulM
MohdSaifulM / bubbleSort.js
Created December 8, 2022 16:30
Algorithms - Sorting (Bubble Sort)
/*
Bubble sort
- Time complexity O(n^2)
- Best use case if array is almost sorted
*/
function bubbleSort(arr) {
// Swap flag for inner iteration
let swap = false;
// Iterate from end of array to beginning of the array to determine the end point of the inner iteration
@MohdSaifulM
MohdSaifulM / binarySearch.js
Last active December 8, 2022 16:32
Algorithms - Binary Search
/*
Binary search
- Time complexity O(logn)
- Array needs to be sorted for binary search to work
*/
function binarySearch(arr, target){
// Define left, right and middle pointers
let left = 0;
let right = arr.length - 1;
@MohdSaifulM
MohdSaifulM / nestedEvenSum.js
Created December 3, 2022 17:01
Algorithms - Recursion
function nestedEvenSum (input) {
let sum = 0;
// Helper function to iterate through objects
function iterateInnerObject (input) {
for (let x in input) {
// If value is number and even add to sum
if (typeof(input[x]) === 'number' && input[x] % 2 === 0) sum += input[x];
// If valus is object re run function
else if (typeof(input[x]) === 'object') iterateInnerObject(input[x])
}
@MohdSaifulM
MohdSaifulM / capitalizeFirst.js
Last active December 3, 2022 15:38
Algorithms - Recursion
function capitalizeFirst (arr) {
// Base case
if (arr.length === 1) return arr[0][0].toUpperCase() + arr[0].slice(1);
// Recursive case - return capitalize first elem of array concat with sliced arr
return [arr[0][0].toUpperCase() + arr[0].slice(1)].concat(capitalizeFirst(arr.slice(1)))
}
// capitalizeFirst(['car','taco','banana']); // ['Car','Taco','Banana']
@MohdSaifulM
MohdSaifulM / flatten.js
Created December 1, 2022 17:04
Algorithms - Recursion
function flatten(arr) {
let result = [];
// Iterate through elements in array
for (let i = 0; i < arr.length; i++) {
// Check if element is array
if (Array.isArray(arr[i])) {
// Recursive case - concat result with recursive function
result = result.concat(flatten(arr[i]));
} else {
@MohdSaifulM
MohdSaifulM / someRecursive.js
Created December 1, 2022 16:30
Algorithms - Recursion
function someRecursive(arr, fn) {
// Return false if arr is empty
if (arr.length === 0) return false;
// if function returns true on first element in array
if (fn(arr[0])) return true;
// else slice array and re run function
return someRecursive(arr.slice(1), fn);
}
// SAMPLE INPUT / OUTPUT
@MohdSaifulM
MohdSaifulM / isPalindrome.js
Created December 1, 2022 15:50
Algorithms - Recursion
function isPalindrome(str){
function reverseHelper(str) {
// Base case
if (str.length === 1) return str;
// Recursive case to reverse string
return reverseHelper(str.slice(1)) + str[0];
}
// Check if reverse of string is same as string
return reverseHelper(str) == str;
@MohdSaifulM
MohdSaifulM / reverse.js
Created November 30, 2022 17:06
Algorithms - Recursion
function reverse(str) {
// Base case
if (str.length === 0) return str;
// Recursive case (shift first letter to the end of string)
return reverse(str.slice(1)) + str[0];
}
reverse('awesome') // 'emosewa'
reverse('rithmschool') // 'loohcsmhtir'