Skip to content

Commit b1a7111

Browse files
committed
adding test examples and index module
1 parent dc21f85 commit b1a7111

File tree

11 files changed

+257
-220
lines changed

11 files changed

+257
-220
lines changed

12-chapter-Sorting-Algorithms/implentation.js

-51
This file was deleted.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(function (exports) {
2-
const {sort} = require('./sort-module')
2+
const sort = require('./sort-module')
33
Object.assign(exports, sort)
44
}((typeof module.exports !== undefined) ? module.exports : window))

12-chapter-Sorting-Algorithms/readme.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Sort Algorithms with ES6
1+
# Sort Algorithms with ES6
22

33
### Code Examples
4-
[Basic Sorting Algorithms](./basic-sort.js)
5-
[Advanced Sorting Algorithms - Quick Sort](./quick-sort.js)
6-
[Advanced Sorting Algorithms - Merge Sort](./merge-sort.js)
7-
[Module](./sort-module.js)
8-
[Implentation](./implementation.js)
9-
[Import Module](./index.js)
4+
- [Basic Sorting Algorithms](./basic-sort.js)
5+
- [Advanced Sorting Algorithms - Quick Sort](./quick-sort.js)
6+
- [Advanced Sorting Algorithms - Merge Sort](./merge-sort.js)
7+
- [Module](./sort-module.js)
8+
- [Implentation](./implementation.js)
9+
- [Import Module](./index.js)
1010

1111
### Description
1212

12-chapter-Sorting-Algorithms/sort-module.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,34 @@
1515
alist[v2] = temp
1616
}
1717

18+
const methods = {
19+
bubbleSort () {
20+
alist = bubbleSort.sort(alist, swap)
21+
return this.getData()
22+
},
23+
selectionSort () {
24+
alist = selectionSort.sort(alist, swap)
25+
return this.getData()
26+
},
27+
insertionSort () {
28+
alist = insertionSort.sort(alist)
29+
return this.getData()
30+
},
31+
quickSort () {
32+
alist = quickMethod.sort(alist, swap)
33+
return this.getData()
34+
},
35+
mergeSort () {
36+
alist = mergeMethod.sort(alist)
37+
return this.getData()
38+
},
39+
getData,
40+
display
41+
}
42+
1843
exports.sort = function sort (args) {
1944
alist = args
20-
return {
21-
bubbleSort () {
22-
alist = bubbleSort.sort(alist, swap)
23-
return this.getData()
24-
},
25-
selectionSort () {
26-
alist = selectionSort.sort(alist, swap)
27-
return this.getData()
28-
},
29-
insertionSort () {
30-
alist = insertionSort.sort(alist)
31-
return this.getData()
32-
},
33-
quickSort () {
34-
alist = quickMethod.sort(alist, swap)
35-
return this.getData()
36-
},
37-
mergeSort () {
38-
alist = mergeMethod.sort(alist)
39-
return this.getData()
40-
},
41-
getData,
42-
display
43-
}
45+
Object.assign(this, methods)
46+
return this
4447
}
4548
}((typeof module.exports !== undefined) ? module.exports : window))

12-chapter-Sorting-Algorithms/test.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const {sort} = require('./sort-module')
2+
3+
const nsTime = (hrtime) => hrtime[0] * 1e9 + hrtime[1]
4+
5+
function setData (numElements = 100) {
6+
let dataStore = []
7+
for (let i = 0; i < numElements; i++) {
8+
dataStore[i] = Math.floor(Math.random() * (numElements + 1))
9+
}
10+
return dataStore
11+
}
12+
13+
let basic = 50
14+
15+
test (`bubbleSort test with ${basic} elements`, assert => {
16+
let start = process.hrtime()
17+
let dataStore = setData(basic)
18+
let sorted = sort(dataStore).bubbleSort()
19+
let end = process.hrtime(start)
20+
21+
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
22+
})
23+
24+
test (`selectionSort test with ${basic} elements`, assert => {
25+
let start = process.hrtime()
26+
let dataStore = setData(basic)
27+
let sorted = sort(dataStore).selectionSort()
28+
let end = process.hrtime(start)
29+
30+
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
31+
})
32+
33+
test (`insertionSort test with ${basic} elements`, assert => {
34+
let start = process.hrtime()
35+
let dataStore = setData(basic)
36+
let sorted = sort(dataStore).insertionSort()
37+
let end = process.hrtime(start)
38+
39+
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${basic} elements is: ${nsTime(end) / 1e9} seconds.`)
40+
})
41+
42+
let advance = 1000
43+
44+
test (`mergeSort test with ${advance} elements`, assert => {
45+
let start = process.hrtime()
46+
let dataStore = setData(advance)
47+
let sorted = sort(dataStore).mergeSort()
48+
let end = process.hrtime(start)
49+
50+
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${advance} elements is: ${nsTime(end) / 1e9} seconds.`)
51+
})
52+
53+
test (`quickSort test with ${advance} elements`, assert => {
54+
let start = process.hrtime()
55+
let dataStore = setData(advance)
56+
let sorted = sort(dataStore).quickSort()
57+
let end = process.hrtime(start)
58+
59+
assert.equal(dataStore.sort(), sorted, `Elapsed time on ${advance} elements is: ${nsTime(end) / 1e9} seconds.`)
60+
})

13-chapter-Searching-Algorithms/13-chapter-Searching-Algorithms.js

-130
This file was deleted.
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(function (exports) {
2+
const search = require('./search-module')
3+
Object.assign(exports, {search})
4+
}((typeof module.exports !== undefined) ? module.exports : window))

13-chapter-Searching-Algorithms/readme.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
# Search Algorithms
1+
# Search Algorithms with ES6
2+
3+
### Code Examples
4+
5+
- [Sequential/Binary Search example](./search-module.js)
6+
- [JavaScript built-in findIndex(), find()](../03-chapter-List.js)
7+
- [Binary Search Tree Searches](../10-chapter-Binary-Trees.js)
8+
- [Depth/Breadth First Search](../11-chapter-2-adjecency-list-Graphs.js)
9+
10+
### Description
211

312
There are two ways to search for data in a list: sequential search and binary search. A sequential search is used when the items in a list are in random order; a binary search is used when the items in a list are in sorted order. Binary search is the more efficient algorithm, but you also have to take into account the extra time it takes to sort the data set before being able to search it for a value.
413

@@ -62,12 +71,6 @@ Applications:
6271
- Used in artificial intelligence to build bots, for instance a chess bot
6372
- Finding shortest path between two cities in a map and many other such applications
6473

65-
### Code Examples
66-
67-
- [JavaScript built-in findIndex(), find()](../03-chapter-List.js)
68-
- [Sequential/Binary Search example](./13-chapter-Searching-Algorithms.js)
69-
- [Binary Search Tree Searches](../10-chapter-Binary-Trees.js)
70-
- [Depth/Breadth First Search](../11-chapter-2-adjecency-list-Graphs.js)
7174

7275
#### Resources
7376

0 commit comments

Comments
 (0)