|
|
|
@ -1,28 +1,27 @@ |
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
var QuickSort = function() { |
|
|
|
|
this.instructions = []; |
|
|
|
|
var QuickSort = function(Visualizer) { |
|
|
|
|
this.swaps = 0; |
|
|
|
|
this.comparisons = 0; |
|
|
|
|
this.V = Visualizer; |
|
|
|
|
|
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.message, 0, 2, 'Swaps: ' + this.swaps) |
|
|
|
|
.instruct(this.V.message, 0, 1, 'Comparisons: ' + this.comparisons); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
QuickSort.prototype = Object.create(Sorter.prototype); |
|
|
|
|
|
|
|
|
|
QuickSort.prototype.instruct = function() { |
|
|
|
|
this.instructions.push(arguments); |
|
|
|
|
return this; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// NOTE adds to an instruction set
|
|
|
|
|
/** |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
QuickSort.prototype.sort = function(arr, start, end) { |
|
|
|
|
if (end - start <= 0) { |
|
|
|
|
// this//.instruct('highlight', 500, end)
|
|
|
|
|
// .instruct('message', 0, 3, 'Start: ' + start)
|
|
|
|
|
// .instruct('message', 0, 4, 'End: ' + end)
|
|
|
|
|
// this//.instruct(this.V.highlight, 500, end)
|
|
|
|
|
// .instruct(this.V.message, 0, 3, 'Start: ' + start)
|
|
|
|
|
// .instruct(this.V.message, 0, 4, 'End: ' + end)
|
|
|
|
|
return arr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -32,60 +31,82 @@ QuickSort.prototype.sort = function(arr, start, end) { |
|
|
|
|
var pivot = Math.floor((right + left) / 2); |
|
|
|
|
var pivotval = arr[pivot].value; |
|
|
|
|
var tmp; |
|
|
|
|
var rval; |
|
|
|
|
|
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.marker, 0, 1, left, 'L') |
|
|
|
|
.instruct(this.V.marker, 0, 2, right, 'R') |
|
|
|
|
|
|
|
|
|
.instruct(this.V.showMarker, 0, 1) |
|
|
|
|
.instruct(this.V.showMarker, 0, 2) |
|
|
|
|
|
|
|
|
|
this//.instruct('initSection', left, right)
|
|
|
|
|
.instruct('showMarker', 0, 1) |
|
|
|
|
.instruct('marker', 0, 1, left, 'L') |
|
|
|
|
.instruct('message', 0, 3, 'Left: ' + left) |
|
|
|
|
.instruct('showMarker', 0, 2) |
|
|
|
|
.instruct('marker', 0, 2, right, 'R') |
|
|
|
|
.instruct('message', 0, 4, 'Right: ' + right) |
|
|
|
|
.instruct('unhighlight') |
|
|
|
|
.instruct('highlight', 100, pivot) |
|
|
|
|
.instruct('message', 0, 5, 'Pivot value: ' + pivotval + ', pivot index: ' + pivot); |
|
|
|
|
.instruct(this.V.unhighlight, 0) |
|
|
|
|
.instruct(this.V.highlight, 0, pivot) |
|
|
|
|
|
|
|
|
|
.instruct(this.V.message, 0, 3, 'Sorting from [' + start + '] to [' + end + ']') |
|
|
|
|
.instruct(this.V.message, 0, 4, '') |
|
|
|
|
.instruct(this.V.message, 100, 5, ''); |
|
|
|
|
|
|
|
|
|
while (left <= right) { |
|
|
|
|
while (arr[left].value < pivotval) { |
|
|
|
|
left++; |
|
|
|
|
|
|
|
|
|
this.V.instruct(this.V.message, 0, 4, ''); |
|
|
|
|
this.V.instruct(this.V.message, 0, 5, ''); |
|
|
|
|
|
|
|
|
|
while (arr[left].value < pivotval) { |
|
|
|
|
this.comparisons++; |
|
|
|
|
this.instruct('marker', 100, 1, left) |
|
|
|
|
.instruct('message', 0, 3, 'Left: ' + left) |
|
|
|
|
.instruct('message', 0, 1, 'Comparisons: ' + this.comparisons) |
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.message, 0, 4, `${arr[left].value} < ${pivotval}, increment left` ) |
|
|
|
|
.instruct(this.V.message, 0, 1, `Comparisons: ${this.comparisons}`) |
|
|
|
|
.instruct(this.V.marker, 500, 1, left); |
|
|
|
|
|
|
|
|
|
left++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (arr[right].value > pivotval) { |
|
|
|
|
right--; |
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.message, 0, 4, `Left stop: ${arr[left].value} >= ${pivotval}`) |
|
|
|
|
.instruct(this.V.marker, 500, 1, left); |
|
|
|
|
|
|
|
|
|
while (arr[right].value > pivotval) { |
|
|
|
|
this.comparisons++; |
|
|
|
|
this.instruct('marker', 100, 2, right) |
|
|
|
|
.instruct('message', 0, 4, 'Right: ' + right) |
|
|
|
|
.instruct('message', 0, 1, 'Comparisons: ' + this.comparisons) |
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.message, 0, 5, `${arr[right].value} < ${pivotval}, decrement right`) |
|
|
|
|
.instruct(this.V.message, 0, 1, 'Comparisons: ' + this.comparisons) |
|
|
|
|
|
|
|
|
|
right--; |
|
|
|
|
this.V.instruct(this.V.marker, 500, 2, right) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.V.instruct(this.V.message, 100, 5, `Right stop: ${arr[right].value} <= ${pivotval} `) |
|
|
|
|
|
|
|
|
|
if (left <= right) { |
|
|
|
|
tmp = arr[left]; |
|
|
|
|
arr[left] = arr[right]; |
|
|
|
|
arr[right] = tmp; |
|
|
|
|
|
|
|
|
|
this.instruct('swap', 500, left, right); |
|
|
|
|
this.V.instruct(this.V.swap, 500, left, right); |
|
|
|
|
|
|
|
|
|
left++; |
|
|
|
|
right--; |
|
|
|
|
|
|
|
|
|
this.swaps++; |
|
|
|
|
|
|
|
|
|
this.instruct('message', 0, 2, 'Swaps: ' + this.swaps) |
|
|
|
|
.instruct('marker', 100, 1, left) |
|
|
|
|
.instruct('message', 0, 3, 'Left: ' + left) |
|
|
|
|
.instruct('marker', 100, 2, right) |
|
|
|
|
.instruct('message', 0, 4, 'Right: ' + right); |
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.message, 0, 2, 'Swaps: ' + this.swaps) |
|
|
|
|
.instruct(this.V.message, 0, 4, 'Incremement left...') |
|
|
|
|
.instruct(this.V.message, 0, 5, 'Decrement right...') |
|
|
|
|
.instruct(this.V.marker, 100, 1, left) |
|
|
|
|
.instruct(this.V.marker, 100, 2, right) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.instruct('unhighlight', 0) |
|
|
|
|
.instruct('hideMarker', 0, 1) |
|
|
|
|
.instruct('hideMarker', 0, 2); |
|
|
|
|
this.V |
|
|
|
|
.instruct(this.V.unhighlight, 0) |
|
|
|
|
.instruct(this.V.hideMarker, 0, 1) |
|
|
|
|
.instruct(this.V.hideMarker, 0, 2) |
|
|
|
|
.instruct(this.V.message, 0, 3, 'Sorting complete.') |
|
|
|
|
.instruct(this.V.message, 0, 4, '') |
|
|
|
|
.instruct(this.V.message, 0, 5, ''); |
|
|
|
|
|
|
|
|
|
this.sort(arr, start, right); |
|
|
|
|
this.sort(arr, left, end); |
|
|
|
|