Open In App

Bingo Sort in Python

Last Updated : 19 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Bingo Sort algorithm is a variant of the Selection Sort. It works by first finding the smallest element, referred to as the Bingo Element, and then repeatedly iterating through the elements of the array to get them in their correct positions. This sorting technique is more efficient than Selection Sort when there are many duplicate values. In this article, we will discuss the bingo sort algorithm in python.

What is Bingo Sort?

This Sorting Technique is similar to the Selection Sort in which we first find the smallest element called the Bingo Element, and then we repeatedly iterate the elements of the array to get the correct positions of all the elements. Similarly, find the next bingo element for the next pass, and so on. Every distinct element is considered a Bingo Element and called out in increasing order. In this article, we will discuss the

Relation between Selection Sort and Bingo Sort:

Selection sort does one pass through the remaining items for each item moved. Bingo sort does one pass for each distinct value (not an item) and moves every item with that value to its final location.

When to use the Bingo Sort among all Sorting Techniques?

You should use the bingo sort if you know that the repetition of every element is large in the array. In that case, you can use this for better time complexity.

Step-by-step algorithm:

  • Find the smallest and the largest element from the array. And smallest element is known as the Bingo Element.
  • Create a global variable named start position that will keep track of the element position to be shifted to its correct position.
  • Run a while loop until the number of distinct Elements – 1.
    • Run a loop inside the while loop that will shift elements of the array to their correct position by checking the equality with bingo Element and find the next Bingo Element.
  • Finally Print your Array that has been sorted.

Below is the implementation of the above approach:

Python3
# Function to print the Array
def printArray(arr):
    print("Sorted Array: ",end="")
    for ele in arr:
        print(ele, end=" ")
    print()

# function for Sorting the Array
def bingoSort(arr, size):

    # Finding the smallest element From the Array
    bingo = min(arr)
    
    # Finding the largest element from the Array
    largest = max(arr)
    nextBingo = largest
    nextPos = 0
    while bingo < nextBingo:
    
        # Will keep the track of the element position to
        # shifted to their correct position
        startPos = nextPos
        for i in range(startPos, size):
            if arr[i] == bingo:
                arr[i], arr[nextPos] = arr[nextPos], arr[i]
                nextPos += 1
                
            # Here we are finding the next Bingo Element
            # for the next pass
            elif arr[i] < nextBingo:
                nextBingo = arr[i]
        bingo = nextBingo
        nextBingo = largest
    
    # Printing the ELements of the Sorted Array
    printArray(arr) 
        
arr = [ 5, 4, 8, 5, 4, 8, 5, 4, 4, 4 ]
bingoSort(arr, size = len(arr))

arr2 = [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
bingoSort(arr2, size = len(arr2)) 

arr3 = [ 0, 1, 0, 1, 0, 1 ]
bingoSort(arr3, size = len(arr3))

# This code is contributed by sdeadityasharma.

Output
Sorted Array: 4 4 4 4 4 5 5 5 8 8 
Sorted Array: 1 2 3 4 5 6 7 8 9 10 
Sorted Array: 0 0 0 1 1 1 

Time Complexity:

  • Average and Worst Case: O(M * N) where M = number of distinct elements and N = size of the array
  • Best Case: O(N + M2 )

Auxiliary Space: O(1)



Similar Reads

Bingo Sort Algorithm
What is Bingo Sort? This Sorting Technique is similar to the Selection Sort in which we first find the smallest element called Bingo Element, and then we repeatedly iterate the elements of the array to get the correct positions of all the elements. Similarly, find the next bingo element for the next pass, and so on. Every distinct element is consid
10 min read
Tag sort or Bucket sort or Bin sort in Python
Tag sort, also known as Bucket sort or Bin sort, is a non-comparison based sorting algorithm that distributes elements of an array into a number of "buckets", and then sorts each bucket individually. Tag sort or Bucket sort or Bin sort Algorithm:Determine Range:Find the maximum and minimum values in the input array to determine the range of tags ne
2 min read
Comparison among Bubble Sort, Selection Sort and Insertion Sort
Bubble Sort, Selection Sort, and Insertion Sort are simple sorting algorithms that are commonly used to sort small datasets or as building blocks for more complex sorting algorithms. Here's a comparison of the three algorithms: Bubble Sort:Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when the input array is already
15 min read
C/C++ Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Java Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Insertion sort to sort even and odd positioned elements in different orders
We are given an array. We need to sort the even positioned elements in the ascending order and the odd positioned elements in the descending order. We must apply insertion sort to sort them.Examples: Input : a[] = {7, 10, 11, 3, 6, 9, 2, 13, 0} Output : 11 3 7 9 6 10 2 13 0 Even positioned elements after sorting int ascending order : 3 9 10 13 Odd
7 min read
Quick Sort vs Merge Sort
Quick sort is an internal algorithm which is based on divide and conquer strategy. In this: The array of elements is divided into parts repeatedly until it is not possible to divide it further.It is also known as “partition exchange sort”.It uses a key element (pivot) for partitioning the elements.One left partition contains all those elements that
4 min read
Sort an array of pairs using Java Arrays.sort() with custom Comparator
Given an array of pairs of integers. The task is to sort the array with respect to the second element of the pair. Examples: Input: [(1, 2), (3, 5), (2, 6), (1, 7)]Output: [(1, 2), (3, 5), (2, 6), (1, 7)] Input: [(10, 20), (20, 30), (5, 6), (2, 5)]Output: [(2, 5), (5, 6), (10, 20), (20, 30)] Approach: Store the pairs in an array using a user-define
2 min read
Sort an Array which contain 1 to N values in O(N) using Cycle Sort
Prerequisite: Cycle SortGiven an array arr[] of elements from 1 to N, the task is to sort the given array in O(N) time.Examples: Input: arr[] = { 2, 1, 5, 4, 3} Output: 1 2 3 4 5 Explanation: Since arr[0] = 2 is not at correct position, then swap arr[0] with arr[arr[0] - 1] Now array becomes: arr[] = {1, 2, 5, 4, 3}Now arr[2] = 5 is not at correct
6 min read
Add elements in start to sort the array | Variation of Stalin Sort
Stalin sort (also 'dictator sort' and 'trump sort') is a nonsensical 'sorting' algorithm in which each element that is not in the correct order is simply eliminated from the list. This sorting algorithm is a less destructive variation of Stalin sort, that will actually sort the list: In this case, the elements that are not in order are moved to the
6 min read
Article Tags :
Practice Tags :