当前位置:网站首页>桶排序
桶排序
2022-06-30 02:13:00 【生活需要深度】
概要
本章介绍排序算法中的桶排序。内容包括:
1. 桶排序介绍
2. 桶排序图文说明
3. 桶排序实现
3.1 桶排序C实现
3.2 桶排序C++实现
3.3 桶排序Java实现
转载请注明出处:桶排序 - 如果天空不死 - 博客园
更多排序和算法请参考:数据结构与算法系列 目录
桶排序介绍
桶排序(Bucket Sort)的原理很简单,它是将数组分到有限数量的桶子里。
假设待排序的数组a中共有N个整数,并且已知数组a中数据的范围[0, MAX)。在桶排序时,创建容量为MAX的桶数组r,并将桶数组元素都初始化为0;将容量为MAX的桶数组中的每一个单元都看作一个"桶"。
在排序时,逐个遍历数组a,将数组a的值,作为"桶数组r"的下标。当a中数据被读取时,就将桶的值加1。例如,读取到数组a[3]=5,则将r[5]的值+1。
桶排序图文说明
桶排序代码
/*
* 桶排序
*
* 参数说明:
* a -- 待排序数组
* n -- 数组a的长度
* max -- 数组a中最大值的范围
*/
void bucketSort(int a[], int n, int max)
{
int i,j;
int buckets[max];
// 将buckets中的所有数据都初始化为0。
memset(buckets, 0, max*sizeof(int));
// 1. 计数
for(i = 0; i < n; i++)
buckets[a[i]]++;
// 2. 排序
for (i = 0, j = 0; i < max; i++)
{
while( (buckets[i]--) >0 )
a[j++] = i;
}
}bucketSort(a, n, max)是作用是对数组a进行桶排序,n是数组a的长度,max是数组中最大元素所属的范围[0,max)。
假设a={8,2,3,4,3,6,6,3,9}, max=10。此时,将数组a的所有数据都放到需要为0-9的桶中。如下图:

在将数据放到桶中之后,再通过一定的算法,将桶中的数据提出出来并转换成有序数组。就得到我们想要的结果了。
桶排序实现
/**
* 桶排序:C 语言
*
* @author skywang
* @date 2014/03/13
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 数组长度
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
/*
* 桶排序
*
* 参数说明:
* a -- 待排序数组
* n -- 数组a的长度
* max -- 数组a中最大值的范围
*/
void bucket_sort(int a[], int n, int max)
{
int i, j;
int *buckets;
if (a==NULL || n<1 || max<1)
return ;
// 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。
if ((buckets=(int *)malloc(max*sizeof(int)))==NULL)
return ;
memset(buckets, 0, max*sizeof(int));
// 1. 计数
for(i = 0; i < n; i++)
buckets[a[i]]++;
// 2. 排序
for (i = 0, j = 0; i < max; i++)
while( (buckets[i]--) >0 )
a[j++] = i;
free(buckets);
}
void main()
{
int i;
int a[] = {8,2,3,4,3,6,6,3,9};
int ilen = LENGTH(a);
printf("before sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
bucket_sort(a, ilen, 10); // 桶排序
printf("after sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
}/**
* 桶排序:C++
*
* @author skywang
* @date 2014/03/13
*/
#include <iostream>
#include <cstring>
using namespace std;
/*
* 桶排序
*
* 参数说明:
* a -- 待排序数组
* n -- 数组a的长度
* max -- 数组a中最大值的范围
*/
void bucketSort(int* a, int n, int max)
{
int i, j;
int *buckets;
if (a==NULL || n<1 || max<1)
return ;
// 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。
if ((buckets = new int[max])==NULL)
return ;
memset(buckets, 0, max*sizeof(int));
// 1. 计数
for(i = 0; i < n; i++)
buckets[a[i]]++;
// 2. 排序
for (i = 0, j = 0; i < max; i++)
while( (buckets[i]--) >0 )
a[j++] = i;
delete[] buckets;
}
int main()
{
int i;
int a[] = {8,2,3,4,3,6,6,3,9};
int ilen = (sizeof(a)) / (sizeof(a[0]));
cout << "before sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
bucketSort(a, ilen, 10); // 桶排序
cout << "after sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}桶排序Java实现
实现代码(BucketSort.java)
/**
* 桶排序:Java
*
* @author skywang
* @date 2014/03/13
*/
public class BucketSort {
/*
* 桶排序
*
* 参数说明:
* a -- 待排序数组
* max -- 数组a中最大值的范围
*/
public static void bucketSort(int[] a, int max) {
int[] buckets;
if (a==null || max<1)
return ;
// 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。
buckets = new int[max];
// 1. 计数
for(int i = 0; i < a.length; i++)
buckets[a[i]]++;
// 2. 排序
for (int i = 0, j = 0; i < max; i++) {
while( (buckets[i]--) >0 ) {
a[j++] = i;
}
}
buckets = null;
}
public static void main(String[] args) {
int i;
int a[] = {8,2,3,4,3,6,6,3,9};
System.out.printf("before sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
bucketSort(a, 10); // 桶排序
System.out.printf("after sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
}
}上面3种实现的原理和输出结果都是一样的。下面是它们的输出结果:
before sort:8 2 3 4 3 6 6 3 9 after sort:2 3 3 3 4 6 6 8 9
边栏推荐
- 谁再用Redis过期监听实现关闭订单,立马滚蛋!
- Is it safe to open an account in Sinosteel futures?
- 图解 Google V8 # 19 :异步编程(二):V8 是如何实现 async/await 的?
- Implement vs to run only one source file at a time
- Conjecture of prime pairs in C language
- Tencent released the first Office Photo 23 years ago. It's so chronological
- DDoS threat situation gets worse
- Vs realize quick replacement function
- Jenkins continuous integration environment construction VII (Jenkins parametric construction)
- [protection mode] segment descriptor
猜你喜欢

Scala基础【入门及安装】

003_ color

UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
![[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce

【MySQL 04】使用MySQL Workbench 8.0 CE 备份及恢复Linux中的MySQL数据库

Matlab 2012a 绘制带箭头的线段

Matlab 2012a drawing line segment with arrow
![[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package

DMX的配置
随机推荐
8 — router
(1) Basic learning - figure out the difference between pin, pad, port, IO and net
33Mysql
DDoS threat situation gets worse
Fake divorce turns into real divorce. What about property
Upload, use of Avatar
207. curriculum - graph theory, depth traversal
After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
想转行,但不知道自己要做什么工作比较好?
Is it safe to open an account in Sinosteel futures?
【MySQL 04】使用MySQL Workbench 8.0 CE 备份及恢复Linux中的MySQL数据库
Method of converting songs from DTS to MP3
NCA: the nine year old has launched a DDoS attack
Is the processor the main factor in buying a mobile phone?
Record an oom exception in production
DHU programming exercise
dhu编程练习
【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
假离婚变成真离婚,财产怎么办
网上炒股安全么?炒股需要开户吗?