DAA: Bead Sort Algorithm

Bead Sort Algorithm

The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things under gravity.

Let us understand it with the help of the below illustration:

Beads are represented here as numbers in the given image.

Bead Sort Algorithm in DAA

The algorithm is to settle down all the beads one by one. So in this way the maximum number of beads will be at bottom and the lesser numbers following up in the same vertical level. Likewise, the smallest element will be at the top and the largest element will be at bottom, hence, array is sorted.

       Let us look at how we will sort (3, 4, 1, 2) using bead sort -

Bead Sort Algorithm in DAA

C++ code:

 #include <vector>
 #include <iostream>
 #include <bits/stdc++.h>
 using namespace std;
 // BeadSort algorithm function
 void beadSort(vector<int>& arr)
 {
     // find maximum element in vector
     auto max = *std::max_element(std::begin(arr), std::end(arr));
     // declare beads vector for sorting
     vector<unsigned char> beads(max * arr.size(), 0);
     // initialize the beads accordingly
     for (auto i = 0; i < arr.size(); i++)
         for (auto j = 0; j < arr[i]; j++)
             beads[i * max + j] = 1;
     // Use gravity to let beads fall
     for (auto j = 0; j < max; j++) {
         int sum = 0;
         // assign beads for each post
         for (auto i = 0; i < arr.size(); i++) {
             sum += beads[i * max + j];
             beads[i * max + j] = 0;
         }
         // Use gravity to bring beads down
         for (auto i = arr.size() - sum; i < arr.size(); i++)
             beads[i * max + j] = 1;
     }
     // put sorted beads back into vector
     for (auto i = 0; i < arr.size(); i++) {
         for (auto j = 0; j < max && beads[i * max + j]; ++j) // Inner loop
             arr[i] = j + 1;
     }
 }
 int main()
 {
     // initialize vector
     vector<int> arr = { 23, 2, 12, 54, 90, 102, 32 };
     int n = sizeof(arr) / sizeof(arr[0]);
     // reserve static vector for optimal performance gain
     arr.reserve(n);
     // Print vector before sorting
     cout << "Before sorting" << endl;
     for (auto const& i : arr)
         cout << i << " ";
     cout << endl;
     beadSort(arr);
     // Print vector after sorting
     cout << "After sorting" << endl;
     for (auto const& i : arr)
         cout << i << " ";
     cout << endl;
     // Free memory space of vector
     arr.shrink_to_fit();
 } 

C code:

 #include <stdio.h>
 #include <stdlib.h>
 #define BEAD(i, j) beads[i * max + j]
 void bead_sort(int* a, int len)
 {
     int i, j, max, sum;
     unsigned char* beads;
     for (i = 1, max = a[0]; i < len; i++)
         if (a[i] > max)
             max = a[i];
     beads = calloc(1, max * len);
     /* mark the beads */
     for (i = 0; i < len; i++)
         for (j = 0; j < a[i]; j++)
             BEAD(i, j) = 1;
     for (j = 0; j < max; j++) {
         /* count how many beads are on each post */
         for (sum = i = 0; i < len; i++) {
             sum += BEAD(i, j);
             BEAD(i, j) = 0;
         }
         /* mark bottom sum beads */
         for (i = len - sum; i < len; i++)
             BEAD(i, j) = 1;
     }
     for (i = 0; i < len; i++) {
         for (j = 0; j < max && BEAD(i, j); j++)
             ;
         a[i] = j;
     }
     free(beads);
 }
 int main()
 {
     int i, x[] = { 23, 2, 12, 54, 90, 102, 32 };
     int len = sizeof(x) / sizeof(x[0]);
     printf("Before sorting: ");
     for(int i=0;i<len;i++)
     printf("%d ",x[i]);
     bead_sort(x, len);
     printf("\n");
     printf("After sorting: ");
     for (i = 0; i < len; i++)
         printf("%d ", x[i]);
     return 0;
 } 

Output:

 Before sorting: 23 2 12 54 90 102 32
 After sorting: 2 12 23 32 54 90 102 


Related Topics

DAA: Insertion Sort Algorithm on Singly Link List

Insertion Sort Algorithm on Singly Link List We will sort a singly link list using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The insertion sort technique works...

3 minutes read.

DAA: Application of DFS and BFS

