Java Program to Sort the Elements of an Array in Ascending Order Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() method that comes under java.util.Arrays class. This method uses a highly optimized algorithm i.e. the Dual-Pivot Quicksort for primitive types and efficiently sorts the array elements in ascending order. Java // Java Program to sort the elements of an array // in Ascending Order using Arrays.sort() method import java.util.Arrays; public class GFG { public static void main(String[] args) { int[] arr = new int[] { -2, 0, 1, 3, -1, 2 }; // use Arrays.sort() method to sort array // elements in ascending order Arrays.sort(arr); System.out.println(Arrays.toString(arr)); } } Output[-2, -1, 0, 1, 2, 3] 2. Using Bubble Sort This example implements a custom bubble sort algorithm to sort the array.Approach:Compare adjacent elements with each other.Use nested for loop to keep track.Swap the elements if the first element is greater than the second element.Repeat until the array is sorted.Example: Java // Java Program to Sort Elements of an Array in // Ascending Order using manual bubble sort import java.util.Arrays; class GFG { // length of an array static int l; // method to sort the array in ascending order public static void sort(int[] a) { // temporary variable for swapping elements int t = 0; // outer loop to iterate through the array for (int i = 0; i < l; i++) { // inner loop to compare and swap elements for (int j = i + 1; j < l; j++) { if (a[i] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } } public static void main(String[] args) { // initialize array to be sorted int[] a = new int[] { -2, 0, 1, 3, -1, 2 }; // set the length of the array l = a.length; sort(a); for (int i = 0; i < l; i++) { System.out.print(a[i] + " "); } } } Output-2 -1 0 1 2 3 Explanation: Here, the bubble sort algorithm sorts the array by swapping elements until each element is in the correct position. It has a higher time complexity of O(n2) as compared to Arrays.sort(). Comment More infoAdvertise with us D deepak710agarwal Follow Improve Article Tags : Java Java Programs Java-Array-Programs Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like