COP3530 Assignment 8 - Sorting
by joseph_kanawall2400
HTML
<input type="button" Value="Create Randomized String List" onClick="createRandomStringList();" />
<input type="button" Value="Sort with Bubble Sort" onClick="bubbleSort();" />
<input type="button" Value="Sort with Selection Sort" onClick="selectionSort();" />
<input type="textbox" id="item_tb" placeholder="Enter String Here" />
<input type="button" id="insert_btn" Value="InsertString" onClick="insertString();" />
<br/>
<div id="output" />
JavaScript
// Node Functions
function Node(id, content, last)
{
this.id = id;
this.next = null;
this.last = last;
this.content = content;
this.toString = function()
{
return "Node <ID:" + this.id + ", Value:'" + this.content + "'>";
}
this.clone = function()
{
var newNode = new Node(this.id, this.content, this.last);
newNode.next = this.next;
return newNode;
}
}
// List Functions
function List()
{
this.head = null;
this.length = 0;
this.append = function(content)
{
// Go to end of list if length is not 0
if (this.head != null)
{
this.goto_end();
}
// Create new node
var node = new Node(this.length, content, null);
// Check if new node is first node
if (this.head != null)
{
node.last = this.head;
this.head.next = node;
}
// Update list properties
this.head = node;
this.length++;
}
this.goto_begin = function()
{
// Reset head to beginning of list
while (this.previous()) {}
}
this.goto_end = function()
{
// Traverse list until next is null
while (this.next()) {}
}
this.previous = function()
{
if (this.head.last != null)
{
this.head = this.head.last;
return true;
}
else
{
return false;
}
}
this.next = function()
{
if (this.head.next != null)
{
this.head = this.head.next;
return true;
}
else
{
return false;
}
}
this.clear = function()
{
this.head = null;
this.length = 0;
}
this.clone = function()
{
var newList = new List();
this.goto_begin();
do
{
newList.append(this.head.content);
}
while (this.next());
this.goto_begin();
return newList;
}
this.index = function(nodeIndex)
{
var nodeToReturn = null;
this.goto_begin();
do
{
if(this.head.id == nodeIndex)
{
nodeToReturn = this.head;
}
}
while (this.next());
this.goto_begin();
return nodeToReturn;
}
this.swap = function(node1, node2)
{
if(node1 == this.head)
{
this.head = node2;
}
var tn1 = node1.clone();
var...