From 48c6feef1ab721341b020b36d504c52895811b48 Mon Sep 17 00:00:00 2001 From: shiven Date: Tue, 22 Oct 2024 19:19:21 +0530 Subject: [PATCH] Bead Sort program --- Sorting Algorithms/Bead Sort/Program.c | 70 ++++++++++++++++++++++++++ Sorting Algorithms/Bead Sort/README.md | 35 +++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Sorting Algorithms/Bead Sort/Program.c create mode 100644 Sorting Algorithms/Bead Sort/README.md diff --git a/Sorting Algorithms/Bead Sort/Program.c b/Sorting Algorithms/Bead Sort/Program.c new file mode 100644 index 00000000..ad9c5a27 --- /dev/null +++ b/Sorting Algorithms/Bead Sort/Program.c @@ -0,0 +1,70 @@ +#include +#include +#include + +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; +} diff --git a/Sorting Algorithms/Bead Sort/README.md b/Sorting Algorithms/Bead Sort/README.md new file mode 100644 index 00000000..1621ec75 --- /dev/null +++ b/Sorting Algorithms/Bead Sort/README.md @@ -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. +