Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

There is no universally best sorting algorithm. The right choice depends on the data, whether equal-key records must keep their original order, available memory, and the performance guarantees you need. This guide explains ten useful algorithms with one shared example, then compares their trade-offs. The ten are an educational selection, not a canonical ranking.

Formally, sorting arranges items in a chosen order while preserving the input as a permutation: the output contains the same items, not merely a selection of them. NIST’s definition of sorting is a useful baseline.

How to compare sorting algorithms

Big-O time is only part of the decision. NIST notes that relevant factors include memory, the range and orderliness of keys, and the costs of comparing and moving items. These comparisons describe standard teaching versions; implementation details can change properties such as stability, memory use, or best-case behavior.

  • Stable: equal-key items remain in their original relative order. This matters when sorting records by multiple fields in successive passes—for example, sort by department, then stably by last name. Cornell’s sorting lecture covers stability and adaptivity.
  • In-place: the algorithm uses little auxiliary storage beyond the input array. This does not mean it uses no memory at all; recursive algorithms may use a call stack.
  • Adaptive: the algorithm can take advantage of existing order in the input. Insertion sort is a classic example.
  • Comparison-based: items are ordered by comparing pairs. Counting and radix sorts instead exploit restricted key representations, so their linear-looking bounds do not apply to arbitrary keys without qualification.

For a consistent trace, each example below sorts [5, 2, 4, 1] into ascending order. The trace illustrates the operation that makes progress; it is not a benchmark.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

10 sorting algorithms, explained

1. Bubble sort

Bubble sort repeatedly compares adjacent values and swaps them when they are out of order. A pass moves a large value toward the end; passes continue until the sequence is sorted. For the example, the first pass transforms [5, 2, 4, 1] into [2, 4, 1, 5]: 5 moves right as it is compared with each neighbor. A version that stops when a pass makes no swaps can finish quickly on already sorted input; without that check, it does not gain that best-case improvement.

2. Selection sort

Selection sort finds the smallest item in the unsorted portion and places it at the next output position. Starting with [5, 2, 4, 1], it selects 1 and swaps it into the first position, giving [1, 2, 4, 5]; it then repeats on the remaining suffix. It makes a quadratic number of comparisons even when the input is already ordered.

3. Insertion sort

Insertion sort grows a sorted prefix by inserting each next item into its correct position. With [5, 2, 4, 1], insert 2 before 5 to get [2, 5, 4, 1], then insert 4 between them to get [2, 4, 5, 1]. It is stable when equal items are not moved past one another, and it is adaptive: nearly sorted input can require far less work than the worst case. Cornell describes insertion sort as stable and adaptive, with quadratic worst-case time and constant extra space in its presentation.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

4. Merge sort

Merge sort divides the sequence into smaller parts, sorts those parts recursively, and merges the sorted results. For example, split [5, 2, 4, 1] into [5, 2] and [4, 1]; sort them as [2, 5] and [1, 4]; then merge to produce [1, 2, 4, 5]. A merge that takes from the left side first when keys are equal is stable. The usual array implementation uses linear auxiliary space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

5. Quicksort

Quicksort chooses a pivot, partitions items into those on either side of it, and recursively sorts the partitions. Using 4 as the pivot for [5, 2, 4, 1], one possible partition is [2, 1] | 4 | [5]; sorting the two sides yields [1, 2, 4, 5]. Performance depends on partition quality and pivot strategy. Cornell gives expected O(n log n) and worst-case O(n²) time; standard in-place partitioning is not stable.

6. Heap sort

Heap sort arranges the data into a heap, a structure in which the maximum (for ascending output) can be retrieved at the root. It repeatedly moves that maximum to the end of the remaining unsorted region and restores the heap. For the example, after building a max-heap, remove the maximum 5 to the final position, then repeat with the remaining values until the array is [1, 2, 4, 5]. The standard array version is in-place, unstable, and has O(n log n) best, average, and worst-case time.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

7. Counting sort

Counting sort counts how often each key appears, then reconstructs the output in key order. For [5, 2, 4, 1], the counts for keys 1 through 5 are [1, 1, 0, 1, 1], which reconstruct as [1, 2, 4, 5]. Its time and space depend on the key range, not just the number of items; it is useful when keys are integers in a reasonably small bounded range. A stable version uses cumulative counts and places items into an output array; the simple reconstruction above does not itself establish stability for records.

8. Radix sort

Radix sort processes keys digit by digit (or by another sequence of positions), using a stable grouping sort at each position. For the one-digit values in the example, a single stable digit pass orders them as [1, 2, 4, 5]. For multi-digit keys, the order of passes and the stability of each pass matter: least-significant-digit radix sort starts with the last digit and proceeds left, preserving earlier ordering through stable passes. Its bounds depend on the number of digits and the cost of each pass.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

9. Bucket sort

