Sorting Algorithms
Sorting#
Sorting organizes data into a specific order, making it easier to search, analyze, and process. While many sorting algorithms exist (QuickSort, MergeSort, HeapSort, TimSort, IntroSort, and more), modern programming languages provide highly optimized built-in sorting methods that you should use in practice.
What You Need to Know#
- Many algorithms exist, but you don't need to implement them yourself. Each language's standard library has already chosen and optimized the best sorting algorithm for general use.
- Built-in sorting is fast: Most languages use hybrid algorithms like TimSort (Python, Java) or IntroSort (Swift) that guarantee O(n log n) performance.
- Use built-in methods: They're battle-tested, highly optimized, and handle edge cases you might not think of.
- Custom comparators: You can easily customize how items are sorted without implementing a sorting algorithm from scratch.
Standard Library Algorithms#
Each language uses different sorting algorithms under the hood:
| Language | Algorithm | Time Complexity | Notes |
|---|---|---|---|
| Java | Dual-Pivot QuickSort (primitives), TimSort (objects) | O(n log n) | Stable for objects |
| Kotlin | TimSort | O(n log n) | Stable |
| TypeScript | TimSort | O(n log n) | Stable |
| Dart | TimSort | O(n log n) | Stable |
| Swift | IntroSort | O(n log n) | Not stable |
| Python | TimSort | O(n log n) | Stable |
Stable sorting means that equal elements keep their original order. This matters when sorting by multiple criteria.
Using Built-in Sorting#
import java.util.*;
// Basic sorting
int[] numbers = {64, 34, 25, 12, 22};
Arrays.sort(numbers); // [11, 12, 22, 25, 34, 64]
List<String> names = Arrays.asList("Charlie", "Alice", "Bob");
Collections.sort(names); // [Alice, Bob, Charlie]
// Custom sorting by age
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 20)
);
Collections.sort(people, Comparator.comparing(Person::getAge));
// Reverse order
Collections.sort(names, Collections.reverseOrder());
// Basic sorting
val numbers = mutableListOf(64, 34, 25, 12, 22)
numbers.sort() // [11, 12, 22, 25, 34, 64]
val names = listOf("Charlie", "Alice", "Bob")
val sortedNames = names.sorted() // [Alice, Bob, Charlie]
// Custom sorting by age
data class Person(val name: String, val age: Int)
val people = listOf(
Person("Alice", 25),
Person("Bob", 30),
Person("Charlie", 20)
)
val sortedByAge = people.sortedBy { it.age }
// Reverse order
val sortedDesc = people.sortedByDescending { it.name }
// Basic sorting
const numbers = [64, 34, 25, 12, 22];
numbers.sort((a, b) => a - b); // [11, 12, 22, 25, 34, 64]
const names = ["Charlie", "Alice", "Bob"];
names.sort(); // ["Alice", "Bob", "Charlie"]
// Custom sorting by age
interface Person {
name: string;
age: number;
}
const people: Person[] = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 20 }
];
people.sort((a, b) => a.age - b.age);
// Reverse order
numbers.sort((a, b) => b - a);
// Basic sorting
List<int> numbers = [64, 34, 25, 12, 22];
numbers.sort(); // [11, 12, 22, 25, 34, 64]
List<String> names = ["Charlie", "Alice", "Bob"];
names.sort(); // [Alice, Bob, Charlie]
// Custom sorting by age
class Person {
final String name;
final int age;
Person(this.name, this.age);
}
List<Person> people = [
Person("Alice", 25),
Person("Bob", 30),
Person("Charlie", 20)
];
people.sort((a, b) => a.age.compareTo(b.age));
// Reverse order
numbers.sort((a, b) => b.compareTo(a));
// Basic sorting
var numbers = [64, 34, 25, 12, 22]
numbers.sort() // [11, 12, 22, 25, 34, 64]
let names = ["Charlie", "Alice", "Bob"]
let sortedNames = names.sorted() // [Alice, Bob, Charlie]
// Custom sorting by age
struct Person {
let name: String
let age: Int
}
let people = [
Person(name: "Alice", age: 25),
Person(name: "Bob", age: 30),
Person(name: "Charlie", age: 20)
]
let sortedByAge = people.sorted { $0.age < $1.age }
// Reverse order
let reversed = numbers.sorted(by: >)
# Basic sorting
numbers = [64, 34, 25, 12, 22]
sorted_numbers = sorted(numbers) # [11, 12, 22, 25, 34, 64]
names = ["Charlie", "Alice", "Bob"]
sorted_names = sorted(names) # ["Alice", "Bob", "Charlie"]
# Custom sorting by age
people = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 20}
]
sorted_by_age = sorted(people, key=lambda p: p["age"])
# Reverse order
sorted_desc = sorted(numbers, reverse=True)
Sorting is a fundamental operation that all modern programming languages handle efficiently through their standard libraries. Simply use the built-in sorting methods—they're fast, reliable, and already optimized for you.