This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
NewerOlder