Java Program to Sort Array Without Using Sort Method
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
How to Think About It
Algorithm
Code
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 + " "); } } }
Dry Run
Let's trace the array [5, 3, 8, 4, 2] through the bubble sort code.
Initial array
[5, 3, 8, 4, 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]
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]
Third pass
Compare 3 and 4, no swap Compare 4 and 2, swap -> [3, 2, 4, 5, 8]
Fourth pass
Compare 3 and 2, swap -> [2, 3, 4, 5, 8]
Array sorted
[2, 3, 4, 5, 8]
| Pass | Array 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
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 + " "); } } }
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 + " "); } } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bubble Sort | O(n²) | O(1) | Simple implementation, small arrays |
| Selection Sort | O(n²) | O(1) | Minimizing swaps |
| Insertion Sort | O(n²) | O(1) | Nearly sorted arrays |