
Spaghetti sort is a
linear-time
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
,
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 analog ...
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for
sorting
Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the data items.
# ordering: arranging items in a sequence ordered by some criterion;
# categorizing: grouping items with similar pro ...
a sequence of items, introduced by
A. K. Dewdney Alexander Keewatin Dewdney (born August 5, 1941) is a Canadian mathematician, computer scientist, author, filmmaker, and conspiracy theorist. Dewdney is the son of Canadian artist and author Selwyn Dewdney, and brother of poet Christopher Dewdney. ...
in his ''
Scientific American
''Scientific American'', informally abbreviated ''SciAm'' or sometimes ''SA'', is an American popular science magazine. Many famous scientists, including Albert Einstein and Nikola Tesla, have contributed articles to it. In print since 1845, it i ...
'' column.
This algorithm sorts a sequence of items requiring ''O''(''n'') stack space in a stable manner. It requires a parallel processor.
Algorithm
For simplicity, assume we are sorting a list of
natural number
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 n ...
s. The sorting method is illustrated using uncooked rods of
spaghetti
Spaghetti () is a long, thin, solid, cylindrical pasta.[spaghetti](_blank)
Dictionary.com. Dictionary.com Unabridg ...
:
# For each number ''x'' in the list, obtain a rod of length ''x''. (One practical way of choosing the unit is to let the largest number ''m'' in the list correspond to one full rod of spaghetti. In this case, the full rod equals ''m'' spaghetti units. To get a rod of length ''x'', break a rod in two so that one piece is of length ''x'' units; discard the other piece.)
# Once you have all your spaghetti rods, take them loosely in your fist and lower them to the table, so that they all stand upright, resting on the table surface. Now, for each rod, lower your other hand from above until it meets with a rod—this one is clearly the longest. Remove this rod and insert it into the front of the (initially empty) output list (or equivalently, place it in the last unused slot of the output array). Repeat until all rods have been removed.
Analysis
Preparing the ''n'' rods of spaghetti takes linear time. Lowering the rods on the table takes constant time,
''O''(''1''). This is possible because the hand, the spaghetti rods and the table work as a fully
parallel computing
Parallel computing is a type of computation in which many calculations or processes are carried out simultaneously. Large problems can often be divided into smaller ones, which can then be solved at the same time. There are several different fo ...
device. There are then ''n'' rods to remove so, assuming each contact-and-removal operation takes constant time, the worst-case time complexity of the algorithm is ''O''(''n'').
References
External links
A. K. Dewdney's homepageImplementations of a model of physical sorting, Boole Centre for Research in InformaticsClassical/Quantum Computing, IFF-Institute
{{DEFAULTSORT:Spaghetti Sort
Sorting algorithms
Metaphors referring to spaghetti