Monday, November 16, 2015

Insertion Sort in JavaScript

A simple program that I wrote using JavaScript as my programming language that demonstrate the concept of Insertion Sort algorithm.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.



Sample Program Output


Program Listing

<html>
 <head>
   <title> Insertion Sort </title>
  </head>
<body> 
   <script>

   var values=[];
   var temp=0,pos=0;
   
   document.write("<font face='arial' size='4'>INSERTION SORT </font>");
   document.write("<br><br>");
 for (a=0; a<10; a++) {
  values.push(Number(prompt("Enter item value at no. " + (a+1))));
   }
      document.write("<font face='arial' size='4'>Numbers");
     document.write(" given by the user </font>");
     document.write("<br><br>");
  for (a=0;a<8; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  
   for (i = 1; i < values.length; i++)
{

temp = values[i];
pos = i - 1;

while ((pos >= 0) && (temp < values[pos]))
{
values[pos + 1] = values[pos];
pos--;
        }
values[pos + 1] = temp;
   
    }
   
      document.write("<br><br>");
 document.write("<font face='arial' size='4'>Sorted List of Numbers </font>");
 document.write("<br><br>");
  for (a=0; a<10; a++) {
    document.write("<font face='arial' size='4'> " +  values[a]+"</font>");

  }  
  </script>
  </body>
 </html>  
   

No comments:

Post a Comment