SORTING IN AN ARRAY
JAVA PROGRAM TO SORT ARRAY IN ASCENDING ORDER
import java.util.Scanner; //Import the Scanner class
class Sorting
{
public static void main(String args[])
{
int a[] = new int[60];
int i,j,temp,size;
System.out.print("Enter array size: ");
Scanner in = new Scanner(System.in);
size=in.nextInt();
//Insert value in size variable at run time
//size=in.nextInt();
System.out.println("Enter values in array:");
//Scanner in = new Scanner(System.in);
for(i=0; i<size; i++)
{
a[i]=in.nextInt();
}
//Loop to sort the array in ascending array.
for(i=0; i<size-1; i++)
{
for(j=0; j<size-1; j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("\nArray values after sort...");
for(i=0; i<size; i++)
{
System.out.println(a[i]);
}
}
}
- - - PROGRAM OUTPUT - - -
0 Comments