非常无聊——STD::sort VS 基数排序
众所周知,Std::sort()是一个非常快速的排序算法,它基于快排,但又有所修改。一般来说用它就挺快的了,代码一行,时间复杂度O(nlogn)(难道不是大叫一声“老子要排序!!”就排好了么。。。)。我们也知道,不基于比较的排序可以达到O(n),比如说基数排序。什么,它是O(n * log(10)( max(n) ) ) 的?NO!!我们可以用sqrt(max(n))来作为进制,这样就是(N*logMax(n))=O(2*n)的了。。看起来很不错, 代码量嘛。。。。呵呵
所谓基数排序,就是做几次筒排,每一位一次,然后拉直,然后继续,时间复杂度O(n),我们来看一下效果吧
Data1:10^7随机数据
Data2:10^7不随机,从10^7到0
Data3:第二个数据每一项除与2,10^7项
Data4:第一个数据每一项除与2,10^7项
效果:
std::sort()
1.7.07s
2.5.51s
3.7.00s
4.5.31s
基数排序
1.5.31s
2.7.26s
3.4.89s
4.7.06s
觉得很奇怪,其实一四是对应的,二三是对应的。。。然后为什么会这样。。。不懂不懂。。。
分析一下,可能是读入原因,或者std::sort()对一些特殊的有优化,但是很大可能是——Cena抽了。。。
基数排序在排序上优化还是挺大的,但是,代码量和常数还有适用范围。。。呵呵
本文纯粹太无聊作死只做,我不会说std::sort()在评测的时候连续4次75分,每次无输出的点还不一样。。。Cena啊,你何时出1.0啊。。。
付:我写的基数排序极丑代码。。。
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib> using namespace std;
struct num
{
int data;
num* next;
}nu[]; void plu(num *f,num *b)
{
f->next=b;
} num* t[];
num* e[];
void add(int pos,num* l)
{
if (e[pos])
{
e[pos]->next=l;
e[pos]=l;
}
else
{
t[pos]=l;
e[pos]=l;
}
} int predo()
{
int n,ret=;
scanf("%d",&n);
for (int i=;i<=n;++i)
{
int j;
scanf("%d",&j);
nu[i].data=j;
nu[i].next=nu+i+;
if (i==n) nu[i].next=NULL;
ret=max(ret,j);
}
return ret;
}
num* root=nu+; void JSsort(int n)
{
for (num* now=root;now;)
{
num* nex=now->next;
now->next=NULL;
int tt=now->data%n;
add(tt,now);
now=nex;
}
num* last=NULL;
for (int i=;i<n;++i)
{
if (t[i])
{
if (!last)
{
root=t[i];
}
else
{
last->next=t[i];
}
for (num *now=t[i];now;now=now->next)
last=now;
}
t[i]=e[i]=NULL;
}
for (num* now=root;now;)
{
num* nex=now->next;
now->next=NULL;
int tt=now->data/n;
add(tt,now);
now=nex;
}
last=NULL;
for (int i=;i<n;++i)
{
if (t[i])
{
if (!last)
{
root=t[i];
}
else
{
last->next=t[i];
}
for (num *now=t[i];now;now=now->next)
last=now;
}
}
return;
} void print()
{
for (num* now=root;now;now=now->next)
{
printf("%d ",now->data);
}
printf("\n");
return;
} int main()
{
freopen ("sort.in","r",stdin);
freopen ("sort.out","w",stdout);
int k=predo();
JSsort(sqrt(k)+);
print();
return ;
}
非常无聊——STD::sort VS 基数排序的更多相关文章
- 将三维空间的点按照座标排序(兼谈为std::sort写compare function的注意事项)
最近碰到这样一个问题:我们从文件里读入了一组三维空间的点,其中有些点的X,Y,Z座标只存在微小的差别,远小于我们后续数据处理的精度,可以认为它们是重复的.所以我们要把这些重复的点去掉.因为数据量不大, ...
- 源码阅读笔记 - 1 MSVC2015中的std::sort
大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...
- c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法
在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误. 这是为什么呢?原因在于什么?如何解决? 看下面一个例子: int main(int, char*[]) { st ...
- std::sort引发的core
#include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...
- 一个std::sort 自定义比较排序函数 crash的分析过程
两年未写总结博客,今天先来练练手,总结最近遇到的一个crash case. 注意:以下的分析都基于GCC4.4.6 一.解决crash 我们有一个复杂的排序,涉及到很多个因子,使用自定义排序函数的st ...
- Qt使用std::sort进行排序
参考: https://blog.csdn.net/u013346007/article/details/81877755 https://www.linuxidc.com/Linux/2017-01 ...
- 今天遇到的一个诡异的core和解决 std::sort
其实昨天开发pds,就碰到了core,我还以为是内存不够的问题,或者其他问题. 今天把所有代码挪到了as这里,没想到又出core了. 根据直觉,我就觉得可能是std::sort这边的问题. 上网一搜, ...
- std::sort的详细用法
#include <algorithm> #include <functional> #include <array> #include <iostream& ...
- 科普:std::sort干了什么
std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...
随机推荐
- poj 2823单调队列模板题
#include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N 1100000 int getmin[N],getmax[N],num[N],n,k, ...
- 主流图数据库Neo4J、ArangoDB、OrientDB综合对比:架构分析
主流图数据库Neo4J.ArangoDB.OrientDB综合对比:架构分析 YOTOY 关注 0.4 2017.06.15 15:11* 字数 3733 阅读 16430评论 2喜欢 18 1: 本 ...
- sql-server-storage-internals
https://www.simple-talk.com/sql/database-administration/sql-server-storage-internals-101/
- How to Use DHCP Relay over LAN? - DrayTek Corp
Assuming Vigor2960 has two LAN networks. Network Administrator wants that, when the internal DHCP is ...
- 有用 .htaccess 使用方法大全
这里收集的是各种有用的 .htaccess 代码片段,你能想到的使用方法差点儿全在这里. 免责声明: 尽管将这些代码片段直接复制到你的 .htaccess 文件中,绝大多数情况下都是好用的,但也有极个 ...
- Python网络爬虫(一):初步认识网络爬虫
不管你是因为什么原因想做一个网络爬虫,首先做的第一件事情就是要了解它. 在了解网络爬虫之前一定要牢记下面4个要点,这是做网络爬虫的基础: 1.抓取 py的urllib不一定去用.可是要学.假设你还没用 ...
- Solidworks如何修改单位
文档属性-单位-修改成mm
- 大数据分析:结合 Hadoop或 Elastic MapReduce使用 Hunk
作者 Jonathan Allen ,译者 张晓鹏 Hunk是Splunk公司一款比較新的产品,用来对Hadoop和其他NoSQL数据存储进行探測和可视化,它的新版本号将会支持亚马逊的Elastic ...
- install yael on the ubuntu 12.04
1. bits/predefs.h no such file or directory ??? sudo apt-get install gcc-multilib 2. sudo gedit /et ...
- USRP通信的结构体和常量(上位机、下位机共用)
fw_common.h包括了USRP固件和上位机共用的代码,寄存器地址映射.结构体定义等 #include <stdint.h> /*! * Structs and constants f ...