Solution:
class Sort
{
public static void main(String abc[])
{
int temp;
int arr[]={3,7,8,5,2,1,9,6,4};
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
//Use arr[i]<arr[j] if you want to sort an array in descending order.
//Note: The below condition will sort an array in ascending order.
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
class Sort
{
public static void main(String abc[])
{
int temp;
int arr[]={3,7,8,5,2,1,9,6,4};
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
//Use arr[i]<arr[j] if you want to sort an array in descending order.
//Note: The below condition will sort an array in ascending order.
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
No comments:
Post a Comment