Application of DFS and BFS Depth-first search and breadth-first searches are the most famous algorithms used in daily life and the programming world. Let us now explore each application in which...

3 minutes read.

DAA: Dynamic Programming

Dynamic Programming Introduction The technique of breaking a problem statement into subproblems and using the optimal result of subproblems as an optimal result of the problem statement is known as dynamic programming....

2 minutes read.

DAA: Bead Sort Algorithm

Bead Sort Algorithm The bead sort is also known as the gravity sort algorithm. The algorithm is based on the natural phenomena of gravity. The phenomenon is the falling of things...

3 minutes read.

Introduction to Sorting in DAA

DAA: What is Sorting? The technique in which a data structure is rearranged in decreasing order, increasing order, or in a specified order is called sorting. We apply to sort in our...

4 minutes read.

DAA: Binary Tree and its Categories

Binary Tree and its Categories The binary tree is a non-linear data structure in which there are 0 or utmost 2 nodes.  Each node has two children, i.e., left and right...

4 minutes read.

Segregate the given Linked List in DAA

Segregate Even and Odd Nodes in a Linked List A linked list is a linear data structure in which each node has two blocks. One contains the node’s value or data,...

3 minutes read.

DAA: Euclid Algorithm

Euclid Algorithm The Euclid algorithm finds the GCD of two numbers in the efficient time complexity. To find the GCD of two numbers, we take the two numbers’ common factors and multiply...

8 minutes read.

DAA: Bubble Sort Algorithm on Linked List

Bubble Sort Algorithm on Linked List In this article, we will sort a Link List using the bubble sort technique. Example: Input : 20->30->40->10 Output :10->20->30->40 Input : 20->4->3 Output : 3->4->20 Sorting Technique The bubble sort technique...

4 minutes read.

DAA: Depth-First Search Algorithm

Depth-first search: DFS is a traversing algorithm of a graph or tree in which one node is taken as arbitrary, and with the help of that arbitrary node, all its...

6 minutes read.

DAA: KMP Algorithm

KMP ALGORITHM The KMP algorithm is abbreviated as the "Knuth Morris Pratt” algorithm. This algorithm was developed by all of them.  This algorithm searches a pattern of length m in a string...

10 minutes read.

DAA: Bottom view of a Binary Tree

Bottom view of a Binary Tree The bottom view of a binary tree is the number of nodes visible when viewed from the bottom. At every horizontal distance, there would be...

3 minutes read.

DAA: Algorithm of Right View of a Binary Tree

Algorithm of Right View of a Binary Tree The right view of a binary tree is the visible nodes from the right side of the tree. In the given tree, the visible...

5 minutes read.

DAA: Floyd Cycle Detection

Floyd Cycle Detection Floyd Cycle algorithm is one of the cycle detection algorithms to detect the cycle in a given singly linked list. In the Floyd Cycle algorithm, we have two pointers...

4 minutes read.

Symmetric Trees in DAA

Symmetric Trees The trees that are mirror images of themselves are known as symmetric trees. Look at the following tree image below: The tree is symmetric as the left subtree is the mirror...

4 minutes read.

DAA: Density of a Binary Tree Algorithm

The Density of a Binary Tree Algorithm The density of a binary tree is defined as the ratio of the tree’s size to the tree’s height.  The height of the tree is...

2 minutes read.

DAA: Bubble Sort Algorithm

Bubble Sort Algorithm The bubble sort algorithm is also known as the sinking algorithm. In this algorithm, we iterate over the array, and it takes two adjacent elements and swaps them...

3 minutes read.

Recurrence relation in DAA

Recurrence relation in DAA The model that uses mathematical concepts to calculate the time complexity of an algorithm is known as the recurrence relational model. A recursive relation, T(n), is a recursive...

5 minutes read.

DAA: Construct a Tree from Inorder and Preorder Traversals

Construct a Tree from Inorder and Preorder Traversals We are given inorder and preorder traversals of a tree. We need to generate a tree from these traversals. Example: Inorder[]   = { 3, 1,...

4 minutes read.

DAA: Continuous Tree

Continuous Tree A continuous tree is the one in which the nodes from root to leaf path, the two adjacent node values, have a difference of 1. Input :          3                     /   \                   ...

5 minutes read.