java
39 lines · 7 steps
Quicksort with Lomuto partitioning in Java
A recursive in-place sort that partitions around a pivot and conquers each half.
Explained by
highlit
1public final class Quicksort {
2
3 private Quicksort() {
4 }
5
6 public static void sort(int[] array) {
7 if (array == null || array.length < 2) {
8 return;
9 }
10 quicksort(array, 0, array.length - 1);
11 }
12
13 private static void quicksort(int[] array, int low, int high) {
14 if (low < high) {
15 int pivotIndex = partition(array, low, high);
16 quicksort(array, low, pivotIndex - 1);
17 quicksort(array, pivotIndex + 1, high);
18 }
19 }
20
21 private static int partition(int[] array, int low, int high) {
22 int pivot = array[high];
23 int i = low - 1;
24 for (int j = low; j < high; j++) {
25 if (array[j] <= pivot) {
26 i++;
27 swap(array, i, j);
28 }
29 }
30 swap(array, i + 1, high);
31 return i + 1;
32 }
33
34 private static void swap(int[] array, int a, int b) {
35 int temp = array[a];
36 array[a] = array[b];
37 array[b] = temp;
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Quicksort sorts in place by recursively partitioning subranges around a chosen pivot.
- 2The Lomuto scheme keeps everything ≤ the pivot to the left, then drops the pivot into its final slot.
- 3Guarding the public entry point against null and tiny arrays keeps the recursive core simple.
Related explainers
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
ruby
class KeyTransformer def self.camelize(data) new.camelize(data) end
Recursively camelizing nested Ruby data
recursion
data-transformation
pattern-matching
Intermediate
7 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/quicksort-with-lomuto-partitioning-in-java-explained-java-891b/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.