Skip to content

Commit

Permalink
Merge pull request #1145 from shivenyadavs/main
Browse files Browse the repository at this point in the history
Bead Sort program
  • Loading branch information
pankaj-bind authored Oct 23, 2024
2 parents fbea5df + 48c6fee commit 1141445
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Sorting Algorithms/Bead Sort/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void bead_sort(int *arr, int n) {
int max = arr[0];

// Find the maximum value in the array
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}

// Create a bead grid with dimensions max (height) x n (width)
unsigned char beads[n][max];
memset(beads, 0, sizeof(beads)); // Initialize bead grid with zeros

// Mark the beads in the grid based on the values of the array
for (int i = 0; i < n; i++) {
for (int j = 0; j < arr[i]; j++) {
beads[i][j] = 1; // Place beads vertically
}
}

// Perform the bead sort by dropping the beads to the bottom of each column
for (int j = 0; j < max; j++) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += beads[i][j]; // Count how many beads are in each column
beads[i][j] = 0; // Clear the column
}

// Drop the beads down
for (int i = n - sum; i < n; i++) {
beads[i][j] = 1;
}
}

// Update the original array with the sorted values
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < max; j++) {
if (beads[i][j]) {
count++;
}
}
arr[i] = count;
}
}

int main() {
int arr[] = {5, 3, 1, 7, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

bead_sort(arr, n);

printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
35 changes: 35 additions & 0 deletions Sorting Algorithms/Bead Sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Bead Sort Algorithm

## Description

Bead Sort, also known as Gravity Sort, is a natural sorting algorithm that mimics the physical process of beads falling due to gravity. The algorithm represents each number in the array by beads stacked vertically and simulates how gravity would naturally cause them to align in sorted order. It works best when sorting integers and is particularly intuitive, resembling an abacus-style sorting method.

# Problem Definition
Given:

An array arr of n non-negative integers.
## Objective:

Sort the array in ascending order using the Bead Sort algorithm by simulating the natural alignment of beads under gravity.
## Algorithm Overview
Create a Bead Grid: Represent the array as a 2D grid where each row corresponds to an element of the array, and each column represents a bead.
Simulate Gravity: Let the beads "fall" vertically in each column, simulating the effect of gravity pulling the beads downward.
Count the Beads: Once the beads have fallen, count the beads in each row to reconstruct the sorted array.
End Sorting: After all beads have aligned, the elements of the array are sorted in ascending order.
Time Complexity
The time complexity of Bead Sort is determined by the sum of the values in the array. This leads to the following time complexity:

# Best Case: O(n) when the elements are already sorted and the values are small.
# Average Case: O(S), where S is the sum of the elements in the array.
# Worst Case: O(S), where S is large, making it inefficient for very large numbers.
## Advantages
Simple and intuitive algorithm, especially for small integers.
Can be parallelized in certain situations, allowing for better performance in specific hardware environments.
Requires no comparisons between elements, which makes it different from most comparison-based sorting algorithms.
## Disadvantages
Limited to sorting non-negative integers and may not work well with larger numbers due to its reliance on the sum of values.
Not practical for floating-point numbers or very large ranges of values.
Inefficient compared to more general sorting algorithms (e.g., Quick Sort, Merge Sort) for large datasets with wide ranges of values.
## Conclusion
Bead Sort is an interesting algorithm that mimics a physical sorting process and works well with small, non-negative integer arrays. Although it has its limitations, especially for large numbers or ranges, it provides an intuitive, gravity-based approach to sorting and can be applied in parallel computing environments where its simplicity may offer advantages.

0 comments on commit 1141445

Please sign in to comment.