You have to initialize the value for j as i+1, this sorting algorithm is called bubble sort, that works by repeatedly swapping the adjacent elements if they are in wrong order. The below method is always runs O(n^2) time even if the array is sorted.

public static void main (String[] args)
{
    int[] array = {4,2,1,3,5,9,6,8,7};

    for(int i = 0 ; i < array.length;i++)
    {
        for(int j = i+1 ; j< array.length;j++)
        {
            if(array[i] > array[j])
            {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
Answer from apoori on Stack Overflow
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to sort an array in java โ€“ tutorial with examples
How To Sort An Array In Java - Tutorial With Examples
April 1, 2025 - You can sort the array using manual sorting like using for loops. What you can do is use two for loops, one to traverse the array from the starting and another for loop inside the outer one to traverse the next element.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arrays_sort.asp
Java Arrays sort() Method
For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ sorting-in-java
Sorting in Java - GeeksforGeeks
Java ยท class GFG { // Main driver method public static void main(String[] args) { // Custom input array int arr[] = { 4, 3, 2, 1 }; // Outer loop for (int i = 0; i < arr.length; i++) { // Inner nested loop pointing 1 index ahead for (int j = i + 1; j < arr.length; j++) { // Checking elements int temp = 0; if (arr[j] < arr[i]) { // Swapping temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // Printing sorted array elements System.out.print(arr[i] + " "); } } } Output ยท
Published: April 16, 2026
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ sorting array in java
Sorting Array in Java - Scientech Easy
February 12, 2025 - In this way, we can also sort elements of an array in natural or ascending order by using for loop. Letโ€™s take a simple example program where we will sort elements of an array of integer types using Comparator in Java. Here, we will use the Scanner class to accept array elements from the keyboard and sort them in ascending order.
๐ŸŒ
Reddit
reddit.com โ€บ r/askcomputerscience โ€บ java arrays.sort(arr) or use loops instead?
r/AskComputerScience on Reddit: Java Arrays.sort(arr) or use loops instead?
October 9, 2020 -

So I'm still kind of a novice in java, and I would appreciate it if someone could explain what is a more preferred method in sorting the elements of an array in ascending order.

Shoul I use Loops? example:

  1. //Sort the array in ascending order  

  2. for (int i = 0; i < arr.length; i++) {     

  3. for (int j = i+1; j < arr.length; j++) {     

  4. if(arr[i] > arr[j]) {    

  5. temp = arr[i];    

  6. arr[i] = arr[j];    

  7. arr[j] = temp;   

Or is it more recomended to use Arrays.sort instead?

Or is it dependent on the usage?

Also what are the pros and cons of each.

Thank you to those who can enlighten me.

Edit: Thank you so much to everyone who shared their insights!

๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2018 โ€บ 10 โ€บ java-program-to-sort-an-array-in-ascending-order
Java Program to Sort an Array in Ascending Order
Once we have all the numbers stored in the array, we are sorting them using nested for loop. import java.util.Scanner; 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 ...
Find elsewhere
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ it & development โ€บ software development โ€บ bubble sort: java array sorting with a for loop
Bubble Sort: Java Array Sorting with a For Loop - Udemy Blog
April 14, 2026 - Implementing bubble sort in Java means using nested for loops to repeatedly compare and swap adjacent array values until they're sorted in ascending or descending order. This article walks through the full Java code, explaining the role of nested ...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java algorithms โ€บ how to sort an array in java
How to Sort an Array in Java
January 7, 2025 - The inner loop compares adjacent elements. If the order is wrong, the method swaps them. This procedure is repeated until the entire array has been sorted. Here is an output of our program: Array before sorting: 18 28 2 7 90 45 Array after sorting: 2 7 18 28 45 90 ยท Selection algorithm sorts an array by repeatedly finding the smallest element from the unsorted part and placing it at the beginning. Letโ€™s write it in Java:
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ java-program-sort-array-ascending-order
Java Program to Sort an Array in Ascending Order - Sanfoundry
May 15, 2023 - 7. If the first element is greater than the second element, the two elements are swapped using a temporary variable. 8. The sorted array is printed in ascending order using a for loop. ... $ javac Ascending _Order.java $ java Ascending _Order ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-sort-the-elements-of-an-array-in-ascending-order
Java Program to Sort the Elements of an Array in Ascending Order - GeeksforGeeks
August 18, 2026 - import java.util.Arrays; class ... 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 ...
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ topics โ€บ how-to-sort-an-array-in-java
How to Sort an Array in Java โ€“ Tutorial With Examples
July 23, 2024 - The above example shows the workings of nested loops to arrange a given array in ascending order. The other way to sort an array is to create a user-defined method. This method contains the sorting logic and can be reused multiple times in a program. ... // Java program to sort integer array in ascending order using user-defined method public class SortArrayExample { public static void main(String[] args){ // Define an integer type array int[] arr = new int[] {99, 39, 10, -9, 20, 2990, 0, -1, 13}; // Printing the original array System.out.println("The Original Array is: "); for (int num : arr)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arrays-sort-in-java
Arrays.sort() in Java - GeeksforGeeks
Arrays.sort() is a built-in method in Java used to sort arrays in ascending order. Arrays.sort() is a static method of the Arrays class .It supports arrays of primitive types and objects. Sorts object arrays using their natural ordering or a provided Comparator. It uses Dual-Pivot QuickSort for primitives and TimSort for objects (optimized for performance).
Published: August 27, 2026
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to lexicographically sort an array in java
How to Lexicographically Sort an Array in Java
September 3, 2026 - Because strings are objects, the method we will use is Arrays.sort(Object[]). This sorting uses the TimSort algorithm, which has a worst-case time complexity of O(n*log(n)). By default, sort() will sort the array lexicographically. Letโ€™s take a look at an example: import java.util.Arrays; public class Sort { public static void main(String[] args) { String[] array = {"shrew", "porcupine", "newt", "chameleon"}; Arrays.sort(array); for (String s : array) { System.out.print(s + " "); } } }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ arrays-sort-in-java-with-examples
Arrays.sort() in Java with Examples | GeeksforGeeks
Arrays sort() method is used for sorting the elements in an Array. It consists of two variations, one in which it sorts the entire array, it may be an integer or character array. Another one allows sorting a specific part of the array by passing ...
Published: November 11, 2024
๐ŸŒ
HackerNoon
hackernoon.com โ€บ how-to-sort-an-array-in-java-a-complete-tutorial
How to Sort an Array in Java - A Complete Tutorial | HackerNoon
August 27, 2024 - In Java, we can sort the array in the following ways: โ— Using the sort() Method โ— Without using the method ยท โ— Using the for Loop โ— Using the User-Defined Method
๐ŸŒ
Quora
quora.com โ€บ Can-we-sort-an-array-using-single-loop-in-Java
Can we sort an array using single loop in Java? - Quora
Answer (1 of 10): Yes, you can do one more thing add it to the ArrayList and sort it via sort() of the arraylist . There you go, this is how you can do sorting without even using a single loop. For further on how to use try importing java.util.ArrayList there are several other classes which can h...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 716204 โ€บ java โ€บ Sorting-array-integers
Sorting an array of integers (Beginning Java forum at Coderanch)
September 11, 2019 - For sort(), the algorithm efficiency depends on the initial state of the array, e.g.: a) an array that's already sorted, b) an array that's already sorted and then moving the least value to the end, c) an array presorted in reverse order, d) random numbers, e) small range of random numbers with lots of duplicates. JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility
๐ŸŒ
Quora
quora.com โ€บ What-is-a-more-preferred-method-in-sorting-the-elements-of-an-array-in-ascending-order-Java-Arrays-sort-arr-or-use-loops-instead
What is a more preferred method in sorting the elements of an array in ascending order, Java Arrays.sort(arr) or use loops instead? - Quora
Answer (1 of 3): Sorting is a common operation. Sort algorithms There are various algorithms to solve the problem of putting elements of a list into a certain order. Which algorithm to use, how to use them, depends on the data set to be sorted and the specific computation resources. With the d...