bead-sort - a natural algorithm for sorting
DESCRIPTION
Bead-Sort - A natural algorithm for sorting. Beads sort themselves out. Bead-Stack machine The “tilt” operation. 90 0. Bead-Sort: illustration 1. 1. 2. 3. Sorting {3, 1, 2}. 1. Drop 3 beads. (Remember, always from left-to-right). 2. Drop 1 bead. 3. Drop 2 beads. - PowerPoint PPT PresentationTRANSCRIPT
Bead-Sort- A natural algorithm for sorting
Beads sort themselves out
900
Bead-Stack machineThe “tilt” operation
Sorting {3, 1, 2}
2. Drop 1 bead
3. Drop 2 beads
1. Drop 3 beads(Remember, always from left-to-right)
12
3
Bead-Sort: illustration 1
Sorting {2, 3, 1}
2. Drop 3 beads
3. Drop 1 beads
1. Drop 2 beads(Remember, always from left-to-right)
1
2
3
Bead-Sort: illustration 2
Sorting {1, 3, 2}
2. Drop 3 beads
3. Drop 2 beads
1. Drop 1 bead(Remember, always from left-to-right)
1
2
3
Bead-Sort: illustration 3
Sorting {1, 3, 2, 4}
2. Drop 3 beads
3. Drop 2 beads
1. Drop 1 bead(Remember, always from left-to-right)
4. Drop 4 beads
3
4
1
2
Bead-Sort: illustration 4
Sorting {2, 4, 3, 2}
2. Drop 4 beads
3. Drop 3 beads
1. Drop 2 beads(Remember, always from left-to-right)
4. Drop 2 beads
4
2
2
3
Bead-Sort: illustration 5
Simulating Bead-Sort with a program
• Two linear arrays used.
• Rod_Count keeps track of number of beads in each rod.
• Level_Count records number of beads in each level.
• Finally, Level_Count will contain sorted data.
1
3
0
0
Lev
el_C
ount
Rod_Count
2 1 1
Bead-Sort: Proof of correctnessMathematical induction on number of rows of beadsClaims:
(i) Rows of beads represent the same set of positive integers before and after dropping down.
(ii) After dropping, each row has beads less than (or equal to) that on the row directly below it.
1 row (k = 1)
2 rows (k = 2)
(i)
(ii)
(iii)
x
x + i x i
xx + i
x
i
Bead-Sort: Proof of correctness
k+1th row
k rows
the smallest amongst k rows
the smallest amongst k+1 rows has already emerged on top
Parallel implementation of Bead-SortUsing a digital circuit
flip-flops
presence of bead - 1
absence of bead - 0
c1
c2
cn
1 1 1
1 1
1 1 1
0
0
0
0 0
0
0
0
0
0
00
c1
c2
cn
1 1 1
1 1
1 1
1
0
0
0
0 0
0
0
0
0
00
0Data entry register 1 1 1
0ci
t
ci-1
Cit+1
Input from corresponding cell in data entry register
Calculating currentResistor chain 1:I1 = V1/R = 3/(1+2+3) = 1/2 AResistor chain 2:I2 = V2/R = 2/(1+2+3) = 1/3 AResistor chain 3:I3 = V3/R = 1/(1+2+3) = 1/6 A
1 1 0 (no.2)
1 0 0 (no. 1)
1 1 1 (no. 3)
3 2 1
‘Trim’ Voltage :
Trim( v ) =
1 if v >= 0.5
0 otherwise
V2V2V1V1 V3V3
Trim
Trim
Trim
Trim
Trim
Trim
Trim
Trim
Trim
vv11
vv22
vv33
1
2
3
1
2
3
1
2
3
Increase voltage by 1 unit every time a ‘1’ is sent
1 1 0
(strings of 1’s similar to balls)DATA ENTRY
1 1 01 1 02 1 02 1 03 2 13 2 1
1/2 A 1/3 A 1/6 A
0.5 V
1 V
1.5 V
0.3 V
0.7 V
1 V
0.2 V
0.3 V
0.5 V
1 V
2 V
3 V
sorted data
Parallel implementation Using an analog circuit
000
initial configuration
{2,1,3}
1 1 1
1
11
0 0
0
final configuration
{1,2,3}
000
1
1
1
1
11
0
00
0
0 0
0
1
0 1
0
0
0 0
1
1
0 1
1
0
1 0
0
1
1 0
0
0
1 1
1
1
1 1
1
1 2 3 4 5 6 7 8
CA rules for Bead-Sort simulation
Parallel simulationUsing a cellular automaton (CA)
Bead-Stack machineLoading the input (beads)
1 2 3 4
Pushing button-1 will make one bead to drop down, pushing button-2 will make two beads to drop down (in parallel), and so on.
900
Bead-Stack machineThe “tilt” operation
Bead-Stack machineReading the output
only label ‘3’ is seen
11 22 33
11 22 33 44
11 22
11
labels stuck on the sides (of the beads)
Beads rolled down along rod-1 will have the label “1”, those rolled down along rod-2 will have “2”, and so on.
Bead-Stack machineComplexity of inputting, sorting and outputting a listInputting a list: Using the gadget that loads a row-of-beads, we need N (loading) operations to input a list of size N (into the frame of the Bead-Stack machine).
Sorting a list: We tilt the frame 90o (a single operation, from the Bead-Stack machine’s perspective). The time taken by the falling beads to settle down is sqrt(2h/g), where h is the height of the rods and g, the acceleration due to gravity. If we fix the height of rods to be the same as the size (N) of the list, then the time taken is given by sqrt(2N/g), i.e. O(sqrt(N)).
Outputting a list: Nothing needs to be done to explicitly output rows of beads; the user could (literally) read the output (row by row).