博客
关于我
桶排序的单链表实现及其变种
阅读量:420 次
发布时间:2019-03-06

本文共 3002 字,大约阅读时间需要 10 分钟。

《算法导论》中桶排序问题的单链表实现

《算法导论》CLRS 第八章 线性时间排序
8.4 桶排序
桶排序的思想就是把区间[0, 1)划分成n个相同大小的子区间,每一个区间称为桶(bucket)。然后,将n个输入数据分布到各个桶中去。因为输入数均匀且独立均匀分布在[0, 1)上,所以一般不会有很多数落在一个桶中的情况。为得到结果,先对各个桶中的数进行排序,然后按次序把各个桶中的元素列出来即可。
在桶排序算法中,假设输入的是一个含n个元素的数组A,且每个元素满足0≤A[i]<1。另外,还需要一个辅助数组B[0..n-1]来存放链表(桶),并假设可以用某种机制来维护这些表。
BUCKET-SORT(A)

1 n ← length[A]2 for i ← 1 to n3     do insert A[i] into list B[⌊n A[i]⌋]4 for i ← 0 to n - 15     do sort list B[i] with insertion sort6 concatenate the lists B[0], B[1], . . ., B[n - 1] together in order

下图表示出了桶排序作用于有10个数的输入数组上的操作过程。

AC代码:

// 待排序数组arr[1...n]内的元素是随机分布在[0,1)区间内的的浮点数 #include
#define bucket_num 10 // 分配到多少个桶中,可设置 using namespace std;struct ListNode{ double value; ListNode *next;};//桶排序主程序void bucketSort(double* arr, int length){ ListNode key[bucket_num]; // 映射关系: int key <- ElemNum/bucket_num int number = 0; ListNode *p, *q; //插入节点临时变量 int counter = 0; for(int i = 0; i < bucket_num; i++) { key[i].value = 0; key[i].next = NULL; } for(int i = 0; i < length; i++) { ListNode *insert = new ListNode(); insert->value = arr[i]; insert->next = NULL; number = arr[i] * bucket_num; if(key[number].next == NULL) { key[number].next = insert; } else { p = &key[number]; q = key[number].next; while((q != NULL) && (q->value <= arr[i])) { q = q->next; p = p->next; } insert->next = q; p->next = insert; } } for(int i = 0; i < bucket_num; i++) { p = key[i].next; if(p == NULL) continue; while(p != NULL) { arr[counter++] = p->value; p = p->next; } }}int main(){ double arr[] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.34, 0.94, 0.21, 0.12, 0.23}; int len=sizeof(arr)/sizeof(arr[0]); bucketSort(arr, len); for(int i = 0; i < len; i++) { cout << arr[i] << " "; } cout << endl; return 0;}

参考:

相关链接:

(感觉代码不太像 桶排序)

#include
#include
#include
using namespace std; void bucketsort(double* a, int n) { list
* b = new list
[n]; for (int i = 0; i < n; i++) { b[int(a[i])].push_back(a[i]); } for (int i = 0; i < n; i++) { b[i].sort(); } for (int i = 0,j=0; i < n; i++) { while (b[j].size() < 1)j++; a[i] = b[j].front(); b[j].pop_front(); }} int main() { double arr[] = {0.1,1.1,2.2,3.5,1.5,2.3,7.5,1.7}; int n = 8; bucketsort(arr, n); for (int i = 0; i < 8; i++) { cout << arr[i] << " "; } cout << endl; return 0;}
将2.3改为9.3时,程序会异常而终止...

1 n ← length[A]2 for i ← 1 to n3     do insert A[i] into list B[⌊n A[i]⌋]4 for i ← 0 to n - 15     do sort list B[i] with insertion sort6 concatenate the lists B[0], B[1], . . ., B[n - 1] together in order

转载地址:http://gvduz.baihongyu.com/

你可能感兴趣的文章
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>