Assignment 8 - Sorting
by Mike Kerney
HTML
Mike Kerney<br/> Assignment 8 - Sorting<br/>
<br/>
<input type="button" value="Populate List" onclick="populateList()" />
<br/>
<br/>
<input type="button" value="BubbleSort" onclick="bubbleSort();" />
<br/>
<br/>
<input type="button" value="InsertionSort" onclick="insertsort();"/>
<br/>
<br/>
<input type="textbox" id="nodeValue" value="Enter Number" />
<input type="button" value="Insert Number" onclick="addNodeAndSort()" />
<br/>
<br/>
<div id="output">
</div>
<br/>
<div id="output2">
</div>
JavaScript
var s="";
var g = "";
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.ID = null;
this.next = null;
this.prev = null;
this.content = null;
}
LinkedList.prototype.add = function(_content) {
var node = new Node();
node.content = _content;
if (this.head == null) {
this.head = node;
this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length++;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
return node
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List";
var string = "";
var node = this.head;
while (node != null) {
string += node.content + " ";
node = node.next;
}
return string;
}
LinkedList.prototype.bubble = function(){
var node=this.head;
var length=this.length;
var val=0;
var count=0;
var flag=null;
do{
count=0;
node=this.head;
while(node.next != flag){
if(node.content > node.next.content){
count ++;
val = node.content;
node.content =node.next.content;
node.next.content = val;
}
node = node.next;
}
flag = node;
}
while (count)
}
LinkedList.prototype.InsertionSort=function(){
}
var aList = new LinkedList();
function bubbleSort(){
aList.bubble();
document.getElementById("output2").innerHTML=aList.print();
}
function insertsort(){
aList.insertSort();
document.getElementById("output2").innerHTML=aList.print();
}
function populateList() {
aList=new LinkedList();
for (i = 0; i < 20; i++) {
g = Math.floor(Math.random() * 1000);
aList.add(g);
document.getElementById("output").innerHTML = aList.print();
}
}
function addNodeAndSort(){
addNode();
aList.bubble();
document.getElementById("output").innerHTML=aList.print();
}
function addNode() {
var c =...