Also known as Gravity sort, this algorithm was inspired by natural phenomena and was designed keeping in mind objects(or beads) falling under the influence of gravity. The Idea: Positive numbers are represented by a set of beads like those on an abacus.
- Like in the image above the beads represent the following numbers from top to bottom : 7, 2, 1, 4, 2. Now, imagine that this is the position of the beads at time t = 0 and with every passing second the beads will fall down by one level provided there is no bead already present below them. In such a case, they just rest upon the bead below them.(Rods are numbered from left to right and levels are numbered from the bottom as 1, 2, ………. n.)
- Now, at time t = 1 the bottom 2 beads in the first two rods from the left stay at their positions while the second bead from the top from the second rod comes down by one level to rest upon the bead below it. The beads in the 3rd and 4th rod at level 2 come down to level 1. Simultaneously, the beads in the rods 3 to 7 come down by one level. Now, the numbers from top to bottom become: 2, 6, 2, 2, 4.
- This goes on till time t = 4 where we get the sorted sequence of numbers from top to bottom which is: 1, 2, 2, 4, 7.
Why is it called so?
When one tries to visualize this algorithm it appears as if beads are falling down under the influence of gravity to the bottom-most level they can reach resulting the set of beads being arranged in a descending order from the ground up. If you are having trouble visualizing this visit this link Lets say that we have to sort the numbers 3, 4, 1, 2. The above algorithm would work like this.
Below is the code, try to implement it yourself before looking at the code. The Code:
#include <iostream> #include <algorithm> #include <vector> std::vector< int > beadSort(std::vector< int > a, int len)
{ // Find the maximum element
int max = a[0];
for ( int i = 1; i < len; i++) {
if (a[i] > max) {
max = a[i];
}
}
// allocating memory
std::vector<std::vector< int >> beads;
beads.resize(len);
for ( int i = 0; i < len; i++) {
beads[i].resize(max);
std::fill(beads[i].begin(), beads[i].end(), 0);
}
// mark the beads
for ( int i = 0; i < len; i++) {
for ( int j = 0; j < a[i]; j++) {
beads[i][j] = 1;
}
}
// move down the beads
for ( int j = 0; j < max; j++) {
int sum = 0;
for ( int i = 0; i < len; i++) {
sum += beads[i][j];
beads[i][j] = 0;
}
for ( int i = len-1; i >= len-sum; i--) {
beads[i][j] = 1;
}
}
// to get sorted array
std::vector< int > result;
result.resize(len);
for ( int i = 0; i < len; i++) {
int sum = 0;
for ( int j = 0; j < max; j++) {
sum += beads[i][j];
}
result[i] = sum;
}
return result;
} int main(){
std::vector< int > a = {3, 1, 4, 1, 5};
std::vector< int > result = beadSort(a, 5);
std::cout << "The sorted array is : " ;
for ( int i = 0; i < result.size(); i++)
std::cout << result[i] << " " ;
return 0;
} // This code is contributed by ishankhandelwals. |
function beadSort(a, len) {
// Find the maximum element
let max = a[0];
for (let i = 1; i < len; i++) {
if (a[i] > max) {
max = a[i];
}
}
// allocating memory
const beads = new Array(len).fill().map(() => new Array(max).fill(0));
// mark the beads
for (let i = 0; i < len; i++) {
for (let j = 0; j < a[i]; j++) {
beads[i][j] = 1;
}
}
// move down the beads
for (let j = 0; j < max; j++) {
let sum = 0;
for (let i = 0; i < len; i++) {
sum += beads[i][j];
beads[i][j] = 0;
}
for (let i = len-1; i >= len-sum; i--) {
beads[i][j] = 1;
}
}
// to get sorted array
const result = new Array(len);
for (let i = 0; i < len; i++) {
let sum = 0;
for (let j = 0; j < max; j++) {
sum += beads[i][j];
}
result[i] = sum;
}
return result;
} const result = beadSort([3, 1, 4, 1, 5], 5); console.log(result); |
public class BeadSort {
public static void beadSort( int [] a)
{
// Find the maximum element
int max = a[ 0 ];
for ( int i = 1 ; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
// allocating memory
int [][] beads = new int [a.length][max];
// mark the beads
for ( int i = 0 ; i < a.length; i++) {
for ( int j = 0 ; j < a[i]; j++) {
beads[i][j] = 1 ;
}
}
// move down the beads
for ( int j = 0 ; j < max; j++) {
int sum = 0 ;
for ( int i = 0 ; i < a.length; i++) {
sum += beads[i][j];
beads[i][j] = 0 ;
}
for ( int i = a.length - 1 ; i >= a.length - sum;
i--) {
a[i] = j + 1 ;
}
}
}
public static void main(String[] args)
{
int [] a = { 4 , 2 , 6 , 1 , 8 };
beadSort(a);
for ( int i : a) {
System.out.print(i + " " );
}
}
} |
public class BeadSort
{ public static void beadSort( int [] a)
{
// Find the maximum element
int max = a[0];
for ( int i = 1; i < a.Length; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
// allocating memory
int [,] beads = new int [a.Length, max];
// mark the beads
for ( int i = 0; i < a.Length; i++)
{
for ( int j = 0; j < a[i]; j++)
{
beads[i, j] = 1;
}
}
// move down the beads
for ( int j = 0; j < max; j++)
{
int sum = 0;
for ( int i = 0; i < a.Length; i++)
{
sum += beads[i, j];
beads[i, j] = 0;
}
for ( int i = a.Length - 1; i >= a.Length - sum;
i--)
{
a[i] = j + 1;
}
}
}
public static void Main( string [] args)
{
int [] a = {3, 1, 4, 1, 5};
beadSort(a);
for ( int i=0; i<a.Length;i++)
{
System.Console.Write(a[i] + " " );
}
}
} |
def bead_sort(a, length):
# Find the maximum element
maximum = a[ 0 ]
for i in range ( 1 , length):
if a[i] > maximum:
maximum = a[i]
# allocating memory
beads = [[ 0 for i in range (maximum)] for j in range (length)]
# mark the beads
for i in range (length):
for j in range (a[i]):
beads[i][j] = 1
# move down the beads
for j in range (maximum):
sum = 0
for i in range (length):
sum + = beads[i][j]
beads[i][j] = 0
for i in range (length - 1 , length - sum - 1 , - 1 ):
beads[i][j] = 1
# to get sorted array
result = [ 0 ] * length
for i in range (length):
sum = 0
for j in range (maximum):
sum + = beads[i][j]
result[i] = sum
return result
print ( "The sorted array is : " , end = "")
result = bead_sort([ 3 , 1 , 4 , 1 , 5 ], 5 )
for i in range ( 0 , len (result)):
print (result[i], end = " " )
|
Output
The sorted array is : 1 1 3 4 5
Time Complexity: 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.
- O(1) : Dropping all beads together as a single (simultaneous) operation. This complexity 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) : Dropping the row of beads in the frame (representing a number) as a distinct operation since the number of rows is equal to n.
- O(S) : Dropping each and every bead’ as a separate operation since S is the sum of all the beads.
Like the Pigeonhole sort, bead sort is unusual in that in worst case it can perform faster than O(
- https://www.wikiwand.com/en/Bead_sort
- https://kukuruku.co/post/bead-sort/
- https://rosettacode.org/wiki/Sorting_algorithms/Bead_sort
- https://www.cs.auckland.ac.nz/~mjd/misc/BeadSort5.pdf