Algorithms

Partition around a pivot (Lomuto scheme, last element) and sort each side — O(n log n) on average.

Version 1.1.0 Versions: 1.2.0 1.1.0 1.0.0

This is an older version. The current version is 1.2.0.

Implementations

Sign in to open in IDE Sign in with your Zerethon account to create your own copy of this sample.

Files

Main.java entry
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = {38, 27, 43, 3, 9, 82, 10, 27};
        System.out.println("Before: " + Arrays.toString(numbers));
        int[] result = numbers.clone();
        QuickSort.quickSort(result, 0, result.length - 1);
        System.out.println("After:  " + Arrays.toString(result));
        int[] expected = numbers.clone();
        Arrays.sort(expected);
        System.out.println("Same as Arrays.sort(): " + Arrays.equals(result, expected));
    }
}
QuickSort.java
// Quick Sort (Zerethon Official Sample: quick-sort 1.1.0).
// The algorithm as shown in Zerethon Tools.
public class QuickSort {
    static void quickSort(int[] a, int lo, int hi) {
        if (lo >= hi) return;
        int pivot = a[hi], i = lo;
        for (int j = lo; j < hi; j++) {
            if (a[j] < pivot) {
                int t = a[i]; a[i] = a[j]; a[j] = t;
                i++;
            }
        }
        int t = a[i]; a[i] = a[hi]; a[hi] = t;
        quickSort(a, lo, i - 1);
        quickSort(a, i + 1, hi);
    }
}
QuickSortTest.java
// Checks for quickSort. To run them, set this file as the entry file
// (the ▶ button next to it in Files) and press Run.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class QuickSortTest {
    /** Deterministic pseudo-random integers in [-50, 49] (the same as the Python checks). */
    static int[] numbers(long seed, int count) {
        int[] out = new int[count];
        for (int i = 0; i < count; i++) {
            seed = (seed * 1103515245L + 12345L) % 2147483648L;
            out[i] = (int) (seed % 100) - 50;
        }
        return out;
    }

    public static void main(String[] args) {
        List<int[]> cases = new ArrayList<>(List.of(
            new int[] {}, new int[] {1}, new int[] {2, 1}, new int[] {1, 2, 3}, new int[] {3, 2, 1},
            new int[] {5, 1, 5, 1, 5}, new int[] {-3, 0, -1, 2}));
        cases.add(numbers(1, 10));
        cases.add(numbers(2, 50));
        cases.add(numbers(3, 200));

        for (int[] c : cases) {
            int[] result = c.clone();
            QuickSort.quickSort(result, 0, result.length - 1);
            int[] expected = c.clone();
            Arrays.sort(expected);
            if (!Arrays.equals(result, expected)) {
                System.err.println("quickSort(" + Arrays.toString(Arrays.copyOf(c, Math.min(6, c.length))) + "...) = " + Arrays.toString(result));
                System.exit(1);
            }
        }
        System.out.println("All " + cases.size() + " checks passed.");
    }
}
README.md
# Quick Sort

Zerethon Official Sample `quick-sort`, version 1.1.0, Java 25.

- `QuickSort.java` — the algorithm, as shown in Zerethon Tools
- `Main.java` — entry file: a short demonstration
- `QuickSortTest.java` — checks; set it as the entry file to run them

Every `.java` file is compiled together before the entry file's class runs.

This project is your own copy. Changing it never changes the official sample.
See the algorithm animated: https://tools.zerethon.com/quick-sort-visualizer

Source: Zerethon Official Sample. Algorithm as shown in the Zerethon Tools Quick Sort Visualizer.