parent
83c367cdc5
commit
78078f1a65
13 changed files with 398 additions and 181 deletions
@ -1,17 +0,0 @@ |
|||||||
rules: |
|
||||||
indent: |
|
||||||
- 2 |
|
||||||
- 4 |
|
||||||
quotes: |
|
||||||
- 2 |
|
||||||
- single |
|
||||||
linebreak-style: |
|
||||||
- 2 |
|
||||||
- unix |
|
||||||
semi: |
|
||||||
- 2 |
|
||||||
- always |
|
||||||
env: |
|
||||||
es6: true |
|
||||||
browser: true |
|
||||||
extends: 'eslint:recommended' |
|
@ -1,6 +0,0 @@ |
|||||||
/** |
|
||||||
* Debugging: try running "npm cache clear" if things aren't working as expected. |
|
||||||
*/ |
|
||||||
module.exports = function(grunt) { |
|
||||||
require('load-grunt-config')(grunt); |
|
||||||
}; |
|
@ -1,2 +0,0 @@ |
|||||||
default: |
|
||||||
- 'concurrent:watch' |
|
@ -1,18 +0,0 @@ |
|||||||
/** |
|
||||||
* CONCURRENT |
|
||||||
* ========== |
|
||||||
* Runs several multiple blocking tasks at once (like "watch"). |
|
||||||
* http://stackoverflow.com/questions/17585385/how-to-run-two-grunt-watch-tasks-simultaneously
|
|
||||||
*/ |
|
||||||
|
|
||||||
module.exports = { |
|
||||||
options: { |
|
||||||
logConcurrentOutput: true, |
|
||||||
}, |
|
||||||
watch: { |
|
||||||
tasks: [ |
|
||||||
//'nodemon',
|
|
||||||
'watch' |
|
||||||
], |
|
||||||
} |
|
||||||
}; |
|
@ -1,8 +0,0 @@ |
|||||||
/** |
|
||||||
* ESLINT |
|
||||||
* ====== |
|
||||||
* Does not support 'subtasks' (like :all). |
|
||||||
*/ |
|
||||||
module.exports = { |
|
||||||
target: ['js/*.js'] |
|
||||||
}; |
|
@ -1,18 +0,0 @@ |
|||||||
/** |
|
||||||
* WATCH |
|
||||||
* ===== |
|
||||||
* Process for watching changes to files in real time. |
|
||||||
*/ |
|
||||||
module.exports = { |
|
||||||
js: { |
|
||||||
files: [ |
|
||||||
'js/**/*.js' |
|
||||||
], |
|
||||||
tasks: [ |
|
||||||
'eslint' |
|
||||||
], |
|
||||||
options: { |
|
||||||
livereload: true |
|
||||||
}, |
|
||||||
}, |
|
||||||
}; |
|
@ -1,56 +1,137 @@ |
|||||||
<!DOCTYPE html> |
<!DOCTYPE html> |
||||||
<html lang="en"> |
<html lang="en"> |
||||||
<head> |
<head> |
||||||
<meta charset="UTF-8"> |
<meta charset="UTF-8"> |
||||||
<title>D3</title> |
<title>D3</title> |
||||||
|
|
||||||
<script type='text/javascript' src='vendor/d3/d3.min.js'></script> |
<link rel="stylesheet" href="vendor/fontawesome/css/font-awesome.css"> |
||||||
|
|
||||||
<style type='text/css'> |
<style type='text/css'> |
||||||
html, body { |
html, body { |
||||||
|
font-family:sans-serif; |
||||||
margin:0; |
margin:0; |
||||||
} |
} |
||||||
|
|
||||||
.quicksort-random { |
svg { |
||||||
display:block; |
border:10px solid dodgerblue; |
||||||
height:60px; |
width:940px; |
||||||
margin:0 auto; |
} |
||||||
width:1198px; |
|
||||||
|
.sorter { |
||||||
|
border:1px solid #e7e7e7; |
||||||
|
margin:20px auto; |
||||||
|
padding:10px; |
||||||
|
width:960px; |
||||||
|
} |
||||||
|
|
||||||
|
.slider { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
.top { |
||||||
|
background:#f88; |
||||||
|
} |
||||||
|
|
||||||
|
.bottom { |
||||||
|
background:#4e0; |
||||||
|
height:100px; |
||||||
|
} |
||||||
|
|
||||||
|
button { |
||||||
|
border:1px solid #333; |
||||||
|
background:#888; |
||||||
|
padding:3px; |
||||||
|
} |
||||||
|
|
||||||
|
button.fa { |
||||||
|
cursor:pointer; |
||||||
|
font-size:12px; |
||||||
|
height:20px; |
||||||
|
line-height:14px; |
||||||
|
margin-right:5px; |
||||||
|
transition:background 0.2s ease; |
||||||
|
width:20px; |
||||||
|
} |
||||||
|
|
||||||
|
button:hover { |
||||||
|
background:#aaa; |
||||||
} |
} |
||||||
</style> |
</style> |
||||||
</head> |
</head> |
||||||
<body> |
<body> |
||||||
|
<h1>Quicksort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
|
||||||
|
|
||||||
|
<h1>Mergesort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
|
||||||
|
|
||||||
|
<!-- <h1>Shellsort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
|
||||||
|
|
||||||
|
<h1>Heapsort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
|
||||||
|
<h1>Bubblesort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
|
||||||
|
<h1>Radixsort discussion</h1> |
||||||
|
|
||||||
|
<div class="sorter"></div> |
||||||
|
--> |
||||||
|
|
||||||
|
|
||||||
|
<script type='text/javascript' src='vendor/d3/d3.min.js'></script> |
||||||
<script type='text/javascript' src='js/visualizer.js'></script> |
<script type='text/javascript' src='js/visualizer.js'></script> |
||||||
<script type='text/javascript' src='js/sorter.js'></script> |
<script type='text/javascript' src='js/sorter.js'></script> |
||||||
|
<script type='text/javascript' src='js/quicksort.js'></script> |
||||||
|
<script type='text/javascript' src='js/mergesort.js'></script> |
||||||
|
|
||||||
<script type='text/javascript'> |
<script type='text/javascript'> |
||||||
var data = []; |
// var QS = new Quicksort(); |
||||||
|
// QS.addInstruction(); |
||||||
// TODO namespace this |
|
||||||
(function populate() { |
var MS = new Mergesort(); |
||||||
var len = 50; |
|
||||||
var r, g, b; |
var data = MS.generate(15); |
||||||
for (var i = 0; i < len; i++) { |
var a = [] |
||||||
data.push({ |
data.forEach(function(obj0) { |
||||||
val: Math.floor(i * 255 / len), |
a.push(obj0.value); |
||||||
fill: `rgb(0, 0, ${Math.floor(i * 255 / len)})`, |
}) |
||||||
fade: 'rgb(220, 220, 220)', |
console.log('DATA: ' + a.join(',')); |
||||||
}); |
|
||||||
}; |
var shuffled = MS.shuffle(data); |
||||||
}()); |
var b = [] |
||||||
|
shuffled.forEach(function(obj0) { |
||||||
var S = new Sorter(); |
b.push(obj0.value); |
||||||
S.shuffle(data); |
}); |
||||||
|
console.log('SHUFFLED: ' + b.join(',')); |
||||||
var V = new Visualizer(); |
|
||||||
V.init(data); |
var ordered = Object.create(shuffled); |
||||||
V.addMarker('quicksort-left-marker'); |
MS.sort(ordered, 0, ordered.length - 1); |
||||||
V.moveMarker('quicksort-left-marker', 2); |
var c = [] |
||||||
|
ordered.forEach(function(obj0) { |
||||||
var temp = Object.create(data); |
c.push(obj0.value); |
||||||
S.quicksort(temp, 0, data.length - 1); |
}); |
||||||
temp = null; |
console.log('ORDERED: ' + c.join(',')); |
||||||
|
|
||||||
setTimeout(V.followInstruction.bind(V, S.instructions, 0), 500); |
|
||||||
|
// Wrap anonymous function to avoid polluting global namespace. |
||||||
|
// (function() { |
||||||
|
// var elements = document.querySelectorAll('.sorter'); |
||||||
|
// for (key in elements) { |
||||||
|
// if (elements.hasOwnProperty(key)) { |
||||||
|
// new Visualizer(elements[key]); |
||||||
|
// } |
||||||
|
// } |
||||||
|
// })(); |
||||||
</script> |
</script> |
||||||
</body> |
</body> |
||||||
</html> |
</html> |
||||||
|
@ -0,0 +1,53 @@ |
|||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
var Mergesort = function() { |
||||||
|
this.instructions = []; |
||||||
|
}; |
||||||
|
|
||||||
|
Mergesort.prototype = Object.create(Sorter.prototype); |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
Mergesort.prototype.addInstruction = function(operation, left, right, mid) { |
||||||
|
console.log({ |
||||||
|
operation: operation, |
||||||
|
left: left, |
||||||
|
right: right, |
||||||
|
mid: mid |
||||||
|
}); |
||||||
|
|
||||||
|
this.instructions.push({ |
||||||
|
operation: operation, |
||||||
|
left: left, |
||||||
|
right: right |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
var count = 0; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
Mergesort.prototype.sort = function(arr, start, end) { |
||||||
|
count++; |
||||||
|
|
||||||
|
if (count > 30) { |
||||||
|
console.error('count exceeded'); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (start >= end) { |
||||||
|
this.addInstruction('single', start, end, null); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
var mid = start + Math.floor((end - start) / 2); |
||||||
|
// this.addInstruction('init', start, end, mid);
|
||||||
|
|
||||||
|
|
||||||
|
this.sort(arr, start, mid); |
||||||
|
this.sort(arr, mid + 1, end); |
||||||
|
return; |
||||||
|
}; |
@ -0,0 +1,67 @@ |
|||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
var Quicksort = function() { |
||||||
|
this.instructions = []; |
||||||
|
}; |
||||||
|
|
||||||
|
Quicksort.prototype = Object.create(Sorter.prototype); |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
Quicksort.prototype.addInstruction = function(operation, left, right, pivot) { |
||||||
|
this.instructions.push({ |
||||||
|
operation: operation, |
||||||
|
left: left, |
||||||
|
right: right, |
||||||
|
pivot: pivot |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
// NOTE adds to an instruction set
|
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
Quicksort.prototype.sort = function(arr, start, end) { |
||||||
|
if (end - start <= 0) { |
||||||
|
this.addInstruction('single', start, end, null); |
||||||
|
return this.visualizer; |
||||||
|
} |
||||||
|
|
||||||
|
var left = start; |
||||||
|
var right = end; |
||||||
|
|
||||||
|
var pivot = Math.floor((right + left) / 2); |
||||||
|
var pivotval = arr[pivot].value; |
||||||
|
var tmp; |
||||||
|
|
||||||
|
this.addInstruction('init', left, right, pivot); |
||||||
|
|
||||||
|
while (left <= right) { |
||||||
|
while (arr[left].value < pivotval) { |
||||||
|
left++; |
||||||
|
this.addInstruction('inc-left', left, right, pivot); |
||||||
|
} |
||||||
|
|
||||||
|
while (arr[right].value > pivotval) { |
||||||
|
right--; |
||||||
|
this.addInstruction('dec-right', left, right, pivot); |
||||||
|
} |
||||||
|
|
||||||
|
if (left <= right) { |
||||||
|
tmp = arr[left]; |
||||||
|
arr[left] = arr[right]; |
||||||
|
arr[right] = tmp; |
||||||
|
|
||||||
|
this.addInstruction('swap', left, right, pivot); |
||||||
|
|
||||||
|
left++; |
||||||
|
right--; |
||||||
|
this.addInstruction('swap-inc-dec', left, right, pivot); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
this.sort(arr, start, right); |
||||||
|
this.sort(arr, left, end); |
||||||
|
}; |
Loading…
Reference in new issue