-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1111 from ShrishtiSingh26/bingo_sort_algo
Bingo sort algorithm added
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |