|
|
|
@ -2,24 +2,17 @@ |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
var ShellSort = function() { |
|
|
|
|
//===== Inits.
|
|
|
|
|
this.actions = []; |
|
|
|
|
|
|
|
|
|
this.swaps = 0; |
|
|
|
|
this.comparisons = 0; |
|
|
|
|
|
|
|
|
|
//===== Action management.
|
|
|
|
|
//
|
|
|
|
|
this.preSort = function(arr, i, gap) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, 0, arr.length, Visualizer.bg0) |
|
|
|
|
.instruct(Itemgroup.background, 1, 0, i, i, Visualizer.bg1) |
|
|
|
|
|
|
|
|
|
.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.`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, `Starting sort at index ${i}.`) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
@ -41,7 +34,7 @@ var ShellSort = function() { |
|
|
|
|
|
|
|
|
|
if (j - gap > 0) { |
|
|
|
|
this |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, `Pushing downstream...`) |
|
|
|
|
.instruct(Itemgroup.message, 0, 100, 5, `Continuing downstream...`) |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -53,14 +46,12 @@ var ShellSort = function() { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
this.doInsertionSort = function() { |
|
|
|
|
// 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, ``)
|
|
|
|
|
// .instruct(Itemgroup.message, 0, 100, 5, ``)
|
|
|
|
|
this.postSort = function(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.`) |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -70,10 +61,12 @@ ShellSort.prototype = Object.create(Sorter.prototype); |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
ShellSort.prototype.init = function() { |
|
|
|
|
console.error('Shell sort visualization ends with insertion sort, how to manage?'); |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
@ -105,10 +98,7 @@ ShellSort.prototype.sort = function(arr) { |
|
|
|
|
this.gapSort(arr, i, gap); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.doInsertionSort(); |
|
|
|
|
var IS = new InsertionSort(); |
|
|
|
|
IS.sort(arr); |
|
|
|
|
// this.postSort();
|
|
|
|
|
this.postSort(arr); |
|
|
|
|
|
|
|
|
|
return arr; |
|
|
|
|
}; |
|
|
|
|