Ascending Order in Java With Example

Ascending Order in Java With Example. In the java programming language, an ascending order program is used to arrange the numbers that are entered in an array. The best method for developing programming skills in any programming language is a sorting program.

Temporary Variable in Java

A temporary variable is a variable in computer programming that has a brief lifetime and is used to retain data that will be removed quickly or before it can be stored in a more permanent memory location. It is commonly stated as a local variable, or a variable with local scope, because it is short-lived. 

A temporary array persists only for the period of the data step in which it is defined. A temporary array can be used to store constant values used in calculations. There are no comparable variables to identify the array elements in a temporary array.

For Loop Definition in Java

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times. The test-condition specified inside the “for” loop determines the number of iterations. 

The Java “for” loop is one of the most simple Java loops to grasp. A “For” Loop is used to repeat a block of code for a certain number of times.

If Else Condition in Java

In Java, the if statement is used to execute a block of code if a condition is true. If a condition evaluates to false, the if…else statement is used alongside an if statement to run code. The if…else…if statement is also used to test multiple conditions. 

The simplest basic decision-making statement in Java is the if statement. It is used to determine if a statement or a block of statements will be executed or not, that is, if a condition is true, a block of statements will be executed; otherwise, it will not.

Ascending Order in Java With Example

import java.util.Scanner;
public class Main 
{
public static void main(String[] args) 
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter the size of an array");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements");
for (int i = 0; i < n; i++) 
{
a[i] = s.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("After sorting");
for (int i = 0; i < n - 1; i++) 
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}

Output:

Enter the size of an array 3

Enter the elements

34 56 78

After sorting34,56,78