# 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.

- 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:**

`// C++ program to implement gravity/bead sort ` `#include <bits/stdc++.h> ` `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[0]; ` ` ` `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[0]); ` ` ` ` ` `beadSort(a, len); ` ` ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `printf` `(` `"%d "` `, a[i]); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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.

**O(1)**: Dropping all beads together as a single (simultaneous) operation. This complexity cannot be implemented in practice.): 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(****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(), 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:**

- 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

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Sorting Algorithm Visualization : Merge Sort
- A sorting algorithm that slightly improves on selection sort
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting objects using In-Place sorting algorithm
- Sorting | Natural Language Programming
- Print a case where the given sorting algorithm fails
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Strings using Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Sorting Algorithms Visualization : Bubble Sort
- C++ program for Sorting Dates using Selection Sort
- Which sorting algorithm makes minimum number of memory writes?
- Sleep Sort – The King of Laziness / Sorting while Sleeping
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)