Bucket Sorting

    0

    0

    Mahendra Kumar

    arraysDSAlogic•••

    Bucket Sort is a sorting algorithm that divides the unsorted array elements into several groups called buckets. Each bucket is then sorted by using any of the suitable sorting algorithms or recursively applying the same bucket algorithm.

    Finally, the sorted buckets are combined to form a final sorted array.

    def bucketSort(array):
        bucket = []
        # Create empty buckets
        for i in range(len(array)):
            bucket.append([])
        # Insert elements into their respective buckets
        for j in array:
            index_b = int(10 * j)
            bucket[index_b].append(j)
        # Sort the elements of each bucket
        for i in range(len(array)):
            bucket[i] = sorted(bucket[i])
        # Get the sorted elements
        k = 0
        for i in range(len(array)):
            for j in range(len(bucket[i])):
                array[k] = bucket[i][j]
                k += 1
        return array
    array = [.42, .32, .33, .52, .37, .47, .51]
    print("Sorted Array in descending order is")
    print(bucketSort(array))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.