Daniel Eberhart A8

SORTING

by Daniel Eberhart

HTML

<input type="button" value="Repopulate Randomly" onClick="repopulateRandomly();" />
<br/>
<input type="button" value="Merge Sort" onClick="CallMergeSort();" />
<br/>
Once the Chain/List is sorted<br/>


(1)  insert elements into the correct location in the Chain based on sort order

=====> Value to Insert: <input type="textbox" id="valueToInsert" value="8" size="3"/>
<input type="button" value="Insertion Sort" onClick="CallInsertSort();" />

<p id='output1'></p>
<p id='output2'></p>
<p id='reportOut'></p>

JavaScript

var Node = function(_content) {
  this.next = null;
  this.previous = null;
  this.content = _content;
}
var Queue = function() {
  this.front = null;
  this.back = null;



  // was  addToBack, now called addToFront
  this.addToFront = function(_content) {
    // No front - create one
    if (this.front == null) {
      this.front = new Node(_content);
      this.back = this.front;
      return this;
    }
    //   create a new node to be the back
    var addedNode = new Node(_content);
    //   a) store pointer to current back
    //      as this.back of new node
    addedNode.next = this.front;
    //   b) store pointer to new back
    //     as this.back.next of current back
    this.front.previous = addedNode;
    //   c) store pointer to new back
    //     as this.back which becomes current back
    this.front = addedNode;        // which becomes new back
    return this;
  }

  // now addToBack adds to the back  
  this.addToBack = function(_content) {
    // No front - create one
    if (this.front == null) {
      this.front = new Node(_content);
      this.back = this.front;
      return this;
    }
    //   create a new node to be the back
    var addedNode = new Node(_content);
    //   a) store pointer to current back
    //      as this.back of new node
    addedNode.previous = this.back;
    //   b) store pointer to new back
    //     as this.back.next of current back
    this.back.next = addedNode;
    //   c) store pointer to new back
    //     as this.back which becomes current back
    this.back = addedNode;        // which becomes new back
    return this;
  }


  this.insertAfter = function(valueToAdd) {
    var listX = "";
    var node = this.back;

    if(valueToAdd < this.front.content){
      return "goesInFront";
    }
    if(valueToAdd > this.back.content){
      return  "goesInBack";
    }
    // This goes through the queue from the back to the front
    while (node != null) {
      if(valueToAdd > node.content){
        //   create...