Adding A sorted function

Question:
How can I add another sort function?

The assignment I am doing is asking me to add 2 sort functions, any sort method for each of the sort functions. Can also delineate the functions by the name of the Sort function - the function can be QuickSort() and MergeSort() - There can be buttons to repopulate randomly, and sort with each algorithm - printing out the list after each operation. Being able to add elements to the chain and see the new chain output each time an element is added. It should remain sorted.

So far; I managed to add one:

Link is here:https://jsfiddle.net/Lzsxhtuw/2/

Full Code is here:

    <html>

    Add Link Name:
    <input type="textbox" id="LinkName" value="newlink">
    <input type="button" id="AddLink" value="Add Link" onclick="addLink();">
    <input type="button" id="DisplayHead" onClick="displayChainHead();" value="Display Chain Head" />
    <input type="button" id="SortLink" onClick="sortChain();" value="Sort the linked-list" />
    <p id="demo"></p>
    </html>


    <script>
    var chain = new Chain('Link 1');
    var i = 0;
    function addLink() {

    var newLinkName = document.getElementById("LinkName").value;
    alert("Adding a Link Named: " + newLinkName);
    chain.addLink(newLinkName);
    chain.print();
    }
    function displayChainHead() {
    //document.getElementById("demo").innerHTML = chain.head.asString();
    chain.print();
    }

    function sortChain() {
    //document.getElementById("demo").innerHTML = chain.head.asString();
    chain.mergeSort(chain.head, chain.linkStorage[chain.head.next]);
    }
    // Define the link object
    function Link(_id, _value, _next) {
    this.id = _id; // The id of the current link
    this.value = _value; // The value stored
    this.next = _next; // a pointer to next link, this is "last" if it is the last link in chain
    }

    Link.prototype.asString = function() {
    return "Link ID: " + this.id + " Value: " + this.value
    + " Points to ID: " + this.next + "<br/>";
    };

    // Define the Chain object
    function Chain(value) {
    this.head = new Link(0, value, null); // first link is head
    this.linkStorage = []; // place to store links
    this.linkStorage.push(this.head); // push first link into array
    }

    Chain.prototype.addLink = function(_linkValue) {
    // A function to add a link to the chain
    link_id = this.linkStorage.length; //dynamically assign link id based on array length
    this.linkStorage[link_id - 1].next = link_id; //assign curr link id value to last link
    this.linkStorage.push(new Link(this.linkStorage.length, _linkValue,
    "End of Chain"));
    }

    Chain.prototype.mergeSort = function(a, b) {
    alert("Val of A: " + a.value + " Val of B: " + b.value);
    result = null;



    if(a == null)
    {
    return b;
    }
    else if(b == null)
    {
    return a;
    }

    if(a.value <= b.value)
    {
    result = a;
    this.linkStorage[result.next] = this.mergeSort(this.linkStorage[a.next], b);
    }
    else
    {
    result = b;
    this.linkStorage[result.next] = this.mergeSort(a, this.linkStorage[b.next]);
    }

    return result;

    }

    Chain.prototype.print = function() {//to see the output
    document.getElementById("demo").innerHTML = '';
    for (i = 0; i < this.linkStorage.length; i++) {
    document.getElementById("demo").innerHTML += this.linkStorage[i]
    .asString();
    }
    }
    </script>
Code in Action is here:

http://s22.postimg.org/khvxffvrl/code_in_action.jpg

I think this is the type of thing they are after:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
<p>
  <button id="qs-button">QuickSort()</button>
  <button id="ms-button">MergeSort()</button>
</p>
<ul id="items">
</ul>
<script type="text/javascript">
var items = ['e', 'a', 'd', 'b', 'c'];
var qs = document.getElementById('qs-button');
var ms = document.getElementById('ms-button');

qs.addEventListener('click', function(event) {
  var sorted = QuickSort(items);
  render(sorted);
});
ms.addEventListener('click', function(event) {
  var sorted = MergeSort(items);
  render(sorted);
});

function QuickSort(items) {
  // sort items here

  return items;
}

function MergeSort(items) {
  // sort items here

  return items;
}

function render(items) {
  var list = document.getElementById('items');
  var html = items.map(function(item) {
    return "<li>" + item + "</li>"
  }).join('');

  list.innerHTML = html;
}

render(items);
</script>
</body>
</html>

Here’s an explanation of the quicksort algorithm in JavaScript.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.