Non-comparison based sort

Bucket Sort:

Scenario:

Work when keys are in a small range.

Linked List.

Algorithm(Stable):

1. Walk through each item and enqueue each item into its appropriate queue.

2. Concatenate queues in order

Complexity:

O(queue+n):

O(q) time to initialize and concatenate buckets together.

O(n) time to put items to buckets.

Counting Sort:

Scenario:

Array. When items are keys, no associated values.

Algorithm: (Stable, Similar to bucket sort)

Count copies of each key.

Complexity:

O(queue+n)

For objects, counting sort with complete items for key plus associated item.

for(i=0;i<x.length;i++)

{

  counts[x[i].key]++;

}

//Then, do a scan so counts[i] contains # of keys less than i.

total = 0;

for(j=0;j<counts.length;j++)

{

  c=counts[j];

  counts[j] = total;

  total += c;

}

//let y be output array, counts[i] tells us first index of y to put items with key i.

for(i=0;i<x.length;i++)

{

  y[counts[x[i].key]++]=x[i];

}

Radix Sort(Base Sort):

sort 1000 items in range 0~99,999,999

Algorithm:

Sort on last digit, then recursively on second digit, up to the most significant digit.

Works because bucket&counting sort are stable.

Faster if we sort on 2 digits at a time(choose radix/base q = 100). For computers, more natural to choose power-of-two radix like q=256

Complexity:

each pass inspects log2(q) bits of each key.

if all keys represented in b bits, # of passes is ceiling of b/log2(q)

O((n+q)*ceil(b/log2(q))

时间: 2024-10-22 04:46:34

Non-comparison based sort的相关文章

Searching Quickly UVA 123

说说:感觉这题目是做得越来越繁琐了.这道题基本上把接下来课设要做的英语词典的框架给做出来了.好像本题的解法就是所谓的倒排索引.先给你一系列的句子,其实就是一系列的词啦.当然里面要把一些词去掉.然后把剩下的每个词都做成索引.最后按字典序把所有词所在的句子都输出就可以了.我的做法是定义了一个结构index包含关键词和一个指针,该指针指向一个链表,链表中的每个节点包含了该关键词所在的句子的位置,以及该关键词在句子中的位置.然后读入词,若不是要忽略的词就判断该词是否已经在关键词表中,在则添加到相应关键词

C++ Merge sort(归并排序)

归并排序(merge sort)是一个时间复杂度为O(nlogn)的基于比较的排序算法(comparison based sorting algorithm). 归并排序大多数实现(implementation)都将其实现成了一个stable sort, 所谓的stable sort的意思就是the implementation preserves the input order of equal elements in the sorted order. merge sort 也采用了divi

.NET Framework System.Array.Sort 演示

本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable.ICloneable 以及泛型委托.匿名方法.Lambda 表达式的理解. 下载 Demo 自定义类 自定义后面用到的 Person 类和 PersonComparer 类. 自定义 Person 类,该类有两个属性:last name 和 first name,重构了 ToString 方法.P

STL_算法_对全部元素排序(sort、stable_sort)

C++ Primer 学习中. . . ? 简单记录下我的学习过程?(代码为主) //大部分容器适用.不适用于list容器 sort(b,e) sort(b,e,p) stable_sort(b,e) stable_sort(b,e,p) /**------http://blog.csdn.net/u010579068------**/ #include<iostream> #include<cstdio> #include<string> #include<ve

ORA_ERROR大全

转自:http://blog.csdn.net/haiross/article/details/12839229 常见错误:-60 ORA00060: deadlock detected while waiting for resource 一般错误:  - 1 ORA00001: unique constraint (.) violated  -17 ORA00017: session requested to set trace event  -18 ORA00018: maximum nu

1、Python基础知识

 以下内容摘自:http://www.cnblogs.com/happyframework/p/3255962.html Hello,大家好!在此感谢“博主”!的分享! 1 # coding=utf-8 2 3 print('Hello,大家好!在此感谢“博主”!的分享!') 乘方 1 print(2**3) 变量 var = 1 print(var) var = "叮当" print(var) 注:这里的var = xxxx不叫变量赋值,而叫变量绑定,python维护了一个符号表(变

RMAN异机恢复步骤及故障处理

一.测试机安装OS+Oracle Software,包括配置oracle用户组和环境变量(略) 二.开始异机恢复 1. 复制源库最新备份集.初始化参数.密码文件到测试机 [[email protected] backupsets]$ scp *20141012* 192.168.1.213:/tmp The authenticity of host '192.168.1.213 (192.168.1.213)' can't be established. RSA key fingerprint

Python:Python学习总结

背景 PHP的$和->让人输入的手疼(PHP确实非常简洁和强大,适合WEB编程),Ruby的#.@.@@也好不到哪里(OO人员最该学习的一门语言). Python应该是写起来最舒服的动态语言了,一下是一些读书笔记,最后会介绍一下高级的用法:Mixin.Open Class.Meta Programming和AOP. 文中有些地方是用2.7开发的,如果您安装的是3.x,有几点需要注意: print "xxx" 要换成 print("xxx") __metacla

Flink - Juggling with Bits and Bytes

http://www.36dsj.com/archives/33650 http://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-Bytes.html http://www.bigsynapse.com/addressing-big-data-performance ,addressing-big-data-performance   第一篇描述,当前JVM存在的问题, 1. Java对象开销 Java对象的存储密度相对偏低,对