How to Use QuickSort from TheAlgorithms/Java: Implementation Guide

To use QuickSort from TheAlgorithms/Java, import the QuickSort class from the com.thealgorithms.sorts package, instantiate it as a SortAlgorithm, and invoke the sort() method on any array or list of Comparable elements.

The TheAlgorithms/Java repository provides a comprehensive collection of algorithm implementations organized under the com.thealgorithms namespace. Each sorting algorithm, including QuickSort, implements the common SortAlgorithm interface, enabling polymorphic usage across your Java projects. This guide explains how to integrate the repository's generic, in-place QuickSort implementation into your code.

Understanding the QuickSort Architecture

The QuickSort implementation follows the classic divide-and-conquer strategy while incorporating optimizations to handle edge cases.

Core Implementation Files

Located in src/main/java/com/thealgorithms/sorts/QuickSort.java, the QuickSort class implements the SortAlgorithm interface defined in SortAlgorithm.java. This design allows QuickSort to be interchangeable with other sorting implementations such as MergeSort or BubbleSort.

The algorithm operates in-place and works with any type that implements Comparable<T>. Key methods include:

  • randomPartition – Selects a random pivot and swaps it to the end to avoid worst-case O(n²) performance on already-sorted data
  • partition – Splits the array around the pivot using two pointers
  • doSort – Recursively sorts sub-arrays left and right of the pivot

Utility methods such as swap, less, and isSorted are inherited from SortUtils (src/main/java/com/thealgorithms/sorts/SortUtils.java).

Implementing QuickSort in Your Project

Sorting Arrays of Comparable Objects

The most common use case involves sorting an array of boxed primitives or custom objects implementing Comparable.

import com.thealgorithms.sorts.QuickSort;
import com.thealgorithms.sorts.SortAlgorithm;

public class QuickSortDemo {
    public static void main(String[] args) {
        Integer[] numbers = { 42, 7, 19, 3, 55, 13 };
        SortAlgorithm sorter = new QuickSort();          // polymorphic usage
        sorter.sort(numbers);                           // sorts in-place
        // numbers is now [3, 7, 13, 19, 42, 55]
        for (int n : numbers) System.out.print(n + " ");
    }
}

Sorting Java Collections

The SortAlgorithm interface provides a default method for sorting List<T> instances, returning a new sorted list.

import com.thealgorithms.sorts.QuickSort;
import java.util.Arrays;
import java.util.List;

public class QuickSortListDemo {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("pear", "apple", "orange", "banana");
        List<String> sorted = new QuickSort().sort(words);
        // sorted is [apple, banana, orange, pear]
        System.out.println(sorted);
    }
}

Building a Generic Sorting Service

For larger applications, inject QuickSort as a generic sorting strategy.

import com.thealgorithms.sorts.QuickSort;

public class Service {
    private final QuickSort quickSort = new QuickSort();

    public <T extends Comparable<T>> T[] sortData(T[] data) {
        return quickSort.sort(data);   // generic, works for any Comparable type
    }
}

Algorithm Characteristics

Time Complexity: Average case O(n log n), with randomized pivot selection mitigating worst-case O(n²) scenarios.

Space Complexity: O(log n) auxiliary space for the recursive call stack.

Stability: Not stable; equal elements may change relative order.

The implementation uses randomized pivot selection via randomPartition to ensure consistent average-case performance regardless of input order.

Summary

  • Import com.thealgorithms.sorts.QuickSort to access the implementation located at src/main/java/com/thealgorithms/sorts/QuickSort.java.
  • The class implements SortAlgorithm, enabling polymorphic substitution with other sorting algorithms from the repository.
  • Use sort(T[] array) for in-place array sorting or the default sort(List<T>) method for collections.
  • The algorithm is generic and requires elements to implement Comparable<T>.
  • Randomized pivot selection in randomPartition prevents performance degradation on sorted inputs.

Frequently Asked Questions

Can I use QuickSort with custom objects in TheAlgorithms/Java?

Yes. Ensure your custom class implements the Comparable interface. The QuickSort.sort() method accepts any T extends Comparable<T>, allowing you to sort arrays or lists of custom business objects, strings, or boxed primitives.

Where is the QuickSort implementation located in the repository?

The main implementation resides in src/main/java/com/thealgorithms/sorts/QuickSort.java. It relies on SortUtils.java in the same package for helper methods like swap and less, and implements the SortAlgorithm interface defined in SortAlgorithm.java.

Does the TheAlgorithms/Java QuickSort handle already-sorted data efficiently?

Yes. The implementation includes randomPartition, which selects a random pivot element before partitioning. This randomization prevents the algorithm from degrading to O(n²) time complexity when processing already-sorted or reverse-sorted arrays.

How do I test the QuickSort implementation locally?

The repository includes JUnit tests in src/test/java/com/thealgorithms/sorts/QuickSortTest.java. You can run these tests to verify the implementation, or instantiate QuickSort in your own test classes and assert that output arrays match expected sorted sequences.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →