This program will show you how to implement Counting Sort algorithm using JavaScript programming language.
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.
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.
Program Listing
<html>
<head>
<title>Counting Sort</title>
</head>
<body>
<script>
var values=[],a=[];
var temp=0,pos=0;
document.write("<font face='arial' size='4'>COUNTING 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 counting_sort(arr, min, max) {
var i, z = 0, count = [];
for (i = min; i <= max; i++) {
count[i] = 0;
}
for (i=0; i < arr.length; i++) {
count[arr[i]]++;
}
for (i = min; i <= max; i++) {
while (count[i]-- > 0) {
arr[z++] = i;
}
}
return arr;
}
counting_sort(values,0,10);
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