A8

forked from 4

by Ebony McCoy

HTML

<input type="button" value="Create Node List" onclick="createList(20)" />
<br>

<br>
<br/>Enter Content Here:
<input type="textbox" id="content" />

<input type="button" value="Add Node to List" onclick="addNode()" />

<br>
<br/>
<input type="button" onclick=doMergeSort(); value="Merge Sort">
<br>
<br>

<input type="button" onclick=insertionSort(); value="Insertion Sort">
<br>
<br>
<p id="output1"></p>
<p id="output2"></p>
<p id="output3"></p>
<p id="output4"></p>

CSS

function doquickSort(left, right) {
  if (DoublyList.length == 0) createList(5);
  document.getElementById("output4").innerHTML += "<br/> Quick Sorted List:" +
    quickSort(DoublyList).print();
  //displayList();

}

function quickSort() {
  var size = DoublyList.length;
  var pivot = this.tail;
  var partitionIndex = left;
  var left = this.last;
  var right = this.next;
  
  if (left > right) {
    pivot = right;
    partitionIndex = partition(pivot, left, right);

    left = quickSort(left, partitionIndex - 1);
    right = quickSort(partitionIndex + 1, right);
  }
  return partition(left, right);
}

function partition(pivot, left, right) {
  var pivotValue = pivot.content;
  partitionIndex = left;

  for (var i = left; i < right; i++) {
    if (i < pivotValue) {
      swap(i, partitionIndex);
      partitionIndex++;
    }
  }
  swap(right, partitionIndex);
  return partitionIndex;
}

function swap(i, j) {
  var temp = i;
  i = j;
  j = temp;
}

JavaScript

function LinkedList() {

  this.head = null;
  this.tail = null;
  this.length = 0;
}

function Node() {
  this.last = null;
  this.next = null;
  this.content = null;

  return this;
}

LinkedList.prototype.add = function(_content) {
  var node = new Node();
  node.content = _content;

  if (this.head == null) {
    this.head = node;
    this.tail = node;
    this.length = 1;
    return node;
  }

  if (this.tail == null) {
    this.tail = node;
    this.tail.last = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  }
  this.tail.next = node;
  node.last = this.tail;
  this.tail = node;
  this.length++;
  return node;
}

LinkedList.prototype.push = function(_content) {
  this.add(_content);
  //var node = new Node(_content);
  // node.content = _content;

  //if (this.head == null) {
  // this.head = node;
  //this.tail = node;
  // 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;

}

LinkedList.prototype.pop = function() {
  if (this.head == null)
    return null;

  if (this.head == this.tail) {
    var temp = this.head;
    this.head = null;
    this.tail = null;
    this.length = 0;
    return temp;
  }
  var oldtail = this.tail;
  var newtail = this.tail.last;

  newtail.next = null;
  this.tail = newtail;
  this.length--;

  return oldtail;
}

LinkedList.prototype.dequeue = function() {

  if (this.head == null)
    return null;


  if (this.head == this.tail) {
    var temp = this.head;
    this.head = null;
    this.tail = null;
    this.length = 0;

    return temp;
  }
  var oldhead = this.head;
  this.head = this.head.next;

  this.length--;
  return oldhead;

}

LinkedList.prototype.print = function() {
  if (this.length == 0) return "Empty...