Algorithm in Javascript.


Algorithms in Javascript


Javascript is a powerful language used traditionally on the client-side but now in this new age of server-side programming it can be used on the server side thanks to technologies like "Node.JS".

Part of writing code is not only typing syntax but rather designing code that solves a problem.

What are algorithms?

Algorithms are a sequence of steps used to transform input to output. To put it simply its an approach to programming.


What kinds of problems do algorithms solve?

Algorithms have solved and continue to improve how software engineers utilize computer hardware. Where there programs are capable of achieving more and running with faster runtime. Think about that program that runs for hours. I mean you could get a bigger and more powerful computer. Surely there must be a solution. The solution is algorithms.

Here is a list of some solutions that was achieved by algorithms:
  • Sorting
  • Cryptography
  • DNA Research Automation Programming.
  • etc.

Example of Insertion sort:

var InsertOrder=function(e){
 return {
  list : [],
  length : 0,
  init:function(li){
  this.list=li;
  },
  compare : function(a,b){
 if(this.list[a]==this.list[b]) 
   return 0; 
    return (this.list[a] > this.list[b])?1:-1;
  },
  sort : function(){
 for(var i=1; i=0){
  
  if(this.compare(i-1,i)==-1){
  var key=this.list[i];
  var p=this.list[i-1];
  this.insert(i-1,key);
  this.insert(i,p);
 }
j--;
  } 
}   
  },
  insert : function(idx,data){
 this.list[idx]=data;
  },

};


};
var io= new InsertOrder();
io.init([4,2,3,1]);
console.log("BEFORE: "+ io.list);
io.sort();
console.log("AFTER: "+ io.list);

Note: Output should display the following:
BEFORE: 4,2,3,1
AFTER: 4,3,2,1


Check out my latest project a career search engine:
http://www.bzcareer.com

Comments

Popular Posts