Monday, November 16, 2015

Selection Sort in JavaScript

A program that I wrote in JavaScript that shows you how to implement selection 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> Selection Sort </title>
  </head>
<body> 
   <script>

   var values=[];
   var a=0,b=0;
   
   document.write("<font face='arial' size='4'>SELECTION SORT </font>");
   document.write("<br><br>");
 for (a=0; a<8; 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( a = values.length - 1; a >= 0; a--)
{
highestIndex = a;
for( j = a; j >= 0; j--)
{
if(values[j] > values[highestIndex])
highestIndex = j;
}
temp = values[a];
values[a] = values[highestIndex];
values[highestIndex] = 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<8; a++) {
    document.write("<font face='arial' size='4'> " +  values[a]+"</font>");
  }  
  
  </script>
  </body>
 </html>  

   

No comments:

Post a Comment