众所周知,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 基数排序的更多相关文章

  1. 将三维空间的点按照座标排序(兼谈为std::sort写compare function的注意事项)

    最近碰到这样一个问题:我们从文件里读入了一组三维空间的点,其中有些点的X,Y,Z座标只存在微小的差别,远小于我们后续数据处理的精度,可以认为它们是重复的.所以我们要把这些重复的点去掉.因为数据量不大, ...

  2. 源码阅读笔记 - 1 MSVC2015中的std::sort

    大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...

  3. c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法

    在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误. 这是为什么呢?原因在于什么?如何解决? 看下面一个例子: int main(int, char*[]) { st ...

  4. std::sort引发的core

    #include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...

  5. 一个std::sort 自定义比较排序函数 crash的分析过程

    两年未写总结博客,今天先来练练手,总结最近遇到的一个crash case. 注意:以下的分析都基于GCC4.4.6 一.解决crash 我们有一个复杂的排序,涉及到很多个因子,使用自定义排序函数的st ...

  6. Qt使用std::sort进行排序

    参考: https://blog.csdn.net/u013346007/article/details/81877755 https://www.linuxidc.com/Linux/2017-01 ...

  7. 今天遇到的一个诡异的core和解决 std::sort

    其实昨天开发pds,就碰到了core,我还以为是内存不够的问题,或者其他问题. 今天把所有代码挪到了as这里,没想到又出core了. 根据直觉,我就觉得可能是std::sort这边的问题. 上网一搜, ...

  8. std::sort的详细用法

    #include <algorithm> #include <functional> #include <array> #include <iostream& ...

  9. 科普:std::sort干了什么

    std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...

随机推荐

  1. Python生成随机不重复姓名昵称

    姓采用百家姓,名字从常用名字高频字选取两个汉字,再和当前时间戳组合,估计应该是不会重复了,代码如下: # -*- coding:utf-8 -*- import random import time ...

  2. codechef营养题 第三弹

    第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...

  3. DESEncrypt对称加密解密

    分享一个很好用的DESEncrypt对称加密解密的类 using System; using System.Security.Cryptography; using System.Text; usin ...

  4. 【Codeforces 1038D】Slime

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 相当于让你确定每个数字前面的系数是-1还是+1 有个结论是这样每次和相邻的减的话, 任何出除了全"-1"和全"+ ...

  5. noip模拟赛 都市

    分析:是一道非常有意思的题,30分的暴力的话枚举每个位置是什么数,然后排个序,用map判一下重就好了,比较麻烦. 满分做法显然不可能讨论每个位置所有的情况,肯定是有规律的,现将这n*(n-1)/2个数 ...

  6. loginitem

    + (BOOL) willStartAtLogin:(NSURL *)itemURL { Boolean foundIt=false; LSSharedFileListRef loginItems = ...

  7. 洛谷—— P1419 寻找段落

    https://www.luogu.org/problem/show?pid=1419 题目描述 给定一个长度为n的序列a_i,定义a[i]为第i个元素的价值.现在需要找出序列中最有价值的“段落”.段 ...

  8. JavaScript 读取CSV文件并转为js对象

    html部分 <!-- 创建隐藏file input --><button type="button" name="seach" onclic ...

  9. MyBatis3-实现单表数据的增删查改

    继续前一篇文章http://www.cnblogs.com/EasonJim/p/7050710.html所示的例子,返回的是单个实体,而接下来将进行列表的返回等操作: 一.查询列表 查询出列表,也就 ...

  10. delphi不同版本字符串类型的演化

    string,DELPHI2009以前的版本string=ansistring,一个字符占一个字节,DELPHI2009及以上版本string=unicodestring,一个字符占二个字节. cha ...