Sorting Algorithms
Also Known As
quicksort
mergesort
Timsort
TL;DR
Algorithms for ordering a collection — ranging from O(n²) simple sorts to O(n log n) comparison sorts and O(n) non-comparison sorts for specific data.
Explanation
Key comparison-based sorts: Bubble (O(n²), educational only), Insertion (O(n²) worst, O(n) nearly-sorted — good for small arrays), Merge (O(n log n) stable, O(n) space), Quick (O(n log n) average, O(n²) worst), Heap (O(n log n), O(1) space). PHP's usort() uses Timsort — hybrid merge+insertion, O(n log n), stable. For integer keys, counting sort and radix sort achieve O(n). Stability matters when sorting objects by multiple fields.
Diagram
flowchart TD
subgraph Comparison Sorts
QUICK[Quicksort<br/>O n log n avg<br/>O n^2 worst]
MERGE[Mergesort<br/>O n log n stable<br/>O n space]
HEAP[Heapsort<br/>O n log n<br/>O 1 space]
end
subgraph Non-comparison
COUNT[Counting Sort<br/>O n+k integers only]
RADIX[Radix Sort<br/>O nk fixed-length keys]
end
PHP[PHP usort<br/>Timsort = Merge+Insertion<br/>O n log n stable]
style PHP fill:#238636,color:#fff
Common Misconception
✗ Quicksort is always the fastest sort — it degrades to O(n²) on already-sorted input without randomisation; PHP's Timsort is faster on real-world (often partially sorted) data.
Why It Matters
Calling sort() inside a loop produces O(n² log n) — sorting once before the loop is a common and impactful optimisation.
Common Mistakes
- Sorting inside a loop when sorting once before would suffice.
- Using usort() with a comparator that returns bool instead of negative/zero/positive — causes incorrect ordering.
- Not using a stable sort when secondary sort order matters — usort() in PHP 8 is stable but older versions were not.
- Reinventing a sort instead of using PHP's built-in usort()/uasort() which use Timsort.
Code Examples
✗ Vulnerable
// Sort inside loop — O(n² log n):
foreach ($categories as $category) {
usort($items, fn($a, $b) => $a->price <=> $b->price); // Re-sorts every iteration
displayCategory($category, $items);
}
✓ Fixed
// Sort once before the loop — O(n log n) total:
usort($items, fn($a, $b) => $a->price <=> $b->price);
foreach ($categories as $category) {
displayCategory($category, $items); // Uses pre-sorted array
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
30
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Perplexity 10
Amazonbot 5
SEMrush 3
Unknown AI 2
Google 2
Ahrefs 2
Also referenced
How they use it
crawler 23
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Medium
⚡ Quick Fix
Use PHP's built-in usort()/uasort()/uksort() for custom sorting — they use an optimised Timsort internally; only implement custom sorting when you need special data structures
📦 Applies To
PHP 5.0+
web
cli
🔗 Prerequisites
🔍 Detection Hints
Manual bubble sort or selection sort implementation when PHP built-ins would serve; sorting inside a loop
Auto-detectable:
✗ No
blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Function
Tests: Update