/** * */ var QuickSort = function() { this.instructions = []; this.swaps = 0; this.comparisons = 0; }; 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) return arr; } var left = start; var right = end; var pivot = Math.floor((right + left) / 2); var pivotval = arr[pivot].value; var tmp; this//.instruct('initSection', left, right) .instruct('showMarker', 0, 1) .instruct('marker', 100, 1, left, 'L') .instruct('message', 0, 3, 'Left: ' + left) .instruct('showMarker', 0, 2) .instruct('marker', 100, 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); while (left <= right) { while (arr[left].value < pivotval) { left++; this.comparisons++; this.instruct('marker', 100, 1, left) .instruct('message', 0, 3, 'Left: ' + left) .instruct('message', 0, 1, 'Comparisons: ' + this.comparisons) } while (arr[right].value > pivotval) { right--; this.comparisons++; this.instruct('marker', 100, 2, right) .instruct('message', 0, 4, 'Right: ' + right) .instruct('message', 0, 1, 'Comparisons: ' + this.comparisons) } if (left <= right) { tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; this.instruct('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.instruct('unhighlight', 0) .instruct('hideMarker', 0, 1) .instruct('hideMarker', 0, 2); this.sort(arr, start, right); this.sort(arr, left, end); return arr; };