0
点赞
收藏
分享

微信扫一扫

桶排序——Java

桶排序——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实现

实现代码(bucket_sort.c)


1 /**

2 * 桶排序:C 语言

3 *

4 * @author skywang

5 * @date 2014/03/13

6 */

7

8 #include <stdio.h>

9 #include <stdlib.h>

10 #include <string.h>

11

12 // 数组长度

13 #define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )

14

15 /*

16 * 桶排序

17 *

18 * 参数说明:

19 * a -- 待排序数组

20 * n -- 数组a的长度

21 * max -- 数组a中最大值的范围

22 */

23 void bucket_sort(int a[], int n, int max)

24 {

25 int i, j;

26 int *buckets;

27

28 if (a==NULL || n<1 || max<1)

29 return ;

30

31 // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。

32 if ((buckets=(int *)malloc(max*sizeof(int)))==NULL)

33 return ;

34 memset(buckets, 0, max*sizeof(int));

35

36 // 1. 计数

37 for(i = 0; i < n; i++)

38 buckets[a[i]]++;

39

40 // 2. 排序

41 for (i = 0, j = 0; i < max; i++)

42 while( (buckets[i]--) >0 )

43 a[j++] = i;

44

45 free(buckets);

46 }

47

48 void main()

49 {

50 int i;

51 int a[] = {8,2,3,4,3,6,6,3,9};

52 int ilen = LENGTH(a);

53

54 printf("before sort:");

55 for (i=0; i<ilen; i++)

56 printf("%d ", a[i]);

57 printf("\n");

58

59 bucket_sort(a, ilen, 10); // 桶排序

60

61 printf("after sort:");

62 for (i=0; i<ilen; i++)

63 printf("%d ", a[i]);

64 printf("\n");

65 }

复制代码

桶排序C++实现

实现代码(BucketSort.cpp)



复制代码

1 /**

2 * 桶排序:C++

3 *

4 * @author skywang

5 * @date 2014/03/13

6 */

7

8 #include <iostream>

9 #include <cstring>

10 using namespace std;

11

12 /*

13 * 桶排序

14 *

15 * 参数说明:

16 * a -- 待排序数组

17 * n -- 数组a的长度

18 * max -- 数组a中最大值的范围

19 */

20 void bucketSort(int* a, int n, int max)

21 {

22 int i, j;

23 int *buckets;

24

25 if (a==NULL || n<1 || max<1)

26 return ;

27

28 // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。

29 if ((buckets = new int[max])==NULL)

30 return ;

31 memset(buckets, 0, max*sizeof(int));

32

33 // 1. 计数

34 for(i = 0; i < n; i++)

35 buckets[a[i]]++;

36

37 // 2. 排序

38 for (i = 0, j = 0; i < max; i++)

39 while( (buckets[i]--) >0 )

40 a[j++] = i;

41

42 delete[] buckets;

43 }

44

45

46 int main()

47 {

48 int i;

49 int a[] = {8,2,3,4,3,6,6,3,9};

50 int ilen = (sizeof(a)) / (sizeof(a[0]));

51

52 cout << "before sort:";

53 for (i=0; i<ilen; i++)

54 cout << a[i] << " ";

55 cout << endl;

56

57 bucketSort(a, ilen, 10); // 桶排序

58

59 cout << "after sort:";

60 for (i=0; i<ilen; i++)

61 cout << a[i] << " ";

62 cout << endl;

63

64 return 0;

65 }

复制代码

桶排序Java实现

实现代码(BucketSort.java)



复制代码

1 /**

2 * 桶排序:Java

3 *

4 * @author skywang

5 * @date 2014/03/13

6 */

7

8 public class BucketSort {

9

10 /*

11 * 桶排序

12 *

13 * 参数说明:

14 * a -- 待排序数组

15 * max -- 数组a中最大值的范围

16 */

17 public static void bucketSort(int[] a, int max) {

18 int[] buckets;

19

20 if (a==null || max<1)

21 return ;

22

23 // 创建一个容量为max的数组buckets,并且将buckets中的所有数据都初始化为0。

24 buckets = new int[max];

25

26 // 1. 计数

27 for(int i = 0; i < a.length; i++)

28 buckets[a[i]]++;

29

30 // 2. 排序

31 for (int i = 0, j = 0; i < max; i++) {

32 while( (buckets[i]--) >0 ) {

33 a[j++] = i;

34 }

35 }

36

37 buckets = null;

38 }

39

40 public static void main(String[] args) {

41 int i;

42 int a[] = {8,2,3,4,3,6,6,3,9};

43

44 System.out.printf("before sort:");

45 for (i=0; i<a.length; i++)

46 System.out.printf("%d ", a[i]);

47 System.out.printf("\n");

48

49 bucketSort(a, 10); // 桶排序

50

51 System.out.printf("after sort:");

52 for (i=0; i<a.length; i++)

53 System.out.printf("%d ", a[i]);

54 System.out.printf("\n");

55 }

56 }

复制代码

上面3种实现的原理和输出结果都是一样的。下面是它们的输出结果:


before sort:8 2 3 4 3 6 6 3 9

after sort:2 3 3 3 4 6 6 8 9


举报

相关推荐

0 条评论