← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Sorting Algorithms

algorithms PHP 5.0+ Intermediate

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
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 30
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Perplexity 10 Amazonbot 5 SEMrush 3 Unknown AI 2 Google 2 Ahrefs 2
crawler 23 crawler_json 1
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

✓ schema.org compliant