api: YAML JSON TOON Database version: 1.0.0 format: toon dataset: id: 2897 slug: sorting-algorithms-comparison title: Sorting Algorithms Comparison description: "Time and space complexity comparison of sorting algorithms: Bubble, Selection, Insertion, Merge, Quick, Heap, Counting, and Radix Sort with stability notes." summary: "A comparison of sorting algorithms by best, average and worst time, space and stability: bubble, selection, insertion, merge, quick, heap, counting and radix sort, extended with Timsort, introsort, bucket sort and shell sort." use_when[2]: You need to choose a sorting algorithm for data of a given size and shape.,You need to know whether a sort is stable. caveats[2]: "Counting, radix and bucket sort are not comparison sorts and depend on the range or distribution of keys.","Library sorts are usually hybrids (Timsort, introsort), so real behavior differs from textbook algorithms." related[4]: sorting-algorithms,complexity-analysis,search-algorithms,python-builtins entry_count: 12 fields[6]{section,field,type,example}: algorithms,name,string,Bubble Sort algorithms,best,string,O(n) algorithms,average,string,O(n^2) algorithms,worst,string,O(n^2) algorithms,space,string,O(1) algorithms,stable,boolean,"true" category: Data Structures category_slug: data-structures tags: "algorithms,sorting,complexity,big-o" view_count: 52 created_at: 1777673262 updated_at: 1777673262 data: algorithms[12]{name,best,average,worst,space,stable}: Bubble Sort,O(n),O(n^2),O(n^2),O(1),true Selection Sort,O(n^2),O(n^2),O(n^2),O(1),false Insertion Sort,O(n),O(n^2),O(n^2),O(1),true Merge Sort,O(n log n),O(n log n),O(n log n),O(n),true Quick Sort,O(n log n),O(n log n),O(n^2),O(log n),false Heap Sort,O(n log n),O(n log n),O(n log n),O(1),false Counting Sort,O(n+k),O(n+k),O(n+k),O(k),true Radix Sort,O(d(n+k)),O(d(n+k)),O(d(n+k)),O(n+k),true Timsort,O(n),O(n log n),O(n log n),O(n),true Introsort,O(n log n),O(n log n),O(n log n),O(log n),false Bucket Sort,O(n + k),O(n + k),O(n^2),O(n + k),true Shell Sort,O(n log n),Depends on gap sequence,O(n^2) for simple gaps,O(1),false