forked from crizstian/data-structure-and-algorithms-with-ES6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-module.js
48 lines (42 loc) · 1.12 KB
/
sort-module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(function (exports) {
const {quickSort: quickMethod} = require('./quick-sort.module')
const {mergeSort: mergeMethod} = require('./merge-sort.module')
const {bubbleSort, selectionSort, insertionSort} = require('./basic-sort')
let alist
const display = () => console.log(alist)
const getData = () => alist
function swap (v1, v2) {
const temp = alist[v1]
alist[v1] = alist[v2]
alist[v2] = temp
}
const methods = {
bubbleSort () {
alist = bubbleSort.sort(alist, swap)
return this.getData()
},
selectionSort () {
alist = selectionSort.sort(alist, swap)
return this.getData()
},
insertionSort () {
alist = insertionSort.sort(alist)
return this.getData()
},
quickSort () {
alist = quickMethod.sort(alist, swap)
return this.getData()
},
mergeSort () {
alist = mergeMethod.sort(alist)
return this.getData()
},
getData,
display
}
exports.sort = function sort (args) {
alist = args
Object.assign(this, methods)
return this
}
}((typeof module.exports !== undefined) ? module.exports : window))