Sorting Lists

List sorted forked

by Juan Alban Franco

HTML

<html>

<head>
Juan Alban Franco
</head>
insert number to add
<input type = textbox id = "in">
<input type = button id = "add" value  = "add to list" onclick = "add()">
<input type = button id = pr value = "Print LIst" onclick = "pr()">
<br>
<input type = button id = "test" value = "Generate List" onclick = "test()">
<br>
<input type = button id = "clear" value = "Clear List" onclick = "clr()">
<input type = button id = "bbl" value = "Bubble Sort" onclick = "bblsort()">
<input type = button id = "merge" value = "Merge Sort" onclick = "mergesort()">
<p id = array>
</p>
<p id = "out">
</p>
</html>

JavaScript

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

function Node() {
	this.content = 0;
  this.next = null;
  this.prev = null;
}


linkedlist.prototype.insert = function (_content) { 
	node = new Node();node.content = _content;
	if(this.length < 1)
  {
  	this.head = node;
    this.tail = node;
    this.length ++;
  }
  else{
  	this.tail.next = node;
    node.prev = this.tail;
    this.tail = node;
    this.length ++;
  }
}
linkedlist.prototype.print = function() {

var traveler = new Node();
traveler = this.head;
var string = "";
while (traveler)
	{
		string += traveler.content + " <br>";
    traveler = traveler.next;
	}
  document.getElementById("out").innerHTML = string + this.length
}

linkedlist.prototype.clear = function () {
	if(this.head != null){
  this.head = null;
  this.tail = null;
  this.length = null;
  document.getElementById("out").innerHTML = "";
  }
  else {return}
}
 
 
linkedlist.prototype.bubblesort = function(){
	var traveler = this.head;
  var length = this.length;
  var holder_value = 0;
  var i = 0;
  var j = 0;
  var counter = 0;
  var last_step = null;
  if (traveler == null){return;}
  
  do{
  	counter = 0;
    traveler = this.head;
  	while(traveler.next != last_step){
     	if(traveler.content > traveler.next.content){
	      	counter ++;
	      	holder_value = traveler.content;
		     	traveler.content = traveler.next.content;
		     	traveler.next.content = holder_value;
	       }
	      traveler = traveler.next;
	    }
	    last_step = traveler;
		}
    while (counter) 
  }
  
  
  
linkedlist.prototype.mergesort = function ()
{
	 var traveler = this.head;
  if(traveler == null || traveler.next == null){
  return traveler;
  }
	var middle = this.middle(); // succ

	

}

linkedlist.prototype.merge = function (first, second){
	if (first == null) { return second;}
  if (second == null) { return first;}
  
  if(first.content > second.content) { 
  	first.next = this.merge (first, second.next);
 ...