Bead sort, also called gravity sort, is a
natural
Nature, in the broadest sense, is the physical world or universe. "Nature" can refer to the phenomena of the physical world, and also to life in general. The study of nature is a large, if not the only, part of science. Although humans are ...
sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is importan ...
, developed by
Joshua J. Arulanandham,
Cristian S. Calude
Cristian Sorin Calude (born 21 April 1952) is a Romanian-New Zealander
mathematician and computer scientist.
Biography
After graduating from the Vasile Alecsandri National College in Galați, he studied at the University of Bucharest, where he ...
and
Michael J. Dinneen in 2002, and published in ''The Bulletin of the
European Association for Theoretical Computer Science
The European Association for Theoretical Computer Science (EATCS) is an international organization with a European focus, founded in 1972. Its aim is to facilitate the exchange of ideas and results among theoretical computer scientists as well as ...
''.
Both
digital
Digital usually refers to something using discrete digits, often binary digits.
Technology and computing Hardware
*Digital electronics, electronic circuits which operate using digital signals
**Digital camera, which captures and stores digital i ...
and
analog
Analog or analogue may refer to:
Computing and electronics
* Analog signal, in which information is encoded in a continuous variable
** Analog device, an apparatus that operates on analog signals
*** Analog electronics, circuits which use analo ...
hardware
implementation
Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy.
Industry-specific definitions
Computer science
In computer science, an implementation is a real ...
s of bead sort can achieve a sorting time of ''
O''(''n''); however, the implementation of this algorithm tends to be significantly slower in
software
Software is a set of computer programs and associated software documentation, documentation and data (computing), data. This is in contrast to Computer hardware, hardware, from which the system is built and which actually performs the work.
...
and can only be used to sort lists of
positive integer
In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country").
Numbers used for counting are called '' cardinal ...
s. Also, it would seem that even in the best case, the algorithm requires ''
O''(''n
2'') space.
Algorithm overview

The bead sort operation can be compared to the manner in which beads slide on parallel poles, such as on an
abacus
The abacus (''plural'' abaci or abacuses), also called a counting frame, is a calculating tool which has been used since ancient times. It was used in the ancient Near East, Europe, China, and Russia, centuries before the adoption of the H ...
. However, each pole may have a distinct number of beads. Initially, it may be helpful to imagine the beads suspended on vertical poles. In Step 1, such an arrangement is displayed using ''n=5'' rows of beads on ''m=4'' vertical poles. The numbers to the right of each row indicate the number that the row in question represents; rows 1 and 2 are representing the positive integer 3 (because they each contain three beads) while the top row represents the positive integer 2 (as it only contains two beads).
[By convention, a row representing the positive integer ''k'' should have beads on poles 1..''k'' and poles ''k''+1..''m'' should be empty. This is not a strict requirement, but will most likely simplify implementation.]
If we then allow the beads to fall, the rows now represent the same integers in sorted order. Row 1 contains the largest number in the set, while row ''n'' contains the smallest. If the above-mentioned convention of rows containing a series of beads on poles 1..''k'' and leaving poles ''k''+1..''m'' empty has been followed, it will continue to be the case here.
The action of allowing the beads to "fall" in our physical example has allowed the larger values from the higher rows to propagate to the lower rows. If the value represented by row ''a'' is smaller than the value contained in row ''a+1'', some of the beads from row ''a+1'' will fall into row ''a''; this is certain to happen, as row ''a'' does not contain beads in those positions to stop the beads from row ''a+1'' from falling.
The mechanism underlying bead sort is similar to that behind
counting sort
In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small positive integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that possess d ...
; the number of beads on each pole corresponds to the number of elements with value equal or greater than the index of that pole.
Complexity
Bead sort can be implemented with four general levels of complexity, among others:
*''
O''(1): The beads are all moved simultaneously in the same time unit, as would be the case with the simple physical example above. This is an abstract complexity, and cannot be implemented in practice.
*''
O''(
): In a realistic physical model that uses gravity, the time it takes to let the beads fall is proportional to the square root of the maximum height, which is proportional to n.
*''
O''(n): The beads are moved one row at a time. This is the case used in the analog and
digital hardware
Digital electronics is a field of electronics involving the study of digital signals and the engineering of devices that use or produce them. This is in contrast to analog electronics and analog signals.
Digital electronic circuits are usually ...
solutions.
*''
O''(S), where S is the sum of the integers in the input set: Each bead is moved individually. This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads, such as in software implementations.
Like the
Pigeonhole sort, bead sort is unusual in that in worst case it can perform faster than ''
O''(''n''
log
Log most often refers to:
* Trunk (botany), the stem and main wooden axis of a tree, called logs when cut
** Logging, cutting down trees for logs
** Firewood, logs used for fuel
** Lumber or timber, converted from wood logs
* Logarithm, in mat ...
''n''), the fastest performance possible for a
comparison sort
A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a "less than or equal to" operator or a three-way comparison) that determines which of two elements should oc ...
in worst case. This is possible because the key for a bead sort is always a positive integer and bead sort exploits its structure.
Implementation
This implementation is written in
Python; it is assumed that the will be a sequence of integers. The function returns a new list rather than mutating the one passed in, but it can be trivially modified to operate in place efficiently.
def beadsort(input_list):
"""Bead sort."""
return_list = []
# Initialize a 'transposed list' to contain as many elements as
# the maximum value of the input -- in effect, taking the 'tallest'
# column of input beads and laying it out flat
transposed_list = * max(input_list)
for num in input_list:
# For each element (each 'column of beads') of the input list,
# 'lay the beads flat' by incrementing as many elements of the
# transposed list as the column is tall.
# These will accumulate atop previous additions.
transposed_listnum
Num may refer to:
* Short for number
* Num (god), the creator and high god of the Nenets people of Siberia
* Short for the Book of Numbers of the Hebrew Bible
* Khnum, a god of Egyptian mythology
* Mios Num, an island of western New Guinea
* Num, ...
= + 1 for n in transposed_list[:num
# We've now dropped the beads. To de-transpose, we count the
# 'bottommost row' of dropped beads, then mimic removing this
# row by subtracting 1 from each 'column' of the transposed list.
# When a column does not reach high enough for the current row,
# its value in transposed_list will be <= 0.
for i in range(len(input_list)):
# Counting values > i is how we tell how many beads are in the
# current 'bottommost row'. Note that Python's bools can be
# evaluated as integers; True 1 and False 0.
return_list.append(sum(n > i for n in transposed_list))
# The resulting list is sorted in descending order
return return_list
We can also implement the algorithm using Java (programming language)">Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
.