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 ebf7cbb

Browse files
add merge sort
1 parent 6052715 commit ebf7cbb

File tree

6 files changed

+231
-4
lines changed

6 files changed

+231
-4
lines changed

‎README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,30 @@ node file-name.js
3434

3535
Feel free to navigate around.
3636

37+
#### Data Structures
38+
39+
[Linked list](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/data-structures/linked-list.js)
40+
41+
[Queue](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/data-structures/queue.js)
42+
43+
[Stack](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/data-structures/stack.js)
44+
45+
[Binary Search Tree](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/data-structures/binary-search-tree.js)
46+
3747
#### Sorting
3848

3949
[Insertion sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/insertion-sort.js)
4050

4151
[Selection sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/selection-sort.js)
4252

53+
[Merge sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/selection-sort.js)
54+
55+
[Quick sort](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/sorting/selection-sort.js)
56+
4357
#### Searching
4458

45-
[Binary search](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/searching/binary-search.js)
59+
[Binary search](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/searching/binary-search.js)
60+
61+
[Breadth-first search](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/searching/breadth-first-search.js)
62+
63+
[Depth-first search](https://github.com/estevanmaito/algorithms-in-javascript/tree/master/searching/depth-first-search.js)

‎data-structures/linked-list.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// https://en.wikipedia.org/wiki/Linked_list
2+
3+
function Node(data) {
4+
this.data = data
5+
this.next = null
6+
}
7+
8+
function LinkedList() {
9+
this.length = 0
10+
this.head = null
11+
}
12+
13+
LinkedList.prototype.add = function (value) {
14+
let node = new Node(value)
15+
current = this.head
16+
17+
if (!current) {
18+
this.head = node
19+
} else {
20+
while (current.next) current = current.next
21+
current.next = node
22+
}
23+
this.length++
24+
25+
return node
26+
}
27+
28+
LinkedList.prototype.findAt = function (position) {
29+
if (position > -1 && position < this.length) {
30+
let current = this.head
31+
32+
for (let i = 0; i++ < position; current = current.next);
33+
34+
return current.data
35+
}
36+
return null
37+
}
38+
39+
LinkedList.prototype.remove = function (position) {
40+
if (position > -1 && position < this.length) {
41+
let current = this.head
42+
43+
if (position === 0) {
44+
current = current.next
45+
} else {
46+
let i = 0
47+
let previous
48+
while (i++ < position) {
49+
previous = current
50+
current = current.next
51+
}
52+
previous.next = current.next
53+
}
54+
this.length--
55+
return current.data
56+
}
57+
return null
58+
}
59+
60+
const test = require('tape')
61+
62+
test('Linked list add', assert => {
63+
let ll = new LinkedList()
64+
ll.add('Hello world!')
65+
assert.deepEqual(ll.head.data, 'Hello world!')
66+
assert.deepEqual(ll.length, 1)
67+
68+
ll.add('Can you see me?')
69+
assert.deepEqual(ll.length, 2)
70+
assert.end()
71+
})
72+
73+
test('Linked list findAt', assert => {
74+
let ll = new LinkedList()
75+
ll.add('Hello world!')
76+
ll.add('Can you see me?')
77+
ll.add('Now you don\'t')
78+
assert.deepEqual(ll.findAt(2), 'Now you don\'t')
79+
assert.deepEqual(ll.findAt(0), 'Hello world!')
80+
assert.deepEqual(ll.findAt(3), null)
81+
assert.end()
82+
})
83+
84+
test('Linked list remove', assert => {
85+
let ll = new LinkedList()
86+
ll.add('Hello world!')
87+
ll.add('Can you see me?')
88+
ll.add('Now you don\'t')
89+
ll.remove(2)
90+
assert.deepEqual(2, ll.length)
91+
ll.remove(1)
92+
assert.deepEqual(1, ll.length)
93+
assert.end()
94+
})

‎data-structures/queue.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
2+
3+
function Queue() {
4+
this.front = 0
5+
this.rear = 0
6+
this.items = {}
7+
}
8+
9+
Queue.prototype.size = function () {
10+
return this.rear - this.front
11+
}
12+
13+
Queue.prototype.enqueue = function (value) {
14+
this.items[this.rear++] = value
15+
}
16+
17+
Queue.prototype.dequeue = function () {
18+
if (this.rear === this.front) return undefined
19+
let front = this.items[this.front]
20+
delete this.items[this.front++]
21+
22+
return front
23+
}
24+
25+
const test = require('tape')
26+
27+
test('Queue enqueue', assert => {
28+
let q = new Queue()
29+
assert.deepEqual(q.dequeue(), undefined)
30+
q.enqueue(1)
31+
q.enqueue(3)
32+
assert.deepEqual(q.size(), 2)
33+
assert.deepEqual(q.dequeue(), 1)
34+
assert.deepEqual(q.size(), 1)
35+
q.enqueue(4)
36+
q.enqueue(5)
37+
assert.deepEqual(q.size(), 3)
38+
assert.deepEqual(q.dequeue(), 3)
39+
assert.deepEqual(q.size(), 2)
40+
assert.deepEqual(q.dequeue(), 4)
41+
assert.deepEqual(q.dequeue(), 5)
42+
assert.deepEqual(q.dequeue(), undefined)
43+
q.enqueue(1)
44+
q.enqueue(2)
45+
assert.deepEqual(q.dequeue(), 1)
46+
assert.end()
47+
})

‎data-structures/stack.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/Stack_(abstract_data_type)
2+
3+
function Stack() {
4+
this.length = 0
5+
this.items = {}
6+
}
7+
8+
Stack.prototype.push = function (value) {
9+
this.items[this.length++] = value
10+
}
11+
12+
Stack.prototype.pop = function () {
13+
if (this.length === 0) return undefined
14+
let top = this.items[--this.length]
15+
delete this.items[this.length]
16+
return top
17+
}
18+
19+
const test = require('tape')
20+
21+
test('Stack add', assert => {
22+
let s = new Stack()
23+
s.push(1)
24+
s.push(3)
25+
assert.deepEqual(s.length, 2)
26+
assert.deepEqual(s.pop(), 3)
27+
assert.deepEqual(s.length, 1)
28+
assert.deepEqual(s.pop(), 1)
29+
assert.deepEqual(s.pop(), undefined)
30+
s.push(2)
31+
assert.deepEqual(s.length, 1)
32+
assert.end()
33+
})

‎searching/binary-search.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// https://en.wikipedia.org/wiki/Binary_search_algorithm
22

33
let binarySearch = (targetValue, array ) => {
4-
let start = 0
5-
letend = array.length - 1
6-
let middle
4+
let start = 0,
5+
end = array.length - 1,
6+
middle
77

88
while (start <= end) {
99
middle = Math.floor((start + end) / 2)

‎sorting/merge-sort.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://en.wikipedia.org/wiki/Merge_sort
2+
3+
let merge = (left, right) => {
4+
let arr = [],
5+
i = 0,
6+
j = 0
7+
8+
while (i < left.length && j < right.length) {
9+
if (left[i] < right[j]) arr.push(left[i++])
10+
else arr.push(right[j++])
11+
}
12+
13+
return arr.concat(left.slice(i)).concat(right.slice(j))
14+
}
15+
16+
let mergeSort = (arr) => {
17+
if (arr.length > 1) {
18+
let middle = Math.floor(arr.length / 2),
19+
left = mergeSort(arr.slice(0, middle)),
20+
rigth = mergeSort(arr.slice(middle))
21+
return merge(left, rigth)
22+
}
23+
24+
return arr
25+
}
26+
27+
const test = require('tape')
28+
29+
test('Merge sort', assert => {
30+
assert.deepEqual(mergeSort([2,4,5,7,1,2,3,6]), [1,2,2,3,4,5,6,7])
31+
assert.deepEqual(mergeSort([2,22,5,-3,4,9,3,6]), [-3,2,3,4,5,6,9,22])
32+
assert.deepEqual(mergeSort([]), [])
33+
assert.deepEqual(mergeSort([1]), [1])
34+
assert.end()
35+
})

0 commit comments

Comments
(0)

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