Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lab-nicholasc/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
26 changes: 26 additions & 0 deletions lab-nicholasc/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}
84 changes: 84 additions & 0 deletions lab-nicholasc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

# Created by https://www.gitignore.io/api/node,linux

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/
db/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
env

#db
db

# End of https://www.gitignore.io/api/node,linux
27 changes: 27 additions & 0 deletions lab-nicholasc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Lab 30 Heaps and Sorting

## Author: Nicholas Carignan

## Node Package dependencies
1. "eslint": "^4.12.1",
1. "jest": "^21.2.1"

## Setup
clone the repo by typing in

`git clone https://github.com/ncarignan/30-heaps-and-basic-sorting.git`

then install the packages with

`npm install`

then the app is ready to be tested by running

`npm run test`

## models
### insertion-sort
insertion sort exports a function that can be used to sort an array. The function takes in an array and then calls an insertion sort method in place on the array that sorts the values. The function expects the input to be an array or else it will throw an error. This function has an O(n) = nlog(n) where n is the length of the array. At its worst performance it has an O(n) = n^2 if it has to resort the entire array every time which is highly improbable.

### max-heap
max-heap exports a model of a max heap that creates a new MaxHeap with an empty data set when its constructor is called. MaxHeas contain an insert method that inserts a new value in the proper position. It expects a number as its input and will throw an error if it is not a number. It also contains an extractMaximum method that will take the maximum value out of the heap and restructure the heap properly. It also has a peek method that returns the maximum value of the heap.
22 changes: 22 additions & 0 deletions lab-nicholasc/__test__/insertion-sorttest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const insertionSort = require('../model/insertion-sort');

