You are on page 1of 7

Comparison of algorithms[edit]

In this table, n is the number of records to be sorted. The columns "Average" and "Worst" give the time complexity in each case, under the assumption that the length of each key is constant, and that therefore all comparisons, swaps, and other needed operations can proceed in constant time. "Memory" denotes the amount of auxiliary storage needed beyond that used by the list itself, under the same assumption. These are all comparison sorts, , and so cannot perform better than O(n log n) in the average or worst case. The run time and the memory of algorithms could be measured using various notations like theta, omega, Big-O, small-o, o, etc. The memory and the run times below are applicable for a all ll the 5 notations.

Comparison sorts

Name

Best

Averag e

Worst

Memor y

Stab le

Metho d

Other notes

Quicksort

Quicksort is usually done in place with O(log(n)) stack typical [citation needed] space. Most in-place sort is implementations are on not Partitioni unstable, as stable inin average, ng place partitioning is worst case stable; stable more is version complex. Nave variant s use an O(n) space s exist array to store the [citation needed] partition.

Depends Merge sort


needed]

[fur

ther explanation

; worst case is

Yes

Highly parallelizable (up to O(log(n)) using the Three Hungarian's Merging Algorithm or more practically, Cole's parallel merge sort) for processing large amounts of data.

In-place merge

Yes

Merging Can be implemented as a stable sort based

Comparison sorts

Name

Best

Averag e

Worst

Memor y

Stab le

Metho d

Other notes

sort

on stable in-place in [2] merging.

Heapsort

No

Selection

Insertion sort

Yes

O(n + d), where d is Insertion the number of inversions

Introsort

No

Partitioni Used in ng & several STL implement Selection ations

Selection sort

No

Stable with O(n) extra Selection space, for example [3] using lists.

Timsort

Yes

comparisons when Insertion the data is already & sorted or reverse Merging sorted.

Shell sort

or

Depends on gap sequence; best known is

No

Small code size, no use of call stack, reasonably fast, useful Insertion where memory is at a premium such as embedded and older mainframe applications

Comparison sorts

Name

Best

Averag e

Worst

Memor y

Stab le

Metho d

Other notes

Bubble sort

Yes

Exchangi Tiny code size ng

Binary tree sort

Yes

When using a selfInsertion balancing binary search tree

Cycle sort

No

In-place In with Insertion theoretically optimal number of writes

Library sort

Yes

Insertion

Patience sorting

No

Finds all the longest Insertion increasing & subsequences within Selection O(n log n)

Smoothsort

No

An adaptive sort comparisons when the Selection data is already sorted, and 0 swaps.

Strand sort

Yes

Selection

Tournament sort

[4]

Selection

Comparison sorts

Name

Best

Averag e

Worst

Memor y

Stab le

Metho d

Other notes

Cocktail sort

Yes

Exchangi ng

Comb sort

No

Exchangi Small code size ng

Gnome sort

Yes

Exchangi Tiny code size ng

Franceschini's [5] method

No

The following table describes integer sorting algorithms and other sorting algorithms that are not comparison sorts. . As such, they are not limited by a lower bound. Complexities below are in terms of n, the number of items to be sorted, k, the size of each key, and d, the digit size used by the implementation. Many of them are based on the assumption that the key size i is s large enough that all k entries have unique key values, and hence that n << 2 , where << means "much less than."

Non-comparison sorts

Name

Best

Average

Worst

Memory

Stable

n << k 2

Notes

Pigeonhole sort

Yes

Yes

Bucket sort (uniform

Yes

No

Assumes uniform distribution of elements from the

Non-comparison sorts

Name

Best

Average

Worst

Memory

Stable

n << k 2

Notes

keys)

domain in the [6] array.

Bucket sort (integer keys)

r is the range of numbers to be sorted. If r Yes Yes = Avg RT =


[7]

then

r is the range of numbers to be sorted. If r Counting sort Yes Yes = Avg RT =


[6]

then

LSD Radix Sort

Yes

No

[6][7]

MSD Radix Sort

Yes

No

Stable version uses an external array of size n to hold all of the bins

MSD Radix Sort

No

No

In-Place. k / d recursion levels, d 2 for count array

Non-comparison sorts

Name

Best

Average

Worst

Memory

Stable

n << k 2

Notes

Spreadsort

No

No

Asymptotics are based on the assumption that n k << 2 , but the algorithm does not require this.

The following table describes some sorting algorithms that are impractical for real real-life life use due to extremely poor performance or a requirement for specialized hardware.

Na me

Be st

Avera ge

Wor st

Memor y

Stab le

Compari son

Other notes

Bead sort

N/A

N/A

N/A

No

Requires specialized hardware

Simple pancak e sort

No

Yes

Count is number of flips.

Spagh etti (Poll) sort

Yes

Polling

This A linear-time, time, analog algorithm for sorting a sequence of items, requiring O(n) stack space, and the sort is stable. This requires requir parallel processors. Spaghetti sort#Analysis

Sorting networ ks

Yes

No

Requires a custom circuit of size

Additionally, theoretical computer scientists have detailed other sorting algorithms that provide better than time complexity with additional constraints, including:

Han's algorithm, a deterministic terministic algorithm for sorting keys from a domain of finite size, taking taking time and time and space. space.
[8]

Thorup's algorithm, a randomized algorithm for sorting keys from a domain of finite size,
[9]

A randomized integer sorting algorithm taking [10] space.

expected time and

Algorithms not yet compared above include:

You might also like