Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

<!

DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var i,j,temp;
var x = 0;
var array = Array();
function add()
{
array[x] = parseInt(document.getElementById("text1").value);
x++;
document.getElementById("text1").value = "";
display();
}
function display()
{
var e = "<hr/>";
for (var y=0; y<array.length; y++)
{
e += "You entered " +" = " + array[y] + "<br/>";
}
document.getElementById("Result").innerHTML = e;
}
function sort()
{
var i,j,temp,loc,small;
temp=loc=small=0;
var n=array.length;
for (i = 0 ; i < n-1; i++)
{
small=array[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(array[j]>small)
{
small=array[j];
loc=j;
}
}
temp= array[i];
array[i]= array[loc];
array[loc] = temp;
//alert(array);
}
document.getElementById("sort").innerHTML = array;
}
</script>
</head>
<body>
<h1 align="center">EXCHANGE SORT</h1>
<input type="text" id="text1"></input>
<input type="button" id="button1" value="Add" onclick="add();"></input>
<input type="button" id="button2" value="Sort" onclick="sort();"></input>
<p>Array Elements</p>
<div id="Result"></div>
<hr>
<p>Sorted Array Elements in Descending Order</p>
<p id="sort"></p>
</body>
</html>

You might also like