×
数据结构教程数据结构简介数据结构算法数据结构渐近分析指针结构体

数组

数组二维数组

链表

链表双链表循环单向链表循环双向链表

堆栈

堆栈数组实现堆栈堆栈的链表实现

队列

队列队列的数组实现队列的链表实现循环队列

二叉树二叉搜索树平衡搜索树(AVL树)B树B+树

图类型图的表示广度优先搜索(BFS)算法深度优先搜索(DFS)算法生成树

搜索

线性搜索二进制(二分查找)搜索

排序

冒泡排序桶排序梳排序计数排序堆排序插入排序合并排序快速排序基数(Radix)排序选择排序希尔排序双调排序鸡尾酒排序圈排序

桶排序


存储桶排序也称为bin 排序。它的工作原理是将元素分配到也称为存储桶的数组中。 桶使用不同的排序算法单独排序。

桶排序的复杂性

算法 复杂性
空间 O(1)
最坏情况 O(n^2)
最好情况 Ω(n+k)
平均情况 θ(n+k)

算法

第1步,开始
第2步,设置一个最初为空的“桶”(`buckets`)数组。
第3步,分散:遍历原始数组,将每个对象放入其存储桶中。
第4步,对每个非空桶进行排序。
第5步,收集:按顺序访问存储桶并将所有元素放回原始数组中。
第6步,停止

使用C语言实现桶排序算法如下 -

#include <stdio.h>  
void Bucket_Sort(int array[], int n)  
{    
    int i, j;    
    int count[n];   
    for (i = 0; i < n; i++)  
        count[i] = 0;  

    for (i = 0; i < n; i++)  
        (count[array[i]])++;  

    for (i = 0, j = 0; i < n; i++)    
        for(; count[i] > 0; (count[i])--)  
            array[j++] = i;  
}     
/* End of Bucket_Sort() */  

/* The main() begins */  
int main()  
{  
    int array[100], i, num;   

    printf("Enter the size of array : ");     
    scanf("%d", &num);     
    printf("Enter the %d elements to be sorted:n",num);   
    for (i = 0; i < num; i++)  
        scanf("%d", &array[i]);   
    printf("The array of elements before sorting : n");  
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);    
    printf("The array of elements after sorting : n");   
    Bucket_Sort(array, num);   
    for (i = 0; i < num; i++)  
        printf("%d ", array[i]);     
    printf("n");       
    return 0;  
}

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)