describe('insertionSort', () => {
let testArray = [9, 8, 5, 6, 3, 1, 4, 7, 2];
let sortedTestArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];



test('basic sort', () => {
expect(insertionSort(testArray)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

test('sorted in place', () => {
expect(insertionSort(sortedTestArray)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

test('not an array', () => {
expect(() => insertionSort('')).toThrow();
});
});
92 changes: 92 additions & 0 deletions lab-nicholasc/__test__/max-heaptest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

const MaxHeap = require('../model/max-heap');

describe('MaxHeap', () => {
let staticTestHeap = new MaxHeap();
staticTestHeap.insert(1);
staticTestHeap.insert(2);
staticTestHeap.insert(3);
staticTestHeap.insert(4);
staticTestHeap.insert(5);
staticTestHeap.insert(6);
staticTestHeap.insert(7);
staticTestHeap.insert(8);
staticTestHeap.insert(9);

describe('insert', () => {
test('inserts values', () =>{
let testHeap = new MaxHeap();
testHeap.insert(1);
testHeap.insert(2);
testHeap.insert(3);
expect(testHeap._data).toEqual([3, 1, 2]);
testHeap.insert(9);
expect(testHeap._data).toEqual([9, 3, 2, 1]);
testHeap.insert(8);
expect(testHeap._data).toEqual([9, 8, 2, 1, 3]);
});
test('insert NaN throws', () => {
let testHeap = new MaxHeap();
expect(() => testHeap.insert('five')).toThrow();
expect(() => testHeap.insert([1])).toThrow();
});
});

test('_getParentIndex', () => {
expect(staticTestHeap._getParentIndex(1)).toEqual(0);
expect(staticTestHeap._getParentIndex(5)).toEqual(2);
expect(staticTestHeap._getParentIndex(0)).toEqual(null);
});

test('_getLeftIndex', () => {
expect(staticTestHeap._getLeftIndex(1)).toEqual(3);
expect(staticTestHeap._getLeftIndex(3)).toEqual(7);
});

test('_getRightIndex', () => {
expect(staticTestHeap._getRightIndex(1)).toEqual(4);
expect(staticTestHeap._getRightIndex(3)).toEqual(8);
});

describe('peek', () => {

test('peek on valid heap', () => {
expect(staticTestHeap.peek()).toEqual(9);
});
test('peek on empty heap', () => {
let emptyHeap = new MaxHeap();
expect(emptyHeap.peek()).toEqual(null);
});
});

describe('extractMaximum', () => {
test('should extract the minimum if valid heap', () => {
let testHeap = new MaxHeap();
testHeap.insert(1);
testHeap.insert(2);
testHeap.insert(3);
testHeap.insert(9);
testHeap.insert(8);
testHeap.insert(6);
testHeap.insert(5);
testHeap.insert(4);
testHeap.insert(7);
expect(testHeap.extractMaximum()).toEqual(9);
expect(testHeap.extractMaximum()).toEqual(8);
expect(testHeap.extractMaximum()).toEqual(7);
expect(testHeap.extractMaximum()).toEqual(6);
expect(testHeap.extractMaximum()).toEqual(5);
expect(testHeap.extractMaximum()).toEqual(4);
expect(testHeap.extractMaximum()).toEqual(3);
expect(testHeap.extractMaximum()).toEqual(2);
expect(testHeap.extractMaximum()).toEqual(1);
});

test('should return null if empty data', () => {
let emptyHeap = new MaxHeap();
expect(emptyHeap.extractMaximum()).toEqual(null);
});
});

});
12 changes: 12 additions & 0 deletions lab-nicholasc/__test__/radix-lsd.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const radixSort = require('../model/radix-lsd');

describe('radixSort', () => {
let testArray = [5, 4, 3, 2, 1, 11, 15, 17, 55, 100, 99];
let sortedArray = [1, 2, 3, 4, 5, 11, 15, 17, 55, 99, 100];

test('should return array properly sorted', () => {
expect(radixSort(testArray, 10)).toEqual(sortedArray);
});
});
6 changes: 6 additions & 0 deletions lab-nicholasc/model/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const mergeSort = require('./merge-sort');
const quickSort = require('./quick-sort');

let testArray = [3,2,1,0];

console.log(quickSort(testArray));
21 changes: 21 additions & 0 deletions lab-nicholasc/model/insertion-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const insertionSort = function(array) {
if(typeof array !== 'object'){
throw new TypeError('input must be an array');
}
for(let i = 0; i < array.length; i++){
let insertion = array[i];
let comparisonInd = i - 1;

while(comparisonInd >= 0 && insertion < array[comparisonInd]){
array[comparisonInd + 1] = array[comparisonInd];
comparisonInd --;
}
comparisonInd++;
array[comparisonInd] = insertion;
}
return array;
};

module.exports = insertionSort;
90 changes: 90 additions & 0 deletions lab-nicholasc/model/max-heap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

class MaxHeap{
constructor(){
this._data = [];
}

_getParentIndex(index){
if(index === 0)
return null;

return Math.floor((index -1) / 2);
}

_getLeftIndex(index){
return (2 * index) + 1;
}

_getRightIndex(index){
return (2 * index) + 2;
}

// lg n
insert(value){
if(typeof value !== 'number')
throw new TypeError('__ERROR__ value should be numeric');

this._data.push(value);
this._bubbleUp(this._data.length -1);
}

_swapValues(indexA,indexB){
let tempSwapValue = this._data[indexA];

this._data[indexA] = this._data[indexB];
this._data[indexB] = tempSwapValue;
}

_bubbleUp(index){
if(this._getParentIndex(index) === null)
return;

let parentIndex = this._getParentIndex(index);

if(this._data[parentIndex] < this._data[index]){
this._swapValues(parentIndex, index);
this._bubbleUp(parentIndex);
}
}

_bubbleDown(index){
let maxIndex = index;
let leftIndex = this._getLeftIndex(index);
let rightIndex = this._getRightIndex(index);

if(leftIndex <= this._data.length -1){
if(this._data[maxIndex] < this._data[leftIndex])
maxIndex = leftIndex;
}
if(rightIndex <= this._data.length -1){
if(this._data[maxIndex] < this._data[rightIndex])
maxIndex = rightIndex;
}

if(maxIndex != index){
this._swapValues(index, maxIndex);
this._bubbleDown(maxIndex);
}
}

extractMaximum(){
if(this._data.length <= 0)
return null;

let max = this._data[0];
let lastValue = this._data.pop();
this._data[0] = lastValue;
this._bubbleDown(0);
return max;
}

peek(){
if(this._data.length <= 0)
return null;

return this._data[0];
}
}

module.exports = MaxHeap;
Loading