This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortAlgorithm.js
61 lines (51 loc) · 1.6 KB
/
SortAlgorithm.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
49
50
51
52
53
54
55
56
57
58
59
60
61
// Generated by CoffeeScript 1.6.3
(function() {
var SortAlgorithm, i, nums, sort;
SortAlgorithm = (function() {
function SortAlgorithm() {}
SortAlgorithm.prototype.bubbleSort = function(nums) {
var i, j, _i, _j, _nums, _ref, _ref1, _ref2, _ref3;
_nums = nums.slice(0);
for (i = _i = 0, _ref = _nums.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
for (j = _j = _ref1 = i + 1, _ref2 = _nums.length; _ref1 <= _ref2 ? _j <= _ref2 : _j >= _ref2; j = _ref1 <= _ref2 ? ++_j : --_j) {
if (_nums[i] > _nums[j]) {
_ref3 = [_nums[j], _nums[i]], _nums[i] = _ref3[0], _nums[j] = _ref3[1];
}
}
}
return _nums;
};
SortAlgorithm.prototype.quickSort = function(nums) {
var i, left, pi, right, _i, _len, _nums, _ref;
_nums = nums.slice(0);
if (_nums.length < 1) {
return _nums;
}
pi = _nums[0];
left = [];
right = [];
_ref = _nums.slice(1);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
if (i < pi) {
left.push(i);
} else {
right.push(i);
}
}
return this.quickSort(left).concat([pi]).concat(this.quickSort(right));
};
return SortAlgorithm;
})();
sort = new SortAlgorithm;
nums = (function() {
var _i, _results;
_results = [];
for (i = _i = 0; _i < 10; i = ++_i) {
_results.push(Math.floor(Math.random() * 10));
}
return _results;
})();
console.log(sort.bubbleSort(nums));
console.log(sort.quickSort(nums));
}).call(this);