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-andrew/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
31 changes: 31 additions & 0 deletions lab-andrew/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"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",
"ecmaVersion": 6,
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"rules": {
"no-console": "off",
"indent": [ "error", 2, { "SwitchCase": 1 } ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"space-infix-ops": ["error", {"int32Hint": false}],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}
106 changes: 106 additions & 0 deletions lab-andrew/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Created by https://www.gitignore.io/api/node,macos

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

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

# 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

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

# 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
*/.env


# mongo db
db

# End of https://www.gitignore.io/api/node,macos
54 changes: 54 additions & 0 deletions lab-andrew/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Lab 39 Sorting Algorithms

## Overview

This is a model of of a stable quicksort algorithm.
***
## Getting Started

Basic understanding of git and npm is assumed. Clone down the repo and navigate to the directory. Install by running `npm i`. If you wish to use these modules, simply copy the desired module's file into your project and require it into the script in which you would like to use it. For example:

```const quicksort = require('./lib/quicksort')```

To run tests to check functionality, do an

```npm run test```

The quicksort method can be called on any array of numbers `quicksort(`<`array`>`)`.
***
## Modules

The modules being exported are the quicksort, mergesort, heapsort and radixsort methods. Each of these modules takes an array of numbers as input, and return that array in sorted order.
***
### Quicksort:

The quicksort method will take an array of numbers and return the sorted array using a divide and conquer scheme, resulting in an average runtime of O(n*log(n)). The worst case will be 0(n^2), which will occur when the array is already in order or ordereded in reverse (i.e. [5, 4, 3, 2, 1]). The big O of space will be O(n), as the array is being copied with the slice method.

The quicksort works by comparing each value to a reference (in this case the last value in the array) and swapping all the values smaller than this value to the left side of the first value found that is larger than the reference. If a value is equal, it is moved next to the reference value. The quicksort method is then recursively called on each of the subsections of the array, and the resulting sorted segments are reassembled according to order from lowest to highest.

### Mergesort:

The mergesort method will take an array of numbers and return the sorted array, it also uses a divide and conquer scheme. The runtime will always be O(n*log(n)). It works by splitting the array into two smaller arrays down the middle, recursively performs mergesort on each of those smaller arrays, and then compares each of the values in the two sorted arrays and merges them back into one sorted array. The space complexity will be O(log(n)), which is the size of the recursive call stack.

### Heapsort:

The heapsort method takes an array of numbers and returns the sorted array, however, it does not use the divide and conquer scheme, although it does keep a O(n*log(n)) runtime. It works by inserting each of the values in an array into a heap. Then it extracts each of the values from that heap. Based on how a heap works, the values will be extracted in order. The sort algorithm itself has a constant space big O, however, the space complexity ultimately depends on the implementation of the heap that it depends on. The heap in this case has a space complexity of O(log(n)) which corresponds to the height of the heap, as this will be the max number of recursive calls in the call stack.

### Radixsort:

The radixsort method takes an array of numbers and returns the sorted array. It does not use comparison to sort, but rather uses modulo arithemtic to place the numbers in a series of 'buckets'. In this implementation, the LSD approach is used, which examines the digits in order from smallest digit to largest. This is a stable sort, as items retain relative positioning between equal values. The big O of this is theoretically O(w\*n), where w is the length of the largest word size, or the exponent of 10 of the largest value in scientific notation, and n is the number of items in the array. The memory complexity is O(n) as a copy is created in this implementation when the buckets are reduced. According to wikipedia, in execution, the performance may be actually limited to O(n*log(n)) because of the physical design of the actual hardware (i.e. how the RAM is accessed) used to run the sorting algorithm.
***
## Code Examples:

```javascript
const quickSort = require('./lib/quicksort');

quickSort([3, 4, 6, 3, 2]); // [2, 3, 3, 4, 6];
quickSort([1, 2, 3, 3, 2, 3, 2, 1, 2, 2]); // [1, 1, 2, 2, 2, 2, 2, 3, 3, 3]
quickSort([2]); // [2]
quickSort([]); // []
```
***
## Technology/Credits

By Andrew Bloom. Using jest to test functionality.
Loading