Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6052715

Browse files
initial commit
0 parents commit 6052715

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
big-o/

‎README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Computer Science algorithms implemented in JavaScript
2+
3+
## Usage
4+
5+
[If you want to test your knowledge](#test-your-knowledge)
6+
7+
[If you just want to see the algorithms](#algorithms)
8+
9+
### Test your knowledge
10+
11+
1. Clone and install dependencies
12+
13+
```
14+
git clone https://github.com/estevanmaito/algorithms-javascript
15+
cd algorithms-javascript
16+
npm install
17+
```
18+
19+
2. Delete the code above this line for every exercise:
20+
21+
```js
22+
const test = require('tape')
23+
```
24+
25+
3. Write code to make the tests pass
26+
27+
4. Run tests
28+
29+
```
30+
node file-name.js
31+
```
32+
33+
### Algorithms
34+
35+
Feel free to navigate around.
36+
37+
#### Sorting
38+
39+
[Insertion sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/insertion-sort.js)
40+
41+
[Selection sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/selection-sort.js)
42+
43+
#### Searching
44+
45+
[Binary search](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/searching/binary-search.js)

‎package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "algorithms-in-javascript",
3+
"version": "1.0.0",
4+
"description": "Computer Science algorithms in JavaScript",
5+
"keywords": [
6+
"algorithms",
7+
"javascript",
8+
"computer",
9+
"science",
10+
"sorting",
11+
"merge",
12+
"quick",
13+
"sort",
14+
"binary",
15+
"bubble"
16+
],
17+
"author": "Estevan Maito <ejmaito@gmail.com>",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"tape": "^4.6.3"
21+
}
22+
}

‎searching/binary-search.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://en.wikipedia.org/wiki/Binary_search_algorithm
2+
3+
let binarySearch = (targetValue, array ) => {
4+
let start = 0
5+
let end = array.length - 1
6+
let middle
7+
8+
while (start <= end) {
9+
middle = Math.floor((start + end) / 2)
10+
11+
if (targetValue === array[middle]) return middle
12+
else if (targetValue < array[middle]) end = middle - 1
13+
else start = middle + 1
14+
}
15+
return -1
16+
}
17+
18+
const test = require('tape')
19+
20+
let generateSortedArray = (range) => {
21+
let array = []
22+
while (array.length < range) {
23+
array.push(array.length + 1)
24+
}
25+
return array
26+
}
27+
28+
test('Binary search', assert => {
29+
assert.deepEqual(binarySearch(7, []), -1)
30+
assert.deepEqual(binarySearch(2, [1,2,3,4,5]), 1)
31+
assert.deepEqual(binarySearch(36, generateSortedArray(150)), 35)
32+
assert.deepEqual(binarySearch(981, generateSortedArray(10000)), 980)
33+
assert.end()
34+
})

‎sorting/insertion-sort.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://en.wikipedia.org/wiki/Insertion_sort
2+
3+
let insertionSort = (array) => {
4+
let u, // unsorted
5+
s, // sorted
6+
value
7+
8+
for (u = 1; u < array.length; u++) {
9+
value = array[u]
10+
11+
for (s = u - 1; s >= 0 && array[s] > value; s--) {
12+
array[s + 1] = array[s]
13+
}
14+
15+
array[s + 1] = value
16+
}
17+
return array
18+
}
19+
20+
const test = require('tape')
21+
22+
test('Insertion sort', assert => {
23+
assert.deepEqual(insertionSort([2, 4, 6, 5, 1, 3]), [1, 2, 3, 4, 5, 6])
24+
assert.deepEqual([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])
25+
assert.deepEqual(insertionSort([-2, 4, 6, -5, 1, 3]), [-5, -2, 1, 3, 4, 6])
26+
assert.deepEqual(insertionSort([40, 12, 5, 34, 8, 35]), [5, 8, 12, 34, 35, 40])
27+
assert.end()
28+
})

‎sorting/selection-sort.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// https://en.wikipedia.org/wiki/Selection_sort
2+
3+
let selectionSort = (array) => {
4+
let min
5+
6+
for (let i = 0; i < array.length; i++) {
7+
min = i
8+
9+
for (let j = i + 1; j < array.length; j++) {
10+
if (array[j] < array[min]) {
11+
min = j
12+
}
13+
}
14+
15+
if (i !== min) {
16+
let temp = array[i]
17+
array[i] = array[min]
18+
array[min] = temp
19+
}
20+
21+
}
22+
return array
23+
}
24+
25+
const test = require('tape')
26+
27+
test('Selection sort', assert => {
28+
assert.deepEqual(selectionSort([2, 4, 6, 5, 1, 3]), [1, 2, 3, 4, 5, 6])
29+
assert.deepEqual([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])
30+
assert.deepEqual(selectionSort([-2, 4, 6, -5, 1, 3]), [-5, -2, 1, 3, 4, 6])
31+
assert.deepEqual(selectionSort([40, 12, 5, 34, 8, 35]), [5, 8, 12, 34, 35, 40])
32+
assert.end()
33+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /