81. JAVA Program to Sort the elements in Ascending Order.
How does this program work?
- This program is used to sort the elements in ascending order using java.
- In this program, an array of elements can be sorted without using any sorting function in java.
- Each element is compared with the next element, If the element is greater than the other in comparison then the elements are swapped.
- After doing this for each element of the given array, then we get the elements in asceding order.
Here is the code
// To arrange the elements in Ascending order
import java.util.Scanner;public class temp
{
public static void main(String[] args )
{
int n, temp;
System.out.print("Enter Elements:\n");
Scanner skill = new Scanner( System.in);
n = skill.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = skill.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order is :");
for(int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + "," );
}
System.out.print(a[n - 1] );
}
}