Skip to content

Instantly share code, notes, and snippets.

View dlucidone's full-sized avatar

Ravi dlucidone

View GitHub Profile
function quicksort(array) {
if (array.length <= 1) {
return array;
}
var pivot = array[0];
var left = [];
var right = [];
@dlucidone
dlucidone / mergeSort.js
Created March 8, 2019 11:17
Merge Sort
// Split the array into halves and merge them recursively
function mergeSort (arr) {
if (arr.length === 1) {
// return once we hit an array with a single item
return arr
}
const middle = Math.floor(arr.length / 2) // get the middle item of the array rounded down
const left = arr.slice(0, middle) // items on the left side
const right = arr.slice(middle) // items on the right side
@dlucidone
dlucidone / insertionSort.js
Created March 8, 2019 11:16
Insertion Sort
const insertionSort = a => {
var n = a.length;
for(var i=1;i<n;i++){
var key = a[i];
var j = i-1;
while(j>=0 && a[j]>key){
a[j+1] = a[j];
j=j-1;
@dlucidone
dlucidone / deepClone.js
Created March 8, 2019 09:15
Deep Clone Object
function cloneObject(obj) {
var clone = {};
for(var i in obj) {
if(typeof(obj[i])=="object" && obj[i] != null)
clone[i] = cloneObject(obj[i]);
else
clone[i] = obj[i];
}
return clone;
}
@dlucidone
dlucidone / tripplets.js
Created February 18, 2019 05:08
tripplets sum
// Time complexity: O(n^2)
function findTriplets(arr, n) {
arr.sort();
var l = arr.length;
for (var i = 0; i < l; i++) {
var j = i + 1,
k = l - 1;
while (j < k) {
if (arr[i] + arr[j] + arr[k] < n) {
j++;
@dlucidone
dlucidone / memoized function.js
Created February 12, 2019 03:30
memoized function
// a simple pure function to get a value adding 10
const add = (n) => (n + 10);
console.log('Simple call', add(3));
// a simple memoize function that takes in a function
// and returns a memoized function
const memoize = (fn) => {
let cache = {};
return (...args) => {
let n = args[0]; // just taking one argument here
if (n in cache) {
@dlucidone
dlucidone / extend.js
Created February 8, 2019 03:59
extend implement
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
@dlucidone
dlucidone / bind.js
Last active February 8, 2019 04:31
JS Bind Implementation
Function.prototype.bind1 = function (scope) {
let fn = this
let prefixArgs = Array.prototype.slice.call(arguments, 1)
return function() {
let suffixArgs = Array.prototype.slice.call(arguments)
let args = prefixArgs.concat(suffixArgs)
return fn.apply(scope, args)
}
}
@dlucidone
dlucidone / apply.js
Created February 6, 2019 16:32
Apply Implementation
Function.prototype.myApply = function(){
if(arguments[0]==null||arguments[0]==this){
return this.bind(...arguments[1])();
}
else{
return this.bind(...arguments[0])();
}
}
@dlucidone
dlucidone / call.js
Last active February 8, 2019 04:35
JS Bind Implementation
Function.prototype.myCall = function(){
return this.bind(...arguments)();
}
ex-
function showProfileMessage(message) {
console.log(message, this.name);
}
const obj = {
name: "Ankur Anand"