JSFiddle - React, Tailwind, and code Playground
by MimiE
HTML
<h4>
Sorting: Assignment 8
</h4>
Enter a String Here:
<input type="textbox" id="nInput" />
<input type="button" value="Insert String" onclick="insertSort();">
<br/><br/>
<input type="button" value="Randomized List" onclick="createRandomList();" /><input type="button" value="Merge Sort" onclick="sortedList();" />
<!--<input type="button" value=" QuickSort" onclick="QuickSort();" />-->
<div id="output1"></div>
<div id="output2"></div>
<div id="output"></div>
JavaScript
function List() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.next = null; //next node in list
this.prev = null; //previous node in list
//this.data = _data;
this.data = null;
}
//var list = new List();
List.prototype.addNode = function(_data) {
var node = new Node();
node.data = _data;
if (this.head == null) {
this.head = node;
this.tail = this.head;
//this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
List.prototype.push = function(_data) {
var node = new Node(_data);
var cnode = this.head;
if (!cnode) {
this.head = node;
this.length++;
return node;
}
while (cnode.next) {
cnode = cnode.next;
}
cnode.next = node;
this.length++;
return node;
}
List.prototype.pop = function() {
var length = this.length;
if (this.head == null) {
return null;
}
var popNode = this.head;
this.head = this.head.next;
this.length--;
return popNode.data;
}
List.prototype.toString = function() {
if (this.head == null) {
return "List is Empty";
}
var str = "";
var node = this.head;
while (node != null) {
str += node.data + ", " + "<br/>";
node = node.next;
}
return str;
}
var list = new List();
function merge(_list) {
debugger;
var length = _list.length;
if (length < 2) return _list;
var left = new List();
var right = new List();
var mid = length / 2;
var node = _list.head;
var i = 0;
while (node != null) {
if (i <= mid) {
left.push(node.data);
} else {
right.push(node.data);
}
i++;
node = node.next;
}
left = merge(left);
right = merge(right);
return mergeS(left, right);
}
function mergeS(left, right) {
var sorted = new...