Open In App

Bingo Sort in Python

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:

Below is the implementation of the above approach:

# 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:

Auxiliary Space: O(1)

Article Tags :