Bead-Sort: A Natural Sorting Algorithm: 1 Beads and Rods
Bead-Sort: A Natural Sorting Algorithm: 1 Beads and Rods
Bead-Sort: A Natural Sorting Algorithm: 1 Beads and Rods
Abstract
Nature is not only a source of minerals and precious stones but is also a mine
of algorithms. By observing and studying natural phenomena, computer algorithms
can be extracted. In this note, a simple natural phenomenon is used to design a
sorting algorithm for positive integers, called here Bead–Sort. The algorithm’s run–
time complexity ranges from O(1) to O(S) (S is the sum of the input integers)
depending on the user’s perspective. Finally, three possible implementations are
suggested.
number 3 number 2
beads
Fig. 1
2
4
3
4
3
4
2
Fig. 2 (a) shows the numbers 4 and 3 (represented by beads) attached to rods; beads
displayed in Fig. 2 (a) appear to be suspended in the air, just before they start sliding
down. Fig. 2 (b) shows the state of the frame (a frame is a structure with the rods
and beads) after the beads are ‘allowed’ to slide down. The row of beads representing
number 3 has ‘emerged’ on top of the number 4 (the ‘extra’ bead in number 4 has dropped
down one ‘level’). Fig. 2 (c) shows numbers of different sizes, suspended one over the
other (in a random order). We allow beads (representing numbers 3, 2, 4 and 2) to slide
down to obtain the same set of numbers, but in a sorted order again (see Fig. 2 (d)). In
this process, the smaller numbers emerge above the larger ones and this creates a natural
comparison (an online animation of the above process can be seen at [1]).
To study our main procedure, called Bead–Sort, we will adopt a few conventions.
Rods (vertical lines) are counted always from left to right and levels are counted from
bottom to top as shown in Fig. 3. A frame is a structure consisting of rods and beads.
n
.
.
.
...
Levels
1 2 3 ... m
Rods
Fig. 3
2
Consider a set A of n positive integers to be sorted and assume the biggest number
in A is m. Then, the frame should have at least m rods and n levels. The Bead–Sort
algorithm is the following:
1
Note that, physically, there are no ‘swaps’, but, the proof assumes the occurrence of ‘pseudo–swaps’,
for convenience; the actual algorithm operates in one unit of time to do all these pseudo–swaps.
3
R 1,n R 2,n R m,n Level n
.. .. . . . ..
. . .
Level 2
V1 V2 Vm
R 1,2 R 2,2 R m,2
Fig. 4
R 2,n R m,n
R 1,n
.. .. ..
. . .. . .
R 2,2 R m,2
R 1,2
R 1,1
R 2,1 R m,1
Trim Trim Trim
v1
.. .
Data Entry Device
Fig. 5
4
The voltage across each resistor is fed into a trimmer circuit (threshold unit) whose
output is given by xi,j = 1, if xi,j ≥ t, xi,j = 0, otherwise. This is shown in Fig. 5, where
dotted lines/dashes do not indicate physical connections. Let v1 , v2 , . . . ,vn denote the
sums of individual voltages across resistors (after thresholding) in the 1st , 2nd , . . . ,nth
levels, respectively (note that in Fig. 5, the detailed circuitry for adding voltages is not
shown). Hence we have:
v1 = x1,1 + x2,1 + . . . + xm,1 ,
v2 = x1,2 + x2,2 + . . . + xm,2 ,
..
.
vn = x1,n + x2,n + . . . + xm,n .
The data–entry device is used to enter data to be sorted. When an integer is keyed
in, the equivalent unary representation is generated (for instance, the number 3 is rep-
resented as three 1’s followed by 0’s). The output lines from the data–entry device are
attached to the rods consisting of series of resistors as shown in Fig. 5, the first unary
digit connected to the 1st rod, and so on. For every ‘1’ in the ith digit of the unary
representation, a voltage increment δv is applied to the corresponding ith rod (of resis-
tors), i.e. the voltage across the ith rod is increased by δv. This is the way a new bead
is introduced onto a rod.
Every time a new data is entered, the individual resistors would have a different
voltage level; and these voltage levels should reflect the presence/absence of a bead in the
frame at that point of time. Hence, the values of the resistors, the voltage increment δv
and the threshold–voltage t should be designed accordingly. After the whole data has
been entered, the voltage vector v1 , v2 , . . . ,vn will contain the sorted list (in descending
order).
To get a concrete example, consider a simple analog resistor circuit (see Fig. 6) that
can sort 3 positive integers, the biggest of which is 3.
1 1 1 v3
2 2 2 v2
3 3 3 v1
Fig. 6
The circuit has three rods and three levels, 9 resistors on the whole. Let us show how it
can sort the data set {2, 1, 3}. The total resistance in each rod R is 1Ω + 2Ω + 3Ω = 6Ω.
The threshold voltage t is 0.5 volt; the voltage increment δv for every new bead is 1
5
volt. The integers 2, 1, 3 are entered one by one; the corresponding changes in voltage
levels across individual resistors and the current after entering the data 2 (110 in unary),
1 (100), 3 (111) are shown in Fig. 7 (a), 7 (b) and 7 (c), respectively. The slanting
arrows represent the flow of current and the values of current are shown in amperes.
The figures written close to the individual resistors represent the voltage levels across
them (xi,j ); those within the brackets represent the voltage after thresholding (xi,j ). The
corresponding state of the frame is shown together. Note that after the last integer has
been entered, v1 , v2 and v3 represent the sorted list.
1 1
6 6 0
(a)
1 1
3 6 0
(b)
1 1 1
2 3 6
(c)
Fig. 7
The time complexity of sorting using the above implementation is due to two com-
ponents: data entry time and the actual sorting time (time taken for electrical charges
6
to settle down). In this implementation, data entry and sorting action alternate each
other; sorting does not wait till all data have been entered. Let t1 be the average time
taken for entering a single data item and t2 be the average charge settling–time. It takes
t1 + t2 units for every single data item in the set. The overall complexity is therefore
n × (t1 + t2 ), i.e. O(n).
In the case of manual data entry, t2 will be so small compared to t1 such that the
actual sorting time (before the next data is entered) is negligible.
0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 0 0
1 1 1 1 0 0 1 1 0
1 0 0 1 1 1 1 1 1
Fig. 8
Each cell has two discrete states. A cell in state ‘1’ represents the space occupied by a
bead and a cell in state ‘0’ represents a space without any bead. The local CA rule used
to ‘roll down’ a bead into its ‘empty’ lower–neighbor is “a state–1 cell and its state–0
(empty) lower–neighbor cell always swap their states”. Table 1 shows in detail how this
rule is applied. The local rules are applied until no change in the configuration can be
obtained by any further application. The snap-shots of the CA configurations that evolve
while sorting the data-set {1, 3, 2, 1} are shown in Fig. 8.
The number of sequential updates of CA configuration necessary for sorting a partic-
ular set of data would depend on the ‘degree of disorder’ (entropy) associated with the
initial configuration (state) of the frame; this is the extent to which the initial state of
the frame ‘deviates’ from the state of a sorted frame (showing the same set of data in
sorted form). In the worst case, there will not be more than n − 1 sequential updates of
CA configuration (note that state–changes in a CA are simultaneous) which leads to a
worst–case complexity of O(n).
7
Present State Upper–neighbour(x) Lower–neighbour(x) Next State
of Cell x of Cell x
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
Table 1
flip-flops
1 0 0 0 (cells)
1 1 1 1
4 1 1 0 0
(levels)
3 1 0 0 0
2 1 0 0 0
j
1 1 1 1 0
1 2 3 4
i (rods)
Fig. 9
There are several possible approaches to make beads ‘go down’ one of which is discussed
here. The input data (in unary form) is entered one–by–one into a data entry register as
shown in Fig. 10. Let the register be represented by a Boolean vector Bead[i]; Bead[i]
= 1 indicates that a bead is to be ‘dropped’ along the ith rod. All 1–bit cells from the
register are to be shifted ‘downward’ into the two dimensional array of cells cell[i, j]
(representing a frame) beneath, before the next data is entered. Thus, after the last data
is entered, the whole set would already be sorted in the array of cells.
A simple logic circuit shown in Fig. 10 is connected to each and every cell in the two
dimensional array of cells (the frame); when Bead[i] = 1, the circuit helps to locate that
cell in the ith rod which should ‘receive’ the bead. The circuit attached to that cell alone
‘triggers’ and makes it flip to a 1–state. Thus, shifting the bead downward is quite fast;
the 1–bit states in the register (the beads) do not have to ‘propagate’ cell–by–cell along
the corresponding rods.
8
Data Entry Register
1 1 1 0
0 0 0 0 Bead[i] 0 0 0 0
0 0 0 0 0 0 0 0
4 0 0 0 0 Cell[i, j] Cell[i, j] 0 0 0 0
(levels)
3 0 0 0 0 0 0 0 0
j 2 0 0 0 0 1 1 0 0
Cell[i, j-1]
1 1 1 0 0 1 1 1 0
1 2 3 4
i (rods)
Fig. 10
The circuit attached to each and every cell cell[i, j] enforces the following logic: Set
cell[i, j] (make cell[i, j] = 1) if (a) it is currently OFF, i.e. cell[i, j] = 0 and (b) the cell
in the data–entry register representing the ith rod is ON, i.e. Bead[i] = 1 (there is a
bead up ready to ‘drop down’ along ith rod) and (c) its immediate lower neighbour cell
(cell in the next lower level) is ON, i.e. cell[i, j − 1] = 1 (which means the bead falling
down can not go any further). Note that cell[i, 0] is assumed to be 1. Thus, after the
last data is entered, the frame cell[i, j] will have the whole set of data in a sorted form.
The time complexity of sorting using the above implementation is due to two com-
ponents: data entry time and the actual sorting time (time taken for the cells to switch
states). In this implementation also, data entry and sorting alternate each other; sorting
does not wait till all data are entered. Let t1 be the average time taken for entering a
single data item and t2 the average time taken for the cells to switch states. It takes
t1 + t2 units for every single data item in the set. The overall complexity is therefore
n × (t1 + t2 ), i.e. O(n).
The observation made at the end of section 3 is equally valid here.
6 Conclusions
A way to design a natural algorithm is to discover an analogous natural property in
a physical system, and then to compute by “hitching a ride” on that inherent natural
property. In our case, “beads fall down in the sorted order”. Bead–Sort, the emergent
sorting algorithm, is simple, parallel and low cost. Some possible implementations of
Bead–Sort have been discussed. The analog implementation is the most natural imple-
mentation; electrical charges behave in the same manner as beads react to gravity. The
cellular automaton implementation is simple and very easy to understand. The digital
design is easy to implement and is cost effective.
References
[1] J. J. Arulanandham. The Bead–Sort. Animation, www.geocities.com/natu-
ral algorithms/josh/beadsort.ppt.
9
[2] C. S. Calude, G. Păun, Monica Tătărâm. A glimpse into natural computing,
J. Multi Valued Logic 7 (2001), 1–28.
[3] E. W. Dijkstra. The end of computing science? Comm. ACM 44, 3 (2001), 92.
[4] A. J. Jeyasooriyan. Ball Sort — A natural algorithm for sorting, Sysreader (1995),
13–16.
[6] G. Rozenberg. The natural computing column, Bulletin of EATCS 66 (1998), 99.
[7] G. Rozenberg. The nature of computation and the computation in nature, Inter-
national Colloquium on Graph Transformation and DNA Computing, Technical
University of Berlin, 14 February 2002.
[8] S. Wolfram. Theory and Applications of Cellular Automata, World Scientific Pub-
lishers, Singapore, 1986.
10