0
0
JavaProgramBeginner · 2 min read

Java Program to Sort Array Without Using Sort Method

You can sort an array in Java without using the sort method by implementing a simple sorting algorithm like bubble sort, for example: use nested loops to compare and swap adjacent elements until the array is sorted.
📋

Examples

Input[3, 1, 2]
Output[1, 2, 3]
Input[5, 4, 3, 2, 1]
Output[1, 2, 3, 4, 5]
Input[]
Output[]
🧠

How to Think About It

To sort an array without built-in methods, think about comparing each pair of elements and swapping them if they are in the wrong order. Repeat this process multiple times until no swaps are needed, meaning the array is sorted.
📐

Algorithm

1
Get the input array.
2
Start from the first element and compare it with the next element.
3
If the current element is greater than the next, swap them.
4
Repeat this for all elements in the array.
5
Repeat the entire process until no swaps happen in a full pass.
6
Return the sorted array.
💻

Code

java
public class SortArrayWithoutSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 4, 2};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Output
2 3 4 5 8
🔍

Dry Run

Let's trace the array [5, 3, 8, 4, 2] through the bubble sort code.

1

Initial array

[5, 3, 8, 4, 2]

2

First pass comparisons and swaps

Compare 5 and 3, swap -> [3, 5, 8, 4, 2] Compare 5 and 8, no swap Compare 8 and 4, swap -> [3, 5, 4, 8, 2] Compare 8 and 2, swap -> [3, 5, 4, 2, 8]

3

Second pass

Compare 3 and 5, no swap Compare 5 and 4, swap -> [3, 4, 5, 2, 8] Compare 5 and 2, swap -> [3, 4, 2, 5, 8]

4

Third pass

Compare 3 and 4, no swap Compare 4 and 2, swap -> [3, 2, 4, 5, 8]

5

Fourth pass

Compare 3 and 2, swap -> [2, 3, 4, 5, 8]

6

Array sorted

[2, 3, 4, 5, 8]

PassArray State
1[3, 5, 4, 2, 8]
2[3, 4, 2, 5, 8]
3[3, 2, 4, 5, 8]
4[2, 3, 4, 5, 8]
💡

Why This Works

Step 1: Compare adjacent elements

The code compares each pair of neighbors to check if they are in the wrong order using if (arr[j] > arr[j + 1]).

Step 2: Swap if needed

If the left element is bigger, it swaps them to move the bigger element rightwards using a temporary variable.

Step 3: Repeat passes

Multiple passes ensure all elements bubble up to their correct positions until the array is fully sorted.

🔄

Alternative Approaches

Selection Sort
java
public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 4, 2};
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Selection sort finds the smallest element each pass and swaps it to the front; it does fewer swaps but still has O(n²) time.
Insertion Sort
java
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 4, 2};
        for (int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Insertion sort builds the sorted array one element at a time by inserting elements into their correct position.

Complexity: O(n²) time, O(1) space

Time Complexity

The nested loops each run up to n times, so the total comparisons and swaps are proportional to n squared.

Space Complexity

The sorting is done in-place, so no extra array is needed, only a temporary variable for swapping.

Which Approach is Fastest?

Bubble, selection, and insertion sorts all have O(n²) time, but insertion sort is often faster on nearly sorted data.

ApproachTimeSpaceBest For
Bubble SortO(n²)O(1)Simple implementation, small arrays
Selection SortO(n²)O(1)Minimizing swaps
Insertion SortO(n²)O(1)Nearly sorted arrays
💡
Use nested loops to compare and swap elements step-by-step when you can't use built-in sort.
⚠️
Forgetting to reduce the inner loop range each pass causes unnecessary comparisons and slows down the sort.