Monday, November 16, 2015

Shell Sort in JavaScript

A simple program that I wrote using JavaScript to show the concept of Shell 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>Shell Sort</title>
  </head>
<body> 
   <script>

   var values=[],a=[];
   var temp=0,pos=0;
   
   document.write("<font face='arial' size='4'>SHELL 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<10; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  
  function shellSort (a) {
    for (var h = a.length; h = parseInt(h / 2);) {
        for (var i = h; i < a.length; i++) {
            var k = a[i];
            for (var j = i; j >= h && k < a[j - h]; j -= h)
                a[j] = a[j - h];
            a[j] = k;
        }
    }
    return a;
}
 shellSort(values);

      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>  

   

Merge Sort in JavaScript

A simple program that I wrote using JavaScript as my programming language that demonstrate Merge 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> Merge Sort </title>
  </head>
<body> 
   <script>

   var values=[];
   var a=0,b=0;
   
   document.write("<font face='arial' size='4'>MERGE 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<10; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  
  function merge(left, right, arr) {
  var a = 0;

  while (left.length && right.length) {
    arr[a++] = (right[0] < left[0]) ? right.shift() : left.shift();
  }
  while (left.length) {
    arr[a++] = left.shift();
  }
  while (right.length) {
    arr[a++] = right.shift();
  }
}

function mSort(arr, tmp, len) {
  if (len === 1) { 
  return; 
}

  var m = Math.floor(len / 2),
      tmp_l = tmp.slice(0, m),
      tmp_r = tmp.slice(m);

  mSort(tmp_l, arr.slice(0, m), m);
  mSort(tmp_r, arr.slice(m), len - m);
  merge(tmp_l, tmp_r, arr);
}

function merge_sort(arr) {
  mSort(arr, arr.slice(), arr.length);
}

 merge_sort(values); 
 
      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>  

   

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>  
   

Heap Sort in JavaScript

A sample program that I wrote using JavaScript to show the concept of Heap 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> Heap Sort </title>
  </head>
<body> 
   <script>

   var values=[];

   
   document.write("<font face='arial' size='4'>HEAP 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<10; a++) {
    document.write("<font face='arial' size='4'> " +values[a] + "");
   }
  

function swap(data, i, j) {
    var tmp = data[i];
    data[i] = data[j];
    data[j] = tmp;
}

 function heap_sort(arr) {
    put_array_in_heap_order(arr);
    end = arr.length - 1;
    while(end > 0) {
        swap(arr, 0, end);
        sift_element_down_heap(arr, 0, end);
        end -= 1
    }
}

function put_array_in_heap_order(arr) {
    var i;
    i = arr.length / 2 - 1;
    i = Math.floor(i);
    while (i >= 0) {
        sift_element_down_heap(arr, i, arr.length);
        i -= 1;
    }
}

function sift_element_down_heap(heap, i, max) {
    var i_big, c1, c2;
    while(i < max) {
        i_big = i;
        c1 = 2*i + 1;
        c2 = c1 + 1;
        if (c1 < max && heap[c1] > heap[i_big])
            i_big = c1;
        if (c2 < max && heap[c2] > heap[i_big])
            i_big = c2;
        if (i_big == i) return;
        swap(heap,i, i_big);
        i = i_big;
    }
}

heap_sort(values);

      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>  

   

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>  

   

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>  

   

Linear Search in JavaScript

A simple program that I  wrote in JavaScript the shows the concept of linear search or sequential search of a series of numbers given by the user.

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> Linear Search </title>
  </head>
<body> 
   <script>

   var values=[];

  for (a=0; a<5; a++) {
   input = values.push(Number(prompt("Enter item value at no. " + (a+1))));
 }

  search_value = Number(prompt("Enter value to find"));

  document.write("<font face='arial' size='4'>List of values");
  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] + "");
   }
  
  document.write("<br>");
  
  for (a=0; a<5; a++) {
    if (values[a] == search_value)
{
 document.write("<br>");
 document.write("<font face='arial' size='4'>The value number "
 +search_value +  " is present at location " + (a+1) + ". </font>");
  break;
}
  }  
  if (values[a] != search_value) {
   document.write("<br>")
   document.write("<font face='arial' size='4'> The value number "  
   + search_value + " is not present from the list. </font>");
   }
  </script>
  </body>
 </html>