Sort
Assignment 8
by Alan Harris
HTML
<label>Type in something: </label><br>
<input type="text" id="nodeinput" value="anything">
<button onclick="addNode();">
Insert String
</button><br><br>
<button onclick="random();">
Put random strings into list
</button><br>
<button onclick="MergeSort();">
Sort with algorithm 1: MergeSort
</button><br>
<button onclick="QuickSort();">
Sort with algorithm 2: QuickSort
</button><br>
<button onclick="clearScreen();">
Clear List and Screen
</button>
<div id="print"></div><br>
<div id="sort">
</div>
CSS
body {
background-color:grey;
}
label {
color:white;
}
button:focus {
border: 1px solid black;
padding: 4px 4px;
}
button {
background-color: grey;
border: 1px solid black;
color: white;
padding: 4px 8px;
text-decoration: none;
margin: 4px 2px;
}
button:hover {
background-color:black;
}
#print {
background-color:grey;
color:white;
}
#sort {
background-color:#656565;
color:white;
}
input[type=text] {
background-color: grey;
color: white;
padding: 4px 10px;
margin: 1px 1;
box-sizing: border-box;
border:1px solid #303030;
}
input[type=text]:focus {
background-color: #303030;
}
JavaScript
var ANode = new Node();
function Node(input) {
// this.id = null;
this.content = input;
this.next = null;
this.last = null;
return this;
}
function List1(input) {
this.head = new Node(input);
this.length = 1;
this.tail = this.head;
this.add = function(ANode) {
var element = new Node();
element.content = ANode;
if (this.head == null){
this.head = element;
this.length++;
return element;
}
if (this.tail == null) {
this.tail = element;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length++;
return element;
}
this.tail.next = element;
element.prev = this.tail;
this.tail = element;
this.length++;
return element;
}
this.pop = function() {
//if (this.head == null) {
// alert("Empty Stack");
// this.length = 1;
//return this;
//}
if (this.head == this.tail) {
var a = this.head;
// this.tail = null;
this.head = null;
this.length--;
return a;
}
var d = this.tail;
this.tail = d.prev;
this.length--;
return d;
}
this.print = function() {
if (this.head == null) {
return "List is Empty."
}
var p = "";
var aNode = this.head;
while(aNode != null) {
p += aNode.content + " ";
aNode = aNode.next;
}
return p;
}
this.clear = function() {
this.head = null;
this.tail = null;
this.length = 1;
this.next = null;
this.last = null;
}
}
var data = new List1();
function clearScreen() {
var c = "";
data.clear();
document.getElementById("print").innerHTML = c;
}
function mer(list) {
var L = new List1();
var R = new List1();
if(list.head == null) {
alert("List is empty, unable to sort.");
}
var length = list.length;
var node = list.head;
var i = 0;
while(node != null) {
if (i < length / 2) {
L.add(node.content);
}
else {
R.add(node.content);
}
i++;
node = node.next;
}
merge(L, R);
}
function merge(L, R) {
var sorted = new List1();
while...