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.
 
 
 

134 lines
3.7 KiB

/**
*
*/
var ShellSort = function() {
var _this = this;
this.ui = {
presort: function presort(arr, i, gap) {
_this
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0)
.instruct(Itemgroup.message, 0, 0, 1, `Comparisons: ${_this.comparisons}`)
.instruct(Itemgroup.message, 0, 0, 2, `Swaps: ${_this.swaps}`)
.instruct(Itemgroup.message, 0, 0, 3, `Gap: ${gap}`)
.instruct(Itemgroup.message, 0, 0, 4, ``)
.instruct(Itemgroup.message, 0, 100, 5, `Starting sort at index ${i}.`)
},
//
midsort: function midsort(arr, i, j, gap) {
_this
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0)
.instruct(Itemgroup.background, 1, 0, j, j, Visualizer.bg1)
.instruct(Itemgroup.background, 1, 0, (j - gap), (j - gap), Visualizer.bg1)
.instruct(Itemgroup.message, 0, 0, 4, `Comparing [${j - gap}] and [${j}]`)
.instruct(Itemgroup.message, 0, 100, 5, ``)
},
//
swap: function swap(arr, j, gap) {
_this
.instruct(Itemgroup.message, 0, 100, 4, `${arr[j]} < ${arr[j - gap]}, swap required.`)
.instruct(Itemgroup.swap, 1, 300, j - gap, j)
if (j - gap > 0) {
_this
.instruct(Itemgroup.message, 0, 100, 5, `Continuing downstream...`)
}
},
//
noswap: function noswap(arr, j, gap) {
_this
.instruct(Itemgroup.message, 0, 0, 4, `${arr[j]} > ${arr[j - gap]}, no swap required.`)
.instruct(Itemgroup.message, 0, 100, 5, ``)
},
//
postsort: function postsort(arr) {
_this
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0)
.instruct(Itemgroup.message, 0, 0, 3, ``)
.instruct(Itemgroup.message, 0, 0, 4, ``)
.instruct(Itemgroup.message, 0, 100, 5, `Ready for insertion sort.`)
}
};
};
ShellSort.prototype = Object.create(Sorter.prototype);
/**
*
*/
ShellSort.prototype.init = function() {
var len = this.shuffled.length;
this.actions = [];
this.swaps = 0;
this.comparisons = 0;
this
.instruct(Itemgroup.items, 0, 0, len)
.instruct(Itemgroup.items, 1, 0, len)
.instruct(Itemgroup.items, 2, 0, len)
for (var i = 0; i < len; i++) {
this.instruct(Itemgroup.text, 1, 0, i, this.shuffled[i]);
}
this
.instruct(Itemgroup.foreground, 1, 0, 0, len, Visualizer.fg0)
.instruct(Itemgroup.background, 1, 0, 0, len, Visualizer.bg0)
.instruct(Itemgroup.opacity, 0, 0, 0, len, 0)
.instruct(Itemgroup.opacity, 2, 0, 0, len, 0)
};
/**
*
*/
ShellSort.prototype.sort = function(arr) {
var len = arr.length;
var gap = Math.floor(len / 3);
var i, j;
for (i = 0; i < gap; i++) {
this.ui.presort(arr, i, gap);
this.gapSort(arr, i, gap);
}
this.ui.postsort(arr);
return arr;
};
/**
*
*/
ShellSort.prototype.gapSort = function(arr, start, gap) {
var i, j;
var len = arr.length;
for (i = start; i < len; i += gap) {
for (j = i; j > start; j -= gap) {
this.ui.midsort(arr, i, j, gap);
this.comparisons++;
if (arr[j - gap] > arr[j]) {
this.swaps++;
tmp = arr[j - gap];
arr[j - gap] = arr[j];
arr[j] = tmp;
this.ui.swap(arr, j, gap);
}
else {
this.ui.noswap(arr, j, gap);
break;
}
}
}
};