Skip to content

Commit

Permalink
Merge pull request #1111 from ShrishtiSingh26/bingo_sort_algo
Browse files Browse the repository at this point in the history
Bingo sort algorithm added
  • Loading branch information
pankaj-bind authored Oct 23, 2024
2 parents c30fae9 + 071253c commit 22e0f5d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Sorting Algorithms/Bingo sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Bingo Sort Algorithm

## Description

Bingo Sort is a comparison-based sorting algorithm that works by repeatedly identifying the largest element and placing it in its correct position. It is an adaptive algorithm in that it takes advantage of duplicate elements in the array, sorting them all in one pass. This makes it efficient when dealing with datasets that have many repeated values.

The algorithm repeatedly selects the largest value from the remaining unsorted part of the array, then finds all occurrences of that value and places them in their correct positions. This process continues until the entire array is sorted.

### Problem Definition
Given:
- An array arr of n elements.

Objective:
- Sort the array in ascending order using the Bingo Sort algorithm.

### Algorithm Overview

1. **Identify the Largest Element:**
The algorithm starts by identifying the largest element in the array.

2. **Move Largest Elements:**
Once the largest element is identified, all occurrences of that element are moved to the end of the array (where they belong in a sorted array).

3. **Repeat for Remaining Elements:**
The process is repeated for the next largest element, excluding the already sorted portion of the array.

4. **End Sorting:**
The algorithm completes when all elements have been placed in their correct positions.

### Time Complexity

The time complexity of Bingo Sort is as follows:

**Best Case:** O(n) when all elements in the array are the same, as the algorithm will only need one pass.
**Average Case:** O(n^2) due to the repeated searches for the largest element.
**Worst Case:** O(n^2), since each pass involves scanning the entire unsorted portion of the array.

### Advantages

- **Efficient for Datasets with Many Duplicates:** Bingo Sort performs well when the array contains many duplicate values, as it can move all duplicates to their correct positions in one pass.

- **Easy to Implement:** The algorithm is simple to understand and implement due to its iterative structure.

###Disadvantages
- **Inefficient for Large Datasets:**
The algorithm has a quadratic time complexity in the average and worst cases, making it less efficient than more advanced algorithms like Quick Sort or Merge Sort.

- **Sequential Nature:**
Bingo Sort is not well-suited for parallel processing environments due to its reliance on iterating through the array multiple times.

### Conclusion
Bingo Sort is a simple yet effective sorting algorithm for arrays with many repeated values. However, its O(n^2) complexity makes it less efficient for large or unique datasets compared to more optimized algorithms. While it is easy to implement and understand, it is generally not the best choice for most sorting applications, especially in parallel computing contexts.

58 changes: 58 additions & 0 deletions Sorting Algorithms/Bingo sort/bingo_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>

// Function to perform Bingo Sort
void bingoSort(int arr[], int n) {
int largest, next_largest, last_index;

// Find the largest element in the array
largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
}

// Start sorting the array
next_largest = largest;
last_index = n - 1;

while (last_index > 0) {
int new_largest = -1;

// Traverse the array and place the largest elements at their correct position
for (int i = 0; i <= last_index; i++) {
if (arr[i] == next_largest) {
int temp = arr[i];
arr[i] = arr[last_index];
arr[last_index] = temp;
last_index--;
} else if (arr[i] > new_largest) {
new_largest = arr[i];
}
}

next_largest = new_largest; // Update the next largest element
}
}

// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {35, 20, 15, 50, 45, 30, 25};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");
printArray(arr, n);

bingoSort(arr, n);

printf("Sorted array:\n");
printArray(arr, n);

return 0;
}

0 comments on commit 22e0f5d

Please sign in to comment.