> For the complete documentation index, see [llms.txt](https://liuzhenglaichn.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuzhenglaichn.gitbook.io/algorithm/sort/sorting.md).

# Sorting

<figure><img src="https://851655761-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7JACxIEuMBezDCzfss%2Fuploads%2FWqUXcDNTYGRqb5ntFd31%2Fimage.png?alt=media&amp;token=daabd025-7ad9-4227-a289-251d806aa72b" alt=""><figcaption></figcaption></figure>

Which algorithm works best for largely sorted array?

* Insertion Sort
* Bubble Sort

<https://stackoverflow.com/a/4276118/3127828>

Only a few items => INSERTION SORT

Items are mostly sorted already => INSERTION SORT

Concerned about worst-case scenarios => HEAP SORT

Interested in a good average-case result => QUICKSORT

Items are drawn from a dense universe => BUCKET SORT

Desire to write as little code as possible => INSERTION SORT
