关于桶排序算法的STL实现
来源:互联网 发布:中国历史多少年知乎 编辑:程序博客网 时间:2024/06/05 06:43
直接按照算法导论上伪代码做的。其中,排序算法直接调用STL库中的sort函数,即快排算法,当然,用归并排序法更好。因为,运用桶排序算法也可能是将所有数“恰巧”分配在一个桶内,例如A=[0.1,0.12,0.15,0.16....],在这种情况下,A的所有数据分配在B中的1桶内。这个时候如果用插排的话,就是最坏运行情况,复杂度为O(N^2)。B是用链表数组实现的。
好了,不多说了,直接上STL代码程序。
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <string.h>
#include <stdexcept>
#include<bitset>
#include<cassert>
#include<stdlib.h>
#include<list>
using namespace std;
#define N 10;
void bucket_sort(double *a,int len)
{
list<double> child[10];
int num;
for(int i=0;i<len;i++)
{
num=len*a[i];
child[num].push_back(a[i]);
}
for(int i=0;i<len;i++)
{
child[i].sort();
for(list<double>::iterator iter=child[i].begin();iter!=child[i].end();iter++)
{
if(*iter!=NULL)
cout<<*iter<<endl;
}
}
}
int main ( void )
{
double a[]={0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68};
int n=sizeof(a)/sizeof(double);
bucket_sort(a,n);
system("pause");
return 0;
}
//桶排序实现
- 关于桶排序算法的STL实现
- stl的排序 和 用算法实现的排序比较
- 关于排序算法的思想及实现
- js中关于排序算法的实现
- STL排序算法的分析
- C++STL 编程技巧1 STL中各种排序算法的实现
- 拓扑排序的STL实现
- STL Sort排序的实现
- 用STL实现的排序
- [排序算法]--桶排序的Java实现
- Dijkstra算法的STL实现
- 关于利用STL中的sort算法对向量等容器进行排序的问题
- STL sort算法技巧和基本排序算法实现
- STL中各种排序算法的区别
- STL的排序算法略看
- STL中list的排序算法
- STL中排序算法的选择
- STL 中 算法的遍历和排序
- MR-找朋友
- POJ 2034 Anti-prime Sequences(数论+dfs)
- java死锁例子详解
- Linux 新手非常有用的 20 个命令
- iOS开发-我的iOS工程结构
- 关于桶排序算法的STL实现
- 图的遍历
- gcc 4.7 + 不能使用boost_thread的解决办法
- SpringMVC定时任务 执行两次
- [树状数组+DP]hdu3450 Counting Sequences
- 数据备份
- iOS开发UI篇—在UItableview中实现加载更多功能
- 正则表达式
- 完美跳槽,注意这八点!