Filled Under: Array
How To Sort Numbers In Ascending Order In Java
There are many situation in which we need to sort numbers or word this tutorial provides a simple way to do this for string see this how to sort string in java .There are two type of number sorting ascending sorting and descending sorting in this tutorial we show how to sort number in ascending order for descending order sorting see descending sorting in java.
CODING:
class intsort
{
public static void main(String args[])
{
int arr[] = {100,30,10,20,60,70};
System.out.println("Before sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j]<arr[i])
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}
OUTPUT:
0 comments:
Post a Comment