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-catherine/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
27 changes: 27 additions & 0 deletions lab-catherine/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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 }],
"space-infix-ops": ["error", {"int32Hint": false}],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}
105 changes: 105 additions & 0 deletions lab-catherine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

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

build

db

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### 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/

# 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


### VisualStudioCode ###
.vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history

# End of https://www.gitignore.io/api/node,macos,visualstudiocode
98 changes: 98 additions & 0 deletions lab-catherine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Code Fellows: Seattle 401 JavaScript - 401d19

## Lab 39: Sorting Algorithms

### Author:
Catherine Looper

### Motivation

### Build

#### Mutate Merge Sort's Input Array

The Merge Sort method accepts an array of items to be sorted. In this implementation, the input array is mutated and returned as a sorted array using a divide and conquer approach. The input array is divided into two arrays (split at the middle point). The values in each array are compared and sorted and ultimately pushed back together into the final sorted array. The Big O of time for this sort method is O(n log(n)) and Big O of space is O(log(n)).

```
test('testing that mergeSort returns a sorted array', () => {
let arrayToSort = [8, 1, 5, 7, 4, 9, 2, 3, 6];
expect(mergeSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});
```

#### Heap Sort

The Heap Sort method accepts an array of items to be sorted. This method also relies on a Max Heap to perform `insert()` and `extractMaximum()` functions. The Heap Sort inserts all of the values into the maximumHeap while removing them from the array. Once all items in the array have been inserted, then each value is removed from the heap using the `extractMaximum()` method and using, `unshift`, are re-inserted back into the beginning of the array. The Big O of time for this sort method is O(n log(n)) and Big O of space is O(log(n)) where n is the height of the heap.

```
test('testing that heapSort returns a sorted array', () => {
let arrayToSort = [8, 1, 5, 7, 4, 9, 2, 3, 6];
expect(heapSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});
```

#### Test Functions

In this project, I implemented a test helper function that allows the project to be tested using a random array generator and a validate array method. To use the test helper function, it simply must be required in at the top of the test file:

```const testFunction = require('./test-function');```

The test function:

```
'use strict';

const testFunction = module.exports = {};

testFunction.generateRandomArray = (length, max) => {
return [...new Array(length)]
.map(() => Math.round(Math.random() * max));
};

testFunction.validateArray = (array) => {
for(let i = 0; i < array.length - 1; i++) {
if(array[i] < array[i + 1])
return true;
}
};
```

An example of a test:

```
test('testing that large array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(10000, 10000);
let arrayToValidate = testFunction.validateArray(heapSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});
});
```

### Limitations

To use this app - it is assumed that the user has familiarity with the tech and frameworks listed below.

### Code Style

Standard JavaScript with ES6.

### Tech/Framework Used

* eslint
* jest

### How to use?

* Step 1. Fork and Clone the Repository.
* Step 2. `npm install`
* Step 3. To test this application: `npm run test`


### Credits

* Code Fellows

### License

MIT © Catherine Looper

54 changes: 54 additions & 0 deletions lab-catherine/__test__/heap-sort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const heapSort = require('../src/heap-sort');
const testFunction = require('./test-function');

