First understand this:
Let us assume we taken below array
var a=["9","8","3","2","8","11","7"]
First understand this:
step1:-->if (r[i]<=r[j])
t=t+1;
Above array 11 is biggest number,Based on above condition t will return
zenrate:t=1 for Highest no 11
t=2 for second biggest no 9
t=3 for third biggest no 8
t=4 for fourth biggest no 7
t=5 for fifth biggest no 3
t=6 for least no---------2
Now i am inserting highest number in last of array index that b[5]=r[i]
not understand see this b[5]=11,b[4]=9,b[3]=8,b[2]=7,b[1]=3,b[0]=2
Don't forget this is the key for writing sorting order
Order in this way:
Display in Ascending Order:a=["9","8","3","2","8","11","7"]
for(i=0;i<=4;i++)
{
document.write(b[i]);--->out put in this order 2,3,7,8,9,11
}
Also,If u want display in Descending Order this code will help you how
for(i=4;i>=0;i--)
{
document.write(b[i]);--->Out put is 11,9,8,7,3,2
}
----------------------------------------------------------------------------------
Next you can change below code in your environment(c,c++,c#,java)
<html>
<script type="text/javascript">
var r=new Array();
r[0]=1;
r[1]=237654;
r[2]=99999;
r[3]=99;
r[4]=10000;
var b=new Array();
var t=0;
document.write("Given format"+r);
document.write("<br />")
for (i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
{
if (r[i]<=r[j])
{
t=t+1;
}
}
if (t==1)
{
b[4]=r[i];
document.write(" Highest no"+b[4]+"<br /> ");
}
else if(t==2)
{
b[3]=r[i];
document.write("second highest no"+b[3]+"<br /> ");
}
else if(t==3)
{
b[2]=r[i];
document.write("Third highest no"+b[2]+"<br /> ");
}
else if(t==4)
{
b[1]=r[i];
document.write("Fourth highest no"+b[1]+"<br /> ");
}
else if(t==5)
{
b[0]=r[i];
document.write("Least no"+b[0]+"<br /> ");
}
t=0;
}
document.write("Output in Ascending order:")
for(i=0;i<=4;i++)
{
document.write(b[i]+" ");
}
</script>
</html>
No comments:
Post a Comment