{
    "slug": "sorting_algorithms",
    "term": "Sorting Algorithms",
    "category": "algorithms",
    "difficulty": "intermediate",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "quicksort",
        "mergesort",
        "Timsort"
    ],
    "tags": [
        "algorithms",
        "sorting",
        "computer-science"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "big_o_notation",
        "time_complexity",
        "dynamic_programming"
    ],
    "prerequisites": [
        "big_o_notation",
        "dynamic_programming",
        "array_functions"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Sorting_algorithm"
    ],
    "bad_code": "// Sort inside loop — O(n² log n):\nforeach ($categories as $category) {\n    usort($items, fn($a, $b) => $a->price <=> $b->price); // Re-sorts every iteration\n    displayCategory($category, $items);\n}",
    "good_code": "// Sort once before the loop — O(n log n) total:\nusort($items, fn($a, $b) => $a->price <=> $b->price);\nforeach ($categories as $category) {\n    displayCategory($category, $items); // Uses pre-sorted array\n}",
    "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",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/sorting_algorithms",
        "html_url": "https://codeclaritylab.com/glossary/sorting_algorithms",
        "json_url": "https://codeclaritylab.com/glossary/sorting_algorithms.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Sorting Algorithms](https://codeclaritylab.com/glossary/sorting_algorithms) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/sorting_algorithms"
            }
        }
    }
}