Skip to content

Instantly share code, notes, and snippets.

View benfluleck's full-sized avatar

Benny Ogidan benfluleck

View GitHub Profile
@benfluleck
benfluleck / heapSort.js
Created April 24, 2019 22:40
Representation of a Heap Sort
const swap = (array, firstItemIndex, lastItemIndex) => {
const temp = array[firstItemIndex];
array[firstItemIndex] = array[lastItemIndex];
array[lastItemIndex] = temp;
}
const heapify = (heap, i, max) => {
let index, leftChild, righChild;
@benfluleck
benfluleck / quickSort.js
Last active April 27, 2019 21:48
Quick sort
const items = [5,3,7,6,2,9];
function swapNumbers(items, leftIndex, rightIndex){
const temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
}
function partition(items, left, right) {
const pivotNumber = items[Math.floor((right + left) / 2)]
while (left <= right) {
@benfluleck
benfluleck / insertionSort.js
Created April 21, 2019 21:02
Insertion Sort
function swapNumbers(items, leftIndex, rightIndex){
const temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
}
function insertionSort(array = []) {
for(let i = 0; i < array.length; i++) {
let currentIndex = i
while(currentIndex > 0 && array[currentIndex] < array[currentIndex - 1]) {
@benfluleck
benfluleck / mergeSort.js
Created April 21, 2019 22:23
MergeSort
function mergeSort (array= []) {
if (array.length == 1) return array
const middle = Math.floor(array.length / 2)
const leftHalf = array.slice(0, middle)
const rightHalf = array.slice(middle)
return mergeHalves(
mergeSort(leftHalf),
@benfluleck
benfluleck / breathFirstSearch.js
Created April 27, 2019 18:05
breathFirstSearch
function Node(value){
this.value = value
this.left = null;
this.right = null;
}
Node.prototype.toString = function() {
return this.value;
}
@benfluleck
benfluleck / depthFirstSearch.JS
Last active April 27, 2019 23:23
Depth First Search
function Node(value){
this.value = value
this.left = null;
this.right = null;
}
Node.prototype.toString = function() {
return this.value;
}
@benfluleck
benfluleck / singleResponsiblity.js
Last active April 28, 2019 00:03
Single Responsibility
// Bad Case
class Staff {
constructor(staff) {
this.staff = staff;
}
registerStaff(staff) {
if (this.validateUser(staff)) {
return `${staff} has been created`;
}
@benfluleck
benfluleck / Bird.js
Last active April 28, 2019 15:12
Open Closed Principle
// Bad
class Bird{}
class FlyingBird extends Bird {
constructor(type, name){
super();
this.name = name
}
flightSpeed(){
if(this.name === "parrot" || this.name === "dove"){
return ('I can fly medium-fast')
@benfluleck
benfluleck / Bird.js
Last active April 28, 2019 15:19
Liskov Substitution Principle
// bad
class Bird{
fly(){
console.log('I can fly')
}
}
class Duck extends Bird {
constructor(){
super();
@benfluleck
benfluleck / posts.js
Created April 28, 2019 16:01
Interface segregation principle
// bad
class Post {
constructor(){
}
editPost(){
console.log('Post edited')
}
createPost(){