当前位置:网站首页>桶排序
桶排序
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
边栏推荐
猜你喜欢

【MySQL 05】SUSE 12 SP5 安装MySQL后第一次修改mysql密码

Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"

Realization of a springboard machine

Want to change careers, but don't know what you want to do?

What is idempotency? Detailed explanation of four interface idempotence schemes!

How does payment splitting help B2B bulk commodity transactions?

012_ switch

Learning C language from scratch day 026

ROS bridge notes (01) - APT installation, source code compilation and installation, installation dependency, and operation display
![[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL](/img/37/d24c9e5fad606d2623900ad018b6af.png)
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
随机推荐
dhu编程练习
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
Is the processor the main factor in buying a mobile phone?
CTF入门学习(Web方向)
001_ layout
Vs realize quick replacement function
dhu编程练习
【MySQL 04】使用MySQL Workbench 8.0 CE 備份及恢複Linux中的MySQL數據庫
9 - regular check set
DDoS attacks and destructive ripple effects against online gamers
DDoS threat situation gets worse
DDoS extortion attacks: what you need to know
Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
AI landing manufacturing: intelligent robots should have these four abilities
Want to change careers, but don't know what you want to do?
DDoS "fire drill" service urges companies to prepare
(4) Blender source code analysis flash window display process
Unity2d-- add keys to animation and bind events
Scala基础【入门及安装】
Upload, use of Avatar