Sortin 2
Ass 8
by Jake Jernigan
HTML
Add to List <input type="textbox" id="content"/>
<input type="button" id = "addToSortedList" onclick="addToList();" value = "Add to List"/>
<br/>
<p id="list"></p>
<input type="button" id="generateRandomList" onclick="generateRandomList()" value="Generate Random List"/><br/><br/>
<input type="button" id="bubbleSort" onclick="bubbleSort()" value="Bubble sort"/><br/><br/>
<br/>
<p id="steps"></p>
JavaScript
var List = function() {
this.head = null;
}
var Node = function(_content) {
this.content = _content;
this.next = null;
}
List.prototype.add = function(_content) {
var node = new Node(_content);
if (this.head == null) {
this.head = node;
return this;
}
if (node.content < this.head.content) {
node.next = this.head;
this.head = node;
return this;
}
this.head.add(this.head, node);
return this;
}
Node.prototype.add = function(p, n){
// put in front if less than
if (n.content < this.content) {
p.next = n;
n.next = this;
return this;
}
if (this.next == null) {
p.next = this;
this.next = n;
return this;
}
return this.next.add(this, n);
}
Node.prototype.sort = function() {
if (this.next == null) {return null;}
if (this.next.next == null) {return null;}
var i = this.next;
var j = this.next.next;
if (b=j.content < i.content) {
this.next = j;
i.next = j.next;
j.next = i;
}
}
List.prototype.bubbleSort = function() {
if (this.head == null) {return this;}
var a = this.head;
var b = this.head.next;
var c = this.head.next;
for (var i = 1; i < 21; i++){
if (b.content < a.content){
a.next = b.next;
b.next = a;
this.head = b;
}
var current = this.head;
while (current.next != null && current.next.next != null) {
current.sort();
current = current.next;
}
}
}
List.prototype.create = function(n) {
for (var i = 1; i < n; i++) {
var letter = String.fromCharCode(65 + Math.floor(Math.random() * 26));
this.addUnsorted(letter);
}
}
List.prototype.addUnsorted = function(_content) {
if (this.head == null) {
this.head = new Node(_content);
return this.head;
}
var currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = new Node(_content);
return...