Sorting Functions
restart
slow
normal
fast
Selection Sort
Insertion Sort
Source Code
<script type="text/javascript">
function initializeCanvas(id) {
var canvas = document.getElementById(id);
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
var width = Math.floor(window.innerWidth / 6) - 5;
var height = Math.floor(width * 9 / 16);
ctx.canvas.width = width;
ctx.canvas.height = height;
ctx.strokeStyle = '#AAAAAA';
return ctx;
}
}
function drawCanvas(ctx, arr) {
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var lineWidth = width / arr.length;
ctx.clearRect(0, 0, width, height);
for (var i = 0; i < arr.length; i++) {
var lineHeight = Math.floor(height * arr[i] / arr.length);
var x = Math.floor(i * lineWidth);
ctx.beginPath();
ctx.moveTo(x, height);
ctx.lineTo(x, height - lineHeight);
ctx.stroke();
}
}
function shuffle(arr) {
for (var i = 0; i < arr.length; i++) {
var j = Math.floor(Math.random() * (i + 1));
var x = arr[j];
arr[j] = arr[i];
arr[i] = x;
}
return arr;
}
this.delay = 100;
function setDelay(delay) {
this.delay = delay;
}
this.arraySize = 100;
var SelectionSort = function(ctx, arr) {
this.ctx = ctx;
this.arr = arr;
this.i = 0;
this.done = false;
this.next = function() {
if (!this.done) {
var min = this.i;
for (var j = this.i; j < this.arr.length; j++) {
if (this.arr[j] < this.arr[min]) min = j;
}
var x = this.arr[min];
this.arr[min] = this.arr[this.i];
this.arr[this.i] = x;
this.i = this.i + 1;
if (this.i >= this.arr.length) this.done = true;
}
return this.arr;
}
}
this.selectionSort;
function selectionSortLoop() {
selectionSort.next();
drawCanvas(selectionSort.ctx, selectionSort.arr);
if (!selectionSort.done) setTimeout('selectionSortLoop()', this.delay);
}
function selectionSortStart() {
var size = arraySize;
var arr = [];
for (var i = 0; i < size; i++) {
arr[i] = i;
}
arr = shuffle(arr);
var ctx = initializeCanvas('canvasSelectionSort');
if (ctx != undefined) {
selectionSort = new SelectionSort(ctx, arr);
drawCanvas(ctx, arr);
setTimeout('selectionSortLoop()', this.delay);
}
}
var InsertionSort = function(ctx, arr) {
this.ctx = ctx;
this.arr = arr;
this.i = 0;
this.done = false;
this.next = function() {
if (!this.done) {
for (var j = this.i - 1; !(j < 0 || this.arr[j] <= this.arr[j + 1]); j--) {
var x = this.arr[j];
this.arr[j] = this.arr[j + 1];
this.arr[j + 1] = x;
}
this.i = this.i + 1;
if (this.i >= this.arr.length) this.done = true;
}
return this.arr;
}
}
this.insertionSort;
function insertionSortLoop() {
insertionSort.next();
drawCanvas(insertionSort.ctx, insertionSort.arr);
if (!insertionSort.done) setTimeout('insertionSortLoop()', this.delay);
}
function insertionSortStart() {
var size = arraySize;
var arr = [];
for (var i = 0; i < size; i++) {
arr[i] = i;
}
arr = shuffle(arr);
var ctx = initializeCanvas('canvasInsertionSort');
if (ctx != undefined) {
insertionSort = new InsertionSort(ctx, arr);
drawCanvas(ctx, arr);
setTimeout('insertionSortLoop()', this.delay);
}
}
function start() {
setTimeout('selectionSortStart()', 100);
setTimeout('insertionSortStart()', 100);
}
window.onload = start;
</script>
<h1>Sorting Functions</h1>
<a id="restart" href="javascript:start()">restart</a>
<a id="slowSpeed" href="javascript:setDelay(1000)">slow</a>
<a id="normalSpeed" href="javascript:setDelay(100)">normal</a>
<a id="fastSpeed" href="javascript:setDelay(0)">fast</a>
<div>
<div style="float:left">
<h3>Selection Sort</h3>
<canvas id="canvasSelectionSort" width="200" height="200">
<p>Canvas is not supported on this browser.</p>
</canvas>
</div>
<div style="float:left">
</div>
<div style="float:left">
<h3>Insertion Sort</h3>
<canvas id="canvasInsertionSort" width="200" height="200">
<p>Canvas is not supported on this browser.</p>
</canvas>
</div>
</div>
<div style="clear:both">
</div>