Skip to content

Instantly share code, notes, and snippets.

View benfluleck's full-sized avatar

Benny Ogidan benfluleck

View GitHub Profile
@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 / 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 / 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 / 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 / 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) {