Skip to content

Instantly share code, notes, and snippets.

View fortunee's full-sized avatar
🎯
Focusing

Fortune Ekeruo fortunee

🎯
Focusing
View GitHub Profile
@fortunee
fortunee / fib.js
Created March 25, 2023 00:50
Fibonacci sequence
function fib(num){
if (num <= 2) return 1;
return fib(num - 1) + fib(num - 2);
}
fib(4);
@fortunee
fortunee / queue.js
Created March 25, 2023 00:48
Queue
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.size = 0;
@fortunee
fortunee / stack.js
Created March 25, 2023 00:47
Stack
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Stack {
constructor() {
this.first = null;
@fortunee
fortunee / sll.js
Created March 25, 2023 00:47
Singly Linked List
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class singlyLinkedList {
constructor() {
this.length = 0;
@fortunee
fortunee / dll.js
Created March 25, 2023 00:46
Doubly Linked List
class Node {
constructor(value) {
this.previous = null;
this.value = value;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
@fortunee
fortunee / radixSort.js
Last active April 6, 2023 02:09
Radix Sort basically uses a bucket of number from 0 to 9 and groups the numbers in a given array based on the number of digits in each number
function radixSort(nums) {
const maxDigitCount = mostDigits(nums);
for (let k = 0; k < maxDigitCount; k++) {
const digitsBucket = Array.from({ length: 10 }, () => []);
for (let i = 0; i < nums.length; i++) {
const currentNum = nums[i];
const digit = getDigit(currentNum, k);
@fortunee
fortunee / quickSort.js
Created March 14, 2023 17:52
Quick sort basically picks a pivot element in the given array, sorts it by moving larger items to the right and the smaller elements to the left of the pivot element
/**
* PSEUDOCODE
* - initialize the left argument to 0
* - initialize the right argument to array.length
* - use the pivot helper fn to get the pivot index pivot(array, right, left)
* - recursively call the quickSort fn on both the left and right side of the pivot index
* - ONLY if the left is < right index
* - hence base case should be if (left < right) { keep calling quickSort recursively }
* - when done return array
*/
@fortunee
fortunee / mergeSort.js
Last active March 14, 2023 01:39
Merge sort basically splits, sorts and merges the values in the array
/**
* PSEUDOCODE
* - Break up the array into halves until you have an empty array or array with one element
* - Once you have the smaller sorted arrays, merge those arrays with other sorted arrays until
* - you are back at the full length of the array
* - Once the array has been merged back together return the merged and sorted array
*/
function mergeSort(array) {
if (array.length <= 1) return array;
const middlePoint = Math.floor(array.length / 2);
@fortunee
fortunee / bubbleSort.js
Last active March 14, 2023 01:27
Bubble sort basically compares each element in the array and swaps them by moving the larger value towards the end of the array
/**
* PSEUDOCODE
* - start a loop that goes from the end to the start of the array;
* - start a second nested loop that goes from the start to the item below the
* - current position of the first loop;
* - compare each item with next item in the array
* - if current item is greater than the next item following it
* - then swap current item with next item
* - check if any swap happend at every complete iteration of the second loop
@fortunee
fortunee / selectionSort.js
Created March 10, 2023 05:31
Selection sort basically places the smaller values into a sorted position in the array by selecting lowest value and swapping it with item at the beginning
/**
* PSEUDOCODE
* - start a for loop
* - store the index of the first item in the array as the current minimum
* - start a second nested for loop which starts iterating from the next item
* - compare each item with the item at the stored minimum index
* - if any item is lower then reassign it to be the stored minimum
* - at the end of the of firt iteration for the second loop
* - then swap the item at stored minimum index with the item at the
* - current iteration of the outer for loop