Instruction steeping built.

master
ben-burlingham 10 years ago
parent 990c6f38ed
commit efbf0d5112
  1. 1
      index.html
  2. 36
      js/quicksort.js
  3. 51
      js/visualizer-inits.js
  4. 13
      js/visualizer.js

@ -105,6 +105,7 @@
margin:0 5px 10px 0; margin:0 5px 10px 0;
text-align:center; text-align:center;
transition:background 0.2s ease, border 0.2s ease; transition:background 0.2s ease, border 0.2s ease;
vertical-align: top;
width:30px; width:30px;
} }

@ -20,9 +20,9 @@ QuickSort.prototype.instruct = function() {
*/ */
QuickSort.prototype.sort = function(arr, start, end) { QuickSort.prototype.sort = function(arr, start, end) {
if (end - start <= 0) { if (end - start <= 0) {
// this.instruct('highlight', true, end) // this.instruct('highlight', 500, end)
// .instruct('message1', true, 'Start: ' + start) // .instruct('message1', 500, 'Start: ' + start)
// .instruct('message2', true, 'End: ' + end) // .instruct('message2', 500, 'End: ' + end)
return arr; return arr;
} }
@ -34,29 +34,29 @@ QuickSort.prototype.sort = function(arr, start, end) {
var tmp; var tmp;
this//.instruct('initSection', left, right) this//.instruct('initSection', left, right)
.instruct('marker1', true, left, 'L') .instruct('marker1', 500, left, 'L')
.instruct('marker2', true, right, 'R'); .instruct('marker2', 500, right, 'R');
// .instruct('unhighlight') // .instruct('unhighlight')
// .instruct('highlight', true, pivot) // .instruct('highlight', 500, pivot)
// .instruct('message3', false, 'Pivot value: ' + pivotval); // .instruct('message3', 500, 'Pivot value: ' + pivotval);
while (left <= right) { while (left <= right) {
while (arr[left].value < pivotval) { while (arr[left].value < pivotval) {
left++; left++;
this.comparisons++; this.comparisons++;
this.instruct('marker1', true, left) this.instruct('marker1', 500, left)
.instruct('message1', false, 'Left: ' + left) .instruct('message1', 500, 'Left: ' + left)
.instruct('stats1', false, 'Comparisons: ' + this.comparisons) .instruct('stats1', 500, 'Comparisons: ' + this.comparisons)
} }
while (arr[right].value > pivotval) { while (arr[right].value > pivotval) {
right--; right--;
this.comparisons++; this.comparisons++;
this.instruct('marker2', true, right) this.instruct('marker2', 500, right)
.instruct('message2', false, 'Right: ' + right) .instruct('message2', 500, 'Right: ' + right)
.instruct('stats1', false, 'Comparisons: ' + this.comparisons) .instruct('stats1', 500, 'Comparisons: ' + this.comparisons)
} }
if (left <= right) { if (left <= right) {
@ -69,11 +69,11 @@ QuickSort.prototype.sort = function(arr, start, end) {
this.swaps++; this.swaps++;
this//.instruct('swap', left, right) this//.instruct('swap', left, right)
.instruct('stats2', false, 'Swaps: ' + this.swaps) .instruct('stats2', 500, 'Swaps: ' + this.swaps)
.instruct('message1', false, 'Left: ' + left) .instruct('message1', 500, 'Left: ' + left)
.instruct('message2', false, 'Right: ' + right) .instruct('message2', 500, 'Right: ' + right)
.instruct('marker1', true, left) .instruct('marker1', 500, left)
.instruct('marker2', true, right); .instruct('marker2', 500, right);
} }
} }

@ -14,7 +14,7 @@ Visualizer.prototype.initItems = function(n) {
} }
// Items // Items
this.groups = this.svg.selectAll('g').data(shuffled).enter().append('g') this.groups = this.svg.selectAll('g').data(shuffled).enter().insert('g')
.attr('transform', `translate(0, ${Visualizer.itemY})`); .attr('transform', `translate(0, ${Visualizer.itemY})`);
this.groups.append('rect') this.groups.append('rect')
@ -103,7 +103,7 @@ Visualizer.prototype.initRange = function() {
}; };
var rangeChange = function(event) { var rangeChange = function(event) {
this.init.items(event.target.value); this.initItems(event.target.value);
}; };
@ -116,8 +116,8 @@ Visualizer.prototype.initRange = function() {
var range = document.createElement('input'); var range = document.createElement('input');
range.setAttribute('type', 'range'); range.setAttribute('type', 'range');
range.setAttribute('value', 40); range.setAttribute('value', 10);
range.setAttribute('min', 5); range.setAttribute('min', 10);
range.setAttribute('max', 80); range.setAttribute('max', 80);
range.addEventListener('change', rangeChange.bind(this)); range.addEventListener('change', rangeChange.bind(this));
range.addEventListener('input', rangeInput.bind(null, msg)); range.addEventListener('input', rangeInput.bind(null, msg));
@ -132,33 +132,64 @@ Visualizer.prototype.initRange = function() {
* *
*/ */
Visualizer.prototype.initControls = function() { Visualizer.prototype.initControls = function() {
var updatePlayPause = function(paused) {
if (paused === true) {
play.className = 'fa fa-pause';
}
else {
play.className = 'fa fa-play';
}
};
var playclick = function() {
this.paused = !this.paused;
updatePlayPause(this.paused);
this.followInstruction(this.instruction + 1);
};
var backclick = function() {
this.paused = true;
this.followInstruction(this.instruction - 1);
};
var forwardclick = function() {
this.paused = true;
this.followInstruction(this.instruction + 1);
};
var restartclick = function() {
this.paused = false;
updatePlayPause(this.paused);
this.followInstruction(0);
};
var play = document.createElement('button'); var play = document.createElement('button');
play.className = 'fa fa-play'; play.className = 'fa fa-play';
play.title = 'Play' play.title = 'Play'
play.addEventListener('click', onclick); play.addEventListener('click', playclick.bind(this));
var back = document.createElement('button'); var back = document.createElement('button');
back.className = 'fa fa-step-backward'; back.className = 'fa fa-step-backward';
back.title = 'Step Backward'; back.title = 'Step Backward';
back.addEventListener('click', onclick); back.addEventListener('click', backclick.bind(this));
var forward = document.createElement('button'); var forward = document.createElement('button');
forward.className = 'fa fa-step-forward' forward.className = 'fa fa-step-forward'
forward.title = 'Step Forward'; forward.title = 'Step Forward';
forward.addEventListener('click', onclick); forward.addEventListener('click', forwardclick.bind(this));
var reset = document.createElement('button'); var reset = document.createElement('button');
reset.className = 'fa fa-fast-backward'; reset.className = 'fa fa-fast-backward';
reset.title = 'Restart' reset.title = 'Restart'
reset.addEventListener('click', onclick); reset.addEventListener('click', restartclick.bind(this));
var stat1 = document.createElement('div'); var stat1 = document.createElement('div');
stat1.className = 'stat'; stat1.className = 'stat';
stat1.innerHTML = 'Swaps: 999'; stat1.innerHTML = 'Comparisons: 999';
var stat2 = document.createElement('div'); var stat2 = document.createElement('div');
stat2.className = 'stat'; stat2.className = 'stat';
stat2.innerHTML = 'Comparisons: 999'; stat2.innerHTML = 'Swaps: 999';
var container = document.createElement('div'); var container = document.createElement('div');
container.className = 'controls-container'; container.className = 'controls-container';

@ -61,6 +61,9 @@ function Visualizer(parent) {
// this.currentInstruction = 0; // this.currentInstruction = 0;
this.initItems(10); this.initItems(10);
this.instruction = 0;
this.paused = false;
}; };
// Public static properties (mutable) // Public static properties (mutable)
@ -77,6 +80,8 @@ Visualizer.prototype.followInstruction = function(index) {
return; return;
} }
this.instruction = index;
var i = this.sorter.instructions[index]; var i = this.sorter.instructions[index];
console.log(i); console.log(i);
@ -84,11 +89,9 @@ Visualizer.prototype.followInstruction = function(index) {
if (typeof this[i[0]] === 'function') { if (typeof this[i[0]] === 'function') {
this[i[0]](i); this[i[0]](i);
if (i[1] === true) { if (this.paused === false) {
setTimeout(this.followInstruction.bind(this, index + 1), 400); var delay = i[1];
} setTimeout(this.followInstruction.bind(this, index + 1), delay);
else {
this.followInstruction(index + 1);
} }
} }
// TODO document this // TODO document this

Loading…
Cancel
Save