Monday, November 16, 2015

Bubble Sort in JavaScript

A simple program that I wrote in JavaScript to demonstrate the Bubble 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> Bubble Sort </title>
  </head>
<body> 
   <script>

   var values=[];
   var a=0,b=0;
   
   document.write("<font face='arial' size='4'>BUBBLE SORT </font>");
   document.write("<br><br>");
 for (a=0; a<5; 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<5; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  
     for (a = 0; a < ( 5 - 1 ); a++) {
      for (b = 0; b < 5 - a - 1; b++) {
        if (values[b] > values[b+1]) 
        {
          swap       = values[b];
          values[b]   = values[b+1];
          values[b+1] = swap;
        }
      }
    }
  
      document.write("<br><br>");
 document.write("<font face='arial' size='4'>Sorted List of Numbers </font>");
 document.write("<br><br>");
  for (a=0; a<5; a++) {
    document.write("<font face='arial' size='4'> " +  values[a]+"</font>");
  }  
  
  </script>
  </body>
 </html>  

   

No comments:

Post a Comment