You are here: Home / Topics / Program to arrange array in Ascending order in Java

Program to arrange array in Ascending order in Java

Filed under: Java on 2023-08-19 18:39:56

// Arrange Array in Ascending order

public class ArrayAscending {

   public static void main(String[] args) {
       int i, temp, j;
       int a[] = {1, 7, 5, 9, 2, 12, 53, 25};

System.out.println("Before Ascending order:");
for (i = 0; i < 8; i++) {
           System.out.print(" "+a[i]);
       }

       for (i = 0; i < 8; i++) {
           for (j = i + 1; j < 8; j++) {
               if (a[i] > a[j]) {
                   temp = a[i];
                   a[i] = a[j];
                   a[j] = temp;
               }
           }
       }

System.out.println("After Ascending order:");
       for (i = 0; i < 8; i++) {
           System.out.println(a[i]);
       }
   }
}


Output:

Before Ascending order: 
1  7  5  9  2  12  53  25

After Ascending order:
1  2  5  7  9  12  25  53


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.