# Bead Sort | A Natural Sorting Algorithm

Also known as Gravity sort, this algorithm was inspired from natural phenomenons 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. Sorting of {7, 2, 1, 4, 2} using Bead Sort. Beads fall down one by one if there is space below.

1. 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.)
2. 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.
3. 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:

 `// C++ program to implement gravity/bead sort ` `#include ` `using` `namespace` `std; ` ` `  `#define BEAD(i, j) beads[i * max + j] ` ` `  `// function to perform the above algorithm ` `void` `beadSort(``int` `*a, ``int` `len) ` `{ ` `    ``// Find the maximum element ` `    ``int` `max = a; ` `    ``for` `(``int` `i = 1; i < len; i++) ` `        ``if` `(a[i] > max) ` `           ``max = a[i]; ` ` `  `    ``// allocating memory ` `    ``unsigned ``char` `beads[max*len]; ` `    ``memset``(beads, 0, ``sizeof``(beads)); ` ` `  `    ``// mark the beads ` `    ``for` `(``int` `i = 0; i < len; i++) ` `        ``for` `(``int` `j = 0; j < a[i]; j++) ` `            ``BEAD(i, j) = 1; ` ` `  `    ``for` `(``int` `j = 0; j < max; j++) ` `    ``{ ` `        ``// count how many beads are on each post ` `        ``int` `sum = 0; ` `        ``for` `(``int` `i=0; i < len; i++) ` `        ``{ ` `            ``sum += BEAD(i, j); ` `            ``BEAD(i, j) = 0; ` `        ``} ` ` `  `        ``// Move beads down ` `        ``for` `(``int` `i = len - sum; i < len; i++) ` `            ``BEAD(i, j) = 1; ` `    ``} ` ` `  `    ``// Put sorted values in array using beads ` `    ``for` `(``int` `i = 0; i < len; i++) ` `    ``{ ` `        ``int` `j; ` `        ``for` `(j = 0; j < max && BEAD(i, j); j++); ` ` `  `        ``a[i] = j; ` `    ``} ` `} ` ` `  `// driver function to test the algorithm ` `int` `main() ` `{ ` `    ``int` `a[] = {5, 3, 1, 7, 4, 1, 1, 20}; ` `    ``int` `len = ``sizeof``(a)/``sizeof``(a); ` ` `  `    ``beadSort(a, len); ` ` `  `    ``for` `(``int` `i = 0; i < len; i++) ` `        ``printf``(``"%d "``, a[i]); ` ` `  `    ``return` `0; ` `} `

Output:

```1 1 1 3 4 5 7 20
```

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.

1. O(1) : Dropping all beads together as a single (simultaneous) operation. This complexity cannot be implemented in practice.
2. 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.
3. 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.
4. 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( ), the fastest performance possible for a comparison sort in worst case. This is possible because the key for a bead sort is always a positive integer and bead sort exploits its structure.

Space Complexity: Bead sort is the record-holder as for waste. The costs for the extra memory exceed the costs for storing the array itself. Its memory complexity is O( )

References:

This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.