You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.4 KiB
77 lines
1.4 KiB
/**
|
|
*
|
|
*/
|
|
var Sorter = function() {
|
|
this.data = [];
|
|
this.shuffled = [];
|
|
this.ordered = [];
|
|
|
|
this.actions = [];
|
|
this.comparisons = [];
|
|
};
|
|
|
|
// NOTE fisher-yates, http://bost.ocks.org/mike/algorithms/
|
|
/**
|
|
* Returns copy of an array shuffled using Fisher-Yates.
|
|
*/
|
|
Sorter.prototype.shuffle = function(arr) {
|
|
var result = Object.create(arr);
|
|
var n = result.length, t, i;
|
|
while (n) {
|
|
i = Math.random() * n-- | 0; // 0 ≤ i < n
|
|
t = result[n];
|
|
result[n] = result[i];
|
|
result[i] = t;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Sorter.prototype.swap = function(arr, i, j) {
|
|
var tmp = arr[i];
|
|
arr[i] = arr[j];
|
|
arr[j] = tmp;
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Sorter.prototype.sort = function() {
|
|
throw new Error('Sorter.sort() method override required.');
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Sorter.prototype.init = function() {
|
|
throw new Error('Sorter.init() method override required.');
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Sorter.prototype.instruct = function() {
|
|
this.actions.push(arguments);
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Sorter.prototype.generate = function(n) {
|
|
this.data = [];
|
|
|
|
var upper = Math.floor(Math.random() * 300 + 100 + n);
|
|
for (var i = 0; i < n; i++) {
|
|
this.data.push(Math.floor(i * upper / n));
|
|
};
|
|
|
|
this.shuffled = this.shuffle(this.data);
|
|
this.ordered = this.shuffled.slice();
|
|
this.init();
|
|
this.sort(this.ordered, 0, this.ordered.length - 1);
|
|
|
|
return this.actions;
|
|
};
|
|
|