Sort an Array in Ascending Order

Java Program to Sort an Array in Ascending Order with Example

In this Java tutorial, we’ll sort an array in ascending order with a temporary variable and a nested for loop. To get user input, we’re using the Scanner class.

Java Example: Program to Sort an Array in Ascending Order

The user is prompted in this program to enter the number of elements that he wishes to enter. Based on the input, we declared an int array and then accept all of the numbers entered by the user and store them in the array.

  • After we’ve stored all of the numbers in the array, we’ll sort them using a nested for loop.
public class JavaExample 
{
    public static void main(String[] args) 
    {
    	int count, temp;
    	
    	//User inputs the array size
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter number of elements you want in the array: ");
        count = scan.nextInt();
    
        int num[] = new int[count];
        System.out.println("Enter array elements:");
        for (int i = 0; i < count; i++) 
        {
            num[i] = scan.nextInt();
        }
        scan.close();
        for (int i = 0; i < count; i++) 
        {
            for (int j = i + 1; j < count; j++) { 
                if (num[i] > num[j]) 
                {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        System.out.print("Array Elements in Ascending Order: ");
        for (int i = 0; i < count - 1; i++) 
        {
            System.out.print(num[i] + ", ");
        }
        System.out.print(num[count - 1]);
    }
}

Output:

Enter number of elements you want in the array: Enter array elements: 100 20 89 -45 78 Array Elements in Ascending Order: -45 20 78 89 100

array is reversed array is reversed array is reversed array is reversed array is reversed array is reversed array is reversed

theudaipurstore.com

Java Basic Syntax | Java Tutorial

In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing. sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum

Leave a Reply