Home  >  Article  >  Backend Development  >  Flow chart of Python code implementing bucket sorting algorithm

Flow chart of Python code implementing bucket sorting algorithm

WBOY
WBOYforward
2024-01-24 20:27:061024browse

The simple understanding of the bucket sorting algorithm is to disperse the data into buckets, then sort the data in each bucket, and finally arrange the data in order.

桶排序算法流程图 Python代码实现桶排序

4. Enter other numbers in the array and repeat step 3, as shown in the figure:

桶排序算法流程图 Python代码实现桶排序

Python code to implement bucket sorting

def bucketSort(array):
    bucket = []

    for i in range(len(array)):
        bucket.append([])

    for j in array:
        index_b = int(10 * j)
        bucket[index_b].append(j)

    for i in range(len(array)):
        bucket[i] = sorted(bucket[i])

    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))

The above is the detailed content of Flow chart of Python code implementing bucket sorting algorithm. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:163.com. If there is any infringement, please contact admin@php.cn delete