Sorting an array means arranging its values in a specific order. The most common order is ascending order, where the smallest value comes first and the largest value comes last.
For example, the array [5, 2, 9, 1, 7] becomes [1, 2, 5, 7, 9] after sorting in ascending order.
In this tutorial, we will sort an array from scratch in Python and Java. We will not use built-in sorting methods such as Python's sort(), sorted(), or Java's Arrays.sort().
Sorting Algorithm Used: Bubble Sort
To understand sorting from the basics, we will use bubble sort. Bubble sort repeatedly compares two nearby values and swaps them when they are in the wrong order.
After each full pass through the array, the largest unsorted value moves to its correct position at the end. This process continues until the array becomes sorted.
Bubble sort is not the fastest sorting algorithm for large data, but it is one of the easiest sorting algorithms for beginners to learn because the comparison and swapping logic is simple.
Python Code to Sort an Array Without Built-in Methods
numbers = [5, 2, 9, 1, 7]
n = len(numbers)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if numbers[j] > numbers[j + 1]:
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
swapped = True
if swapped == False:
break
print(numbers)
In this Python program, the outer loop controls the number of passes. A pass means one complete check through the unsorted part of the array.
The inner loop compares two neighboring values. If the left value is greater than the right value, we swap them. This moves larger values toward the end of the array.
The variable swapped helps optimize the algorithm. If no swap happens during a full pass, it means the array is already sorted, so the loop stops early.
Java Code to Sort an Array Without Built-in Methods
public class Main {
public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 7};
int n = numbers.length;
for (int i = 0; i < n; i++) {
boolean swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
swapped = true;
}
}
if (swapped == false) {
break;
}
}
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i]);
if (i < numbers.length - 1) {
System.out.print(" ");
}
}
}
}
In this Java program, we use two loops to sort the array manually. The outer loop decides how many passes are needed, and the inner loop compares adjacent values.
When numbers[j] is greater than numbers[j + 1], both values are swapped using a temporary variable. This is the basic swapping technique used in many beginner algorithms.
We also use a swapped boolean variable. If the inner loop completes without any swap, the array is already sorted and the algorithm stops early.
Dry Run Example
Let us understand how bubble sort works with the array [5, 2, 9, 1, 7].
Original array: [5, 2, 9, 1, 7] Pass 1: Compare 5 and 2 -> swap [2, 5, 9, 1, 7] Compare 5 and 9 -> no swap [2, 5, 9, 1, 7] Compare 9 and 1 -> swap [2, 5, 1, 9, 7] Compare 9 and 7 -> swap [2, 5, 1, 7, 9] After pass 1, the largest value 9 is in the correct position. Pass 2: Compare 2 and 5 -> no swap [2, 5, 1, 7, 9] Compare 5 and 1 -> swap [2, 1, 5, 7, 9] Compare 5 and 7 -> no swap [2, 1, 5, 7, 9] Pass 3: Compare 2 and 1 -> swap [1, 2, 5, 7, 9] Compare 2 and 5 -> no swap [1, 2, 5, 7, 9] Final sorted array: [1, 2, 5, 7, 9]
Output
[1, 2, 5, 7, 9]
The Java program prints the values separated by spaces:
1 2 5 7 9
Time and Space Complexity
The average and worst-case time complexity of bubble sort is O(n²). This happens because each element may need to be compared with many other elements.
The best-case time complexity is O(n) when the array is already sorted and the swapped optimization stops the loop early.
The space complexity is O(1) because the sorting is done inside the same array and only a few extra variables are used.
Why Learn Sorting From Scratch?
Built-in sorting methods are useful in real projects, but learning how sorting works internally helps you understand comparisons, loops, swapping, and algorithm efficiency.
Bubble sort is a good first sorting algorithm because it clearly shows how values move step by step until the array becomes sorted.
Sorting an array without built-in methods is an important beginner exercise. It builds a strong foundation for understanding algorithms, time complexity, nested loops, and array manipulation.