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.
1 1 1 3 4 5 7 20
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(), 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()
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 email@example.com. 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.
- 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)