describe('testing heap-sort.js', () => {

describe('testing that heapSort is functioning properly', () => {
test('testing that heapSort returns a sorted array', () => {
let arrayToSort = [8, 1, 5, 7, 4, 9, 2, 3, 6];
expect(heapSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

test('testing that heapSort returns a sorted array even when there are duplicate values in the array', () => {
let arrayToSort = [8, 8, 1, 5, 5, 7, 4, 9, 2, 3, 6];
expect(heapSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9]);
});

test('testing that heapSort will return an array with one value if one value is passed in', () => {
let arrayToSort = [8];
expect(heapSort(arrayToSort)).toEqual([8]);
});

test('testing that heapSort will return an empty array if the array is empty', () => {
let arrayToSort = [];
expect(heapSort(arrayToSort)).toEqual([]);
});

test('testing that heapSort will return the correct order with multi-digit integers', () => {
let arrayToSort = [8, 1, 25, 7, 11, 9, 2, 3, 6, 10];
expect(heapSort(arrayToSort)).toEqual([1, 2, 3, 6, 7, 8, 9, 10, 11, 25]);
});
});

describe('testing random arrays', () => {
test('testing that small array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(8, 3);
let arrayToValidate = testFunction.validateArray(heapSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});

test('testing that medium array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(100, 100);
let arrayToValidate = testFunction.validateArray(heapSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});

test('testing that large array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(10000, 10000);
let arrayToValidate = testFunction.validateArray(heapSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});
});
});
40 changes: 40 additions & 0 deletions lab-catherine/__test__/max-heap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

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

describe('max-heap.js', () => {
test('insert method should throw an error if value is not a number', () => {
const testMaxHeap = new MaxHeap();
expect(() => {
testMaxHeap.insert('invalidValue');
}).toThrow();
});

test('testing that insert method is functioning properly and that peek returns max value', () => {
const testMaxHeap = new MaxHeap();

expect(testMaxHeap.peek()).toEqual(null);
testMaxHeap.insert(6);
expect(testMaxHeap.peek()).toEqual(6);
testMaxHeap.insert(10);
expect(testMaxHeap.peek()).toEqual(10);
testMaxHeap.insert(15);
expect(testMaxHeap.peek()).toEqual(15);
});

test('testing that extractMaximum method is functioning properly', () => {
const testMaxHeap = new MaxHeap();

testMaxHeap.insert(10);
testMaxHeap.insert(7);
testMaxHeap.insert(8);
testMaxHeap.insert(5);
testMaxHeap.insert(9);
expect(testMaxHeap.extractMaximum()).toEqual(10);
expect(testMaxHeap.extractMaximum()).toEqual(9);
expect(testMaxHeap.extractMaximum()).toEqual(8);
expect(testMaxHeap.extractMaximum()).toEqual(7);
expect(testMaxHeap.extractMaximum()).toEqual(5);
expect(testMaxHeap.extractMaximum()).toEqual(null);
});
});
54 changes: 54 additions & 0 deletions lab-catherine/__test__/merge-sort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const mergeSort = require('../src/merge-sort');
const testFunction = require('./test-function');

describe('testing merge-sort.js', () => {

describe('testing that mergeSort is functioning properly', () => {
test('testing that mergeSort returns a sorted array', () => {
let arrayToSort = [8, 1, 5, 7, 4, 9, 2, 3, 6];
expect(mergeSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

test('testing that mergeSort returns a sorted array even when there are duplicate values in the array', () => {
let arrayToSort = [8, 8, 1, 5, 5, 7, 4, 9, 2, 3, 6];
expect(mergeSort(arrayToSort)).toEqual([1, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9]);
});

test('testing that mergeSort will return an array with one value if one value is passed in', () => {
let arrayToSort = [8];
expect(mergeSort(arrayToSort)).toEqual([8]);
});

test('testing that mergeSort will return an empty array if the array is empty', () => {
let arrayToSort = [];
expect(mergeSort(arrayToSort)).toEqual([]);
});

test('testing that mergeSort will return the correct order with multi-digit integers', () => {
let arrayToSort = [8, 1, 25, 7, 11, 9, 2, 3, 6, 10];
expect(mergeSort(arrayToSort)).toEqual([1, 2, 3, 6, 7, 8, 9, 10, 11, 25]);
});
});

describe('testing random arrays', () => {
test('testing that small array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(8, 3);
let arrayToValidate = testFunction.validateArray(mergeSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});

test('testing that medium array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(100, 100);
let arrayToValidate = testFunction.validateArray(mergeSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});

test('testing that large array will be sorted', () => {
let arrayToSort = testFunction.generateRandomArray(10000, 10000);
let arrayToValidate = testFunction.validateArray(mergeSort(arrayToSort));
expect(arrayToValidate).toEqual(true);
});
});
});
Loading