MOD 7 - SORTING - USING back storage array

by SHELDON PASCIAK

HTML

<input type="text" id="newItem" placeholder="New Value" />
<input type="button" id="addItem" value="Add" />
<input type="button" id="bubbleSort" value="bubble sort" />
<input type="button" id="sortQuick" value="quick sort" />
<br/>

<p id="demo"></p>



<!--

Assignment
 

We are going to return to your Chain and Links code from Module 3 - Lists . You will need to fork your JSFiddle into a new Fiddle. In this Fiddle we are going to add a Chain function -  Sort(). I would like you to 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.

 

Once the Chain is sorted - we want to keep it sorted. You have a couple of ways to do that; (1) insert elements into the correct location in the Chain based on sort order or (2) re-sort the chain after every insertion. If you choose (2) you will need to pick an algorithm that is highly efficient for a pre-sorted array with one item out of place (look at all the algorithms - it is there).

 

You will now need an interface to add Links to your chain. Just create either a text box and a button or a button and any type of input. I should be able to add elements to your chain and see the new chain output each time I add an element.

-->

JavaScript

/* 	Module 7 - VERSION 2 - Assignment 1 - SHELDON PASCIAK */

// VERSION 2 WOO HOO - A LOT OF WORK!

// 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 the next link, this is 0 if it is the last link in the chain
    this.prev = 0;
}

Link.prototype.asString = function () {
    return "Link: " + this.id +
        " Value: " + this.value +
        " next: " + this.next; 
        //+ " prev: " + this.prev.id ; // used for debugging
};

// Define the Chain object
function Chain(_firstValue) { // We will define the chain with the first link defined
    this.length = 1; // I like to keep track of the first link, not really necessary
    this.head = new Link(1, _firstValue, 0);    
    this.linkStorage = []; // Array for back storage
    this.linkStorage.push(this.head); // A chain has at least a head
}

/*
	Iterates all elements of the back storage array (linkStorage) looking for node with the ID to search for.  Returns with null object when ID not found, or the node object when the ID  is found.
*/
Chain.prototype.findID = function (id) {    
    //consider using head-->toe walk to search rather than iterate through linkStorage[0-(n-1)]
    for (var i=0;i<(this.linkStorage).length;i++) {
        if (this.linkStorage[i].id == id) return this.linkStorage[i];
    }
    return null; 
}

Chain.prototype.sortBubble = function () {
 
    for (var i=0;i<this.linkStorage.length;i++)
    {
        console.log(i + " --- " + this.linkStorage[i].value);
        
        for (var j=i+1;j<this.linkStorage.length-1;j++) 
        {
            if ( this.linkStorage[i].value > this.linkStorage[j].value ) {
				temp = this.linkStorage[j];
                this.linkStorage[j] = this.linkStorage[i];
                this.linkStorage[i] = temp;
            }
        }
    }  
    
    
    var oo = "";
    for (var...