program for Sorting and Reversing of an Array - C C++ Java Programs - Examples

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Friday, October 7, 2011

program for Sorting and Reversing of an Array


 Sorting and Reversing of an Array


var a=new Array(5);
a=[4,7,2,18,10];
ascend(a);
descend(a);
var i,j,temp=0;
function ascend(a:Number):Void
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
trace("Ascending order of given numbers is:");
for(i=0;i<5;i++)
{
trace(a[i]);
}
}
function descend(a:Number):Void
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
trace("Descending order of given numbers is:");
for(i=0;i<5;i++)
{
trace(a[i]);
}
}




-----------------------------------------------------------------------------
output
----------------------------------------------------------------------------
Ascending order of given numbers is:
2
4
7
10
18
Descending order of given numbers is:
18
10
7
4
2

No comments:

Post a Comment