Assignment 8 Sorting
by austinmillett
October 21, 2018
HTML
<!-- Heading 1 -->
<h2> Austin Millett </h2>
<!-- Heading 2 -->
<h3> Assignment 8 - Sorting </h3>
<!-- Button for Run Insertion Sort -->
<input type = "button" id = "Insertion" onclick = "InsertionSort()" value = "Run Insertion Sort" /><br><br>
<!-- Button for Run Bubble Sort -->
<input type = "button" id = "Bubble" onclick = "BubbleSort()" value = "Run Bubble Sort" /><br><br>
<!-- Textbox -->
Enter a value to add to the list the Rerun your desired Sort:
<input type="text" id="yourNumber" /><br><br>
<!-- Div's for Outputs 1,2, and 3 -->
Output-1:Unsorted List <br>
<div id = "output1"></div><br>
Output-2:Sorting List<br>
<div id = "output2"></div><br>
Output-3:Sorted List<br>
<div id = "output3"></div>
CSS
/* Design for Run Insertion Sort button */
#Insertion {
background-color: black;
border: 2px solid;
color: white;
padding: 4px 8px;
text-align: center;
font-size: 15px;
}
/* Design for Run Bubble Sort button */
#Bubble {
background-color: black;
border: 2px solid;
color: white;
padding: 4px 8px;
text-align: center;
font-size: 15px;
}
JavaScript
//First is Insertion, Second is Bubble
var listString = "";
var listString2 = "";
//Doubly linked list
function Node(value) {
this.data = value;
this.previous = null;
this.next = null;
}
function List() {
this._length = 0;
this.head = null;
this.tail = null;
}
List.prototype.add = function(value) {
var node = new Node(value);
if (this._length) {
this.tail.next = node;
node.previous = this.tail;
this.tail = node;
} else {
this.head = node;
this.tail = node;
}
this._length++;
return node;
};
Node.prototype.toText = function() {
var node = this;
var str ="";
while (node != null) {
str += node.data + ",";
node = node.next;
}
return str;
}
List.prototype.isEmpty = function() {
if (this.head == null){
return true;
}
else return false;
}
//Creating a linked list with "20" random numbers
function GenerateLLwithString(){
var ll = new List();
for( var i=0; i < 20; i++ ){
var tempString = (Math.floor(Math.random() * (100 - 1)) + 1).toString();
ll.add(tempString);
}
return ll;
}
function insertionSort(node) {
if (node == undefined)
return undefined;
//Initialize sortedList as the first node.
var sortedList = node;
listString = listString + sortedList.toText() + "<br>";
node = node.next;
sortedList.next = undefined;
while(node != undefined) {
var current = node;
node = node.next;
if ((current.data.localeCompare(sortedList.data) < 0)) {
current.next = sortedList;
sortedList = current;
listString = listString + sortedList.toText() + "<br>";
} else {
var search = sortedList;
while(search.next != undefined && (current.data.localeCompare(search.next.data) > 0))
search = search.next;
//Current is after search.
current.next = search.next;
search.next = current;
listString = listString + sortedList.toText() + "<br>";
}
}
document.getElementById('output2').innerHTML = listString;
return sortedList;
}
//Insertion sort Function
function InsertionSort(){
document.getElementById('output1').innerHTML = "";
document.getElementById('output2').innerHTML =...