sort
by chris richarde
HTML
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<input type="button" id="AddLink" value="Bubble Sort" onClick="bubbleSort();" />
<input type="textbox" id="LinkName" value="A" /><br/>
<input type="button" id="AddLink" value="Insert String" onClick="addNode();" />
<p id="output"></p>
<p id="output2"></p>
<p id="output3"></p>
JavaScript
var list = new List();
function lets(content, last) {
this.content = content;
this.last = last;
this.next = null;
this.id = 0;
this.id_previous = 0;
return this;
}
function List(content) {
this.length = 0;
this.head = null;
this.last = this.head;
}
function createList() {
for (i=0; i<20; i++){
list.addNode(ran());
}
document.getElementById("output").innerHTML = list.print();
document.getElementById("output2").innerHTML = a;
}
function bubbleSort() {
list.bubbleSort();
document.getElementById("output").innerHTML = list.print();
}
function addNode() {
var value = document.getElementById("LinkName").value;
list.addNode(value);
list.bubbleSort();
document.getElementById("output").innerHTML = list.print();
}
List.prototype.addNode =
this.add = function (content) {
if (this.head == null) {
this.head = new lets(content);
this.length++;
this.id++;
return this.head;
}
if (this.last == null) {
this.last = new lets(content);
this.head.next = this.last;
this.length++;
this.id++;
return this.last;
}
this.last.next = new lets(content);
this.last = this.last.next;
this.last.next = null;
this.length++;
this.id++;
this.id_previous++;
return this.last;
lets.prototype.asString = function() {
return "Node Value:" + this.last + "<br/>";
}
}
function ran()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for( var i=0; i < 2; i++ ){
text += possible.charAt(Math.floor(Math.random() * possible.length));}
return text;
}
lets.prototype.asString = function() {
return this.content + "<br/>";
}
List.prototype.swap = function(x, y) {
var temp = x.content;
x.content = y.content;
y.content = temp;
}
List.prototype.bubbleSort = function() {
var swapped;
do {
swapped = false;
var link = this.head;
while (link.next != null) {
var a = link;
var b = link.next;
if (a.content > b.content) {
this.swap(a, b);
...