Content uploaded by Michael J. Dinneen

Author content

All content in this area was uploaded by Michael J. Dinneen

Content may be subject to copyright.

In The Bulletin of the European Association for Theoretical Computer Science 76 (2002), 153-162.

Bead–Sort: A Natural Sorting Algorithm

Joshua J. Arulanandham, Cristian S. Calude, Michael J. Dinneen

Department of Computer Science

University of Auckland

Auckland, New Zealand

Email: hi josh@hotmail.com,{cristian,mjd}@cs.auckland.ac.nz

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)(Sis the sum of the input integers)

depending on the user’s perspective. Finally, three possible implementations are

suggested.

1BeadsandRods

According to Dijkstra [3], “our systems are much more complicated than can be con-

sidered healthy, and are too messy and chaotic to be used in comfort and conﬁdence.

. . . unmastered complexity is at the root of the misery”. One way to cope with this situa-

tion is to “go back to Nature” for resources and inspiration (see, for example, [4, 5, 2]), a

trend strongly advocated by Rozenberg (see his ideas in the inaugural column “Natural

Computing”, [6]; a recent account was presented in [7]).

In this note, a simple natural phenomenon is used to design a sorting algorithm for

positive integers, called here Bead–Sort. In what follows, we represent positive integers

by a set of beads (like those used in an Abacus) as illustrated below in Fig. 1.

beads

number 3 number 2

Fig. 1

Beads slide through rods asshowninFig.2.

4

3

34

2

3

4

2

(a) (b) (c) (d)

Fig. 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 diﬀerent 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.

..

1

2

3

n

...

.

.

.

Rods

.

Levels

123 m

Fig. 3

2 Algorithm and Complexity

In this section, we present the Bead–Sort algorithm and analyze its complexity in three

diﬀerent ways.

2

Consider a set Aof npositive integers to be sorted and assume the biggest number

in Ais m. Then, the frame should have at least mrods and nlevels. The Bead–Sort

algorithm is the following:

The Bead–Sort Algorithm

For all a∈Adrop abeads (one bead per rod) along the rods, starting

from the 1st rod to the ath rod. Finally, the beads, seen level by level,

from the nth level to the first level, represent Ain ascending order.

The Algorithm Bead–Sort is correct. We prove the result by mathematical induction

on the number of rows of beads. We claim (*) that the set of positive integers represented

by the states of the frame before and after the beads are dropped is the same. Also we

claim (**) that the number of beads on each row i, after dropping, is at most the number

of beads on row i−1 (the row directly below it).

Consider a set of cardinality n= 1. Since there is no possibility for a bead to drop

the above two claims hold. Now assume that these two properties hold for an input set

whose cardinality is k. Suppose a row k+1 of m<mbeads is now dropped on top

of these krows. Since claim (**) holds there is an index j≤msuch that all m−j

beads in columns greater then jdrop down (from row k+1). If m−j= 0, we are done.

Otherwise, the values of row k+1 and row khave been swapped, with any excess beads

on row kready to drop further. Thus, claim (*) holds, after a series of at most kswaps1.

Claim (**) also holds since we repeat these swaps until the force of gravity cannot pull

down any more beads.

The time complexity of Bead–Sort can be evaluated at three diﬀerent coarseness

of discretization: complexity (1) treats ‘dropping all beads together’ as a single (si-

multaneous) operation, complexity (2) treats ‘dropping the row of beads in the frame

(representing a number)’ as a distinct operation and complexity (3) treats ‘dropping each

and every bead’ as a separate operation.

According to the ﬁrst measure of complexity, the ‘dropping’ works in one unit of time

and hence the complexity is O(1). For the second measure, there are ndistinct rows of

input beads in the frame and therefore the complexity is O(n). For the third measure,

the complexity is equal to the total number of beads dropped. Hence, the complexity is

O(S), where Sis the sum of the input integers.

Note that in each of the above cases, the number of levels each bead drops is not

taken into account. Some implementations might require that this factor is taken into

account, since a bead dropping down from a higher level could cost more.

3 An Analog Hardware Implementation

The presence or absence of a bead is represented by an analog voltage across electrical

resistors. A rod is represented by a series of electrical resistors of increasing value from

top to bottom (see Fig. 4).

1Note 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

.

.

.

.

.

..

.

.

Rod m

.. .

V1 V2

R 1,n

R 1,1

R 1,2

R 2,n

R 2,2

R 2,1

Vm

R m,2

R m,n

R m,1

Rod 1 Rod 2

Level 1

Level 2

Level n

Fig. 4

If the voltage across a resistor is above a preset threshold–voltage t, then it indicates

the ‘presence of bead’. The values of resistances in series (representing a rod) are ar-

ranged in increasing order so that when a voltage is applied across them, there is more

‘concentration’ of charge (voltage–drop) in the bottom resistors than in the top ones; this

is the electrical equivalent of the eﬀect of gravity (that attracts beads downward). Let

the voltages across the rods be V1,V2,...,Vm, respectively. Let the currents through the

rods be C1,C2,...,Cm, respectively. Each resistor is denoted by Ri,j where iis the rod

number and jthe level number; xi,j represents the voltage–drop across Ri,j . The resistors

lying on the same level (say, j) have the same value, i.e. R1,j =R2,j =... =Rm,j .

.

.

.

R 1,n

R 1,2

R 1,1

.

.

... .

.. .

.

.

.

R 2,1

R 2,2

R 2,n

Data Entry Device

Trim

Trim