Bucket sort distributes values into ordered ranges, sorts values inside each bucket, then concatenates the buckets. For the example, buckets for ranges 1–2 and 3–5 could receive [2, 1] and [5, 4]; sorting within each gives [1, 2] and [4, 5], which concatenate to [1, 2, 4, 5]. Its performance depends on choosing suitable ranges and on how evenly values distribute. It is not a general guarantee of linear time for arbitrary inputs.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

10. Shell sort

Shell sort performs insertion-like passes over items separated by a gap, reducing the gap until it reaches one. With a gap of 2 on [5, 2, 4, 1], compare and order positions 0 and 2, then positions 1 and 3, producing [4, 1, 5, 2]. Further gap passes reduce long-distance disorder; the final gap-1 pass is insertion sort. Its time bounds depend on the gap sequence, so there is no single complexity figure that describes every version.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

At-a-glance comparison

Here, n is the number of items and k is the size of a bounded integer key range. For radix sort, d is the number of digit positions and b the number of possible values per position. These are standard algorithmic bounds, not measured runtimes; counting and radix figures rely on their stated key assumptions. The comparison reflects common teaching implementations; exact space and stability can vary by implementation.

Algorithm Best time Average time Worst time Auxiliary space Stable? In-place? Adaptive? Key assumption or caveat
Bubble O(n) with early-exit check O(n²) O(n²) O(1) Yes, if equal items are not swapped Yes Yes, with early exit Comparison-based; mainly useful for teaching
Selection O(n²) O(n²) O(n²) O(1) No, in the usual swap-based version Yes No Comparison-based; comparison count remains quadratic
Insertion O(n) on already sorted input O(n²) O(n²) O(1) Yes, with conventional shifting Yes Yes Comparison-based; effective on small or nearly sorted data
Merge O(n log n) O(n log n) O(n log n) O(n) for the usual array version Yes, with tie-aware merge No, in the usual array version No, in the usual version Comparison-based; predictable bound trades extra array storage
Quick O(n log n) with balanced partitions Expected O(n log n) O(n²) O(log n) expected stack; O(n) worst stack No, in the usual in-place version Usually, aside from recursion stack No Comparison-based; pivot and partition strategy affect behavior
Heap O(n log n) O(n log n) O(n log n) O(1) for the standard array version No Yes No Comparison-based; array implementation gives a worst-case bound
Counting O(n + k) O(n + k) O(n + k) O(n + k) for a stable output-array version Yes, in stable versions No, in the usual stable version No Integer keys in a bounded range of size k
Radix O(d(n + b)) O(d(n + b)) O(d(n + b)) O(n + b) for common stable-pass implementations Yes, when each digit pass is stable No, in common output-array versions No Keys must have processable digits/positions; cost depends on d and b
Bucket O(n + m) under favorable distribution Distribution-dependent O(n²) if many values land in one bucket and it is sorted quadratically O(n + m) Depends on bucket sorting and distribution No, in the usual bucket-array form No m is bucket count; useful ranges and even distribution matter
Shell Gap-sequence dependent Gap-sequence dependent Gap-sequence dependent O(1) No, in the usual version Yes Not generally characterized as adaptive Bounds vary with the selected gap sequence

The broad comparison categories and illustrative bounds are consistent with educational references such as the DSAMaster sorting guide; those labels should not be read as a substitute for checking a particular implementation. Cornell’s lecture provides detailed treatment of insertion, merge, and quicksort behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Which sorting algorithm should you use?

  • For a tiny or nearly sorted sequence: insertion sort is a useful fit because its work can shrink as the input becomes more ordered.
  • For stable sorting with a predictable O(n log n) bound: merge sort is a clear choice when the extra array space is acceptable.
  • To understand a common fast general-purpose approach: quicksort is instructive, provided you account for pivot behavior and its quadratic worst case.
  • For bounded integer keys: consider counting sort when the key range is manageable, or radix sort when keys have a suitable digit representation and stable passes are available.
  • For in-place sorting with O(n log n) worst-case time: heap sort offers that asymptotic guarantee, but it is not stable.
  • For keys that fit meaningful ordered buckets: bucket sort may help when the distribution is favorable; performance can degrade when buckets become unbalanced.

These are conceptual recommendations, not benchmark results or universal production prescriptions. Real language libraries may use hybrid algorithms and implementation-specific guarantees. Check the official documentation for the language and runtime you actually use before relying on a library’s stability, memory use, or complexity.

Further reading

For a formal definition and discussion of factors that influence algorithm choice, see NIST’s Dictionary of Algorithms and Data Structures entry on sorting. Cornell’s CS 2110 lecture on sorting algorithms explains stability, adaptivity, and key complexity distinctions. MIT OpenCourseWare also has a sorting lecture note. For a textbook treatment, Pearson’s catalog lists Robert Sedgewick and Kevin Wayne’s Algorithms, 4th edition, with a chapter devoted to sorting.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.