Sorting

You should automatically populate the List with 20 element (random strings). Once you have completed this – you will add 2 sort functions, you can use any sort method that you desire for each of the sort functions. You can also delineate the functions by the name of the Sort function – so your functions might be QuickSort() and MergeSort() – if you chose to implement those 2 algorithms (you can select from any sort functions). The interface should have buttons to (1) Repopulate the list with Random strings , (2) Sort list with selected algorithm 1 (3) Sort list with selected algorithm 2 and (4) Insert a new random string entered by the user into the list. After each operation it should display the new list.

by Neil Daley

HTML

<H2>
Sorting
</H2>

Let's test the sorting functions. <br/><br />I will test Merge Sort and Bubble Sort.<br/>
&#9830 Repopulate the list after adjusting List Size or entering a new string to insert.<br/>
&#9830 List Size MAX is 20 items.<br/>
&#9830 String insertion MAX is 5 characters. <br/><br/>

List Size:
<input type="textbox" id="inputValue" value="20" />
<br/>

Insert String:
<input type="textbox" id="inputToNode" value="abcde" onkeypress="handle(event)" />

<input type="button" id="btnInsert" value="Insert String" onClick="insertString()" style="color:white; background-color:blue" />
<br/>

<table>
  <tr>
    <th>
      <input type="button" id="btnRepop" value="Repopulate" onClick="createList()" style="color:white; background-color:blue" />
    </th>
    <th>
      <input type="button" id="btnSort1" value="Merge Sort" onClick="startMergeSort()" style="color:white; background-color:blue" />
    </th>
    <th>
      <input type="button" id="btnSort2" value="Bubble Sort" onClick="startBubbleSort()" style="color:white; background-color:blue" />
    </th>

</tr>
  <tr>
    <td>
      <div id="ntimer"></div>
      <p id="randList"></p>
    </td>
    <td>
      <div id="mtimer"></div>
      <p id="mergesortoutput"></p>

    </td>
    <td>
      <div id="btimer"></div>
      <p id="bubblesortoutput"></p>

    </td>

</tr>
</table>

<br/>

JavaScript

function handle(event) {
  var key = event.keyCode || event.which;
  if (key == 13) {
    //testInsertValue();
    //document.getElementById("inputToNode").value = "";
    insertString();
  }
}

var DoublyLinkedList = function() {
  this.head = null;
  this.tail = null;
  this.length = 0;

  this.clear = function() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  };

  // Adds Node to the tail of the List
  this.add = function(_content) {
    if (this.head == null) {
      this.head = new LinkedListNode(_content);
      this.length++;
      return this.head;
    }
    if (this.tail == null) {
      this.tail = new LinkedListNode(_content);
      this.head.next = this.tail;
      this.tail.last = this.head;
      this.length++;
      return this.tail;

    }
    this.tail.next = new LinkedListNode(_content);
    this.tail.next.last = this.tail;
    this.tail = this.tail.next;
    this.tail.next = null;
    this.length++;
    return this.tail;
  }
  
  // Push adds to the head of the List (like a Stack)
  this.push = function(_content) {
     var node = new LinkedListNode(_content);
     node.next = this.head;
     this.head.last = node;
     this.head = node;
     length++;
     return node;
  }

  this.addSorted = function(_content) {
     if (this.head == null) {return this.add(_content); }
     
     // Push onto top of node if it should be first element
     if (_content < this.head.content) {return this.push(_content);}
     
     // Go through each node and insert at sorted location
     var node = this.head;
     while (node.next != null) {
       if ((_content > node.content) && (_content <= node.next.content)) {
           var newNode = new LinkedListNode(_content);
           newNode.next = node.next;
           newNode.last = node;
           node.next = newNode;
           this.length++;
           return newNode;
       }
       node = node.next;
     }
     
     // If at last node - simply add to tail
     return...