Trim

Trim

Trim

Trim

R m,n

R m,1

Trim

Trim

Trim

R m,2

vn

v1

v2

Fig. 5

4

The voltage across each resistor is fed into a trimmer circuit (threshold unit) whose

output is given by x

i,j =1,ifxi,j ≥t,x

i,j = 0, otherwise. This is shown in Fig. 5, where

dotted lines/dashes do not indicate physical connections. Let v1,v2, ...,vndenote the

sums of individual voltages across resistors (after thresholding) in the 1st,2

nd, ...,nth

levels, respectively (note that in Fig. 5, the detailed circuitry for adding voltages is not

shown). Hence we have:

v1=x

1,1+x

2,1+...+x

m,1,

v2=x

1,2+x

2,2+...+x

m,2,

.

.

.

vn=x

1,n +x

2,n +...+x

m,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 ﬁrst 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 diﬀerent

voltage level; and these voltage levels should reﬂect 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 tshould be designed accordingly. After the whole data has

been entered, the voltage vector v1,v2,...,vnwill 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

3

2

1

3

2

1

3

2

v3

v1

v2

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 Ris 1Ω + 2Ω + 3Ω = 6Ω.

The threshold voltage tis 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 ﬂow of current and the values of current are shown in amperes.

The ﬁgures written close to the individual resistors represent the voltage levels across

them (xi,j ); those within the brackets represent the voltage after thresholding (x

i,j ). The

corresponding state of the frame is shown together. Note that after the last integer has

been entered, v1,v2and v3represent the sorted list.

1 v

0.5

0.3

0.2

(0)

(0)

(1)

1 v

0.5

0.3

0.2

(0)

(0)

(1)

0 v

(0)

0.0

0.0

0.0

(0)

(0)

3 v 2 v 1 v

1.5

1.0

0.5

(1)

(1)

(1) 1.0

0.7

0.3

(0)

(1)

(1) 0.5

0.3

0.2

(0)

(0)

(1)

0.5

0.3

0.2

(0)

(0)

(1)

0 v

0.0

0.0

0.0

(0)

(0)

(0)

2 v

1.0

0.7

0.3

(0)

(1)

(1)

1 v

0

(b)

(a)

(c)

0

1

3

1

2

1

6

1

3

1

6

1

6

1

6

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 t1be the average time

taken for entering a single data item and t2be the average charge settling–time. It takes

t1+t2units 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, t2will be so small compared to t1such that the

actual sorting time (before the next data is entered) is negligible.

4 A Cellular Automaton Implementation

A cellular automaton (CA) is a suitable choice for simulating natural physical systems

because it is massively parallel, self-organizing and is driven by a set of simple, local

rules (see, for example [8]).

In its simplest form, a CA can be considered a homogeneous array of cells in one,

two or more dimensions. Each cell has a ﬁnite discrete state. Cells communicate with

a number of local neighbors and update synchronously according to deterministic rules.

A cell updates its state depending on its current state and its neighbor states.

When modeling physical systems using cellular automata, space is treated as having

ﬁnitely many locations per unit of volume. Each location is represented by a cell and a

state is associated with each cell.

To emulate Bead–Sort, we use a two dimensional CA as shown in Fig. 8.

0

0

0

0

0

0

0

000

0

0

0

0

0

0

00

0

0

0

000

1

1

1

1

1

1

1

1

1

1111

1

11

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 conﬁguration can be

obtained by any further application. The snap-shots of the CA conﬁgurations that evolve

while sorting the data-set {1, 3, 2, 1}are shown in Fig. 8.

The number of sequential updates of CA conﬁguration necessary for sorting a partic-

ular set of data would depend on the ‘degree of disorder’ (entropy) associated with the

initial conﬁguration (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 conﬁguration (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 xof 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

5 A Digital Hardware Implementation

We now discuss a digital implementation: the presence and absence of a bead is repre-

sented by the states ‘1’ and ‘0’ of a cell (ﬂip–ﬂop). A rod is represented by an array

of cells. Fig. 9 shows how the frame can be described with a two–dimensional array of

cells cell[i, j ]; cell[i, j]=1,ifthereisabead in the ith rod at the jth level, cell[i, j ]=0,

otherwise.

1

2

3

4

j

i (rods)

(cells)

flip-flops

0

0

0

0

00 0

0

0

0

0

234

0

1

(levels)

1

1

1

1

1

1

1

1

1

1

11

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 ﬂip 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

0

3

0

0

00

4

0

1

0

1

0

0

0

00

0

0

0

0

0

0

0

0

0

00

0

0

0

0

0

00

0

0

0

00

0

0

2

0

j

0

0

0

0

(levels)

i (rods)

Data Entry Register

4

3

Bead[i]

Cell[i, j-1]

Cell[i, j] Cell[i, j]

2

1 1

1

1

1

1

1

1

11

Fig. 10

The circuit attached to each and every cell cell[i, j] enforces the following logic: Set

cell[i, j ](makecell[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 t1be the average time taken for entering a

single data item and t2the average time taken for the cells to switch states. It takes

t1+t2units 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 eﬀective.

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˘aun, Monica T˘at˘arˆam. 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.

[5] A. J. Jeyasooriyan, R. Soodamani. Natural algorithms — A new paradigm for

algorithm development, In Proc. 2nd International Conference on Information,

Communications and Signal Processing, CD-Rom, ICICS ’99, 1999.

[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 Scientiﬁc Pub-

lishers, Singapore, 1986.

10