CLRS:sorting in linear time O(n)

//intput array A,output array result.   count array count .

//all the elements is in te range of 0~k.

//if k=O(n),the complexity is Θ(n)

//counting sort is stable

for(i=0;i<n;i++)result[i]=0;

for(i=1;i<=n;i++)count[a[i]]++;

for(i=1;i<=n;i++)count[i]+=count[i-1];

for(j=n;j>1;j--)

{

result[count[A[j]]]=A[j];

count[A[j]]--;

}

/radix sort

//对每一位数字进行排序,收集后,递归进行,直到全部排序完成

//bucket sort

//按区间分布bucket,bucket内部使用insert sort.

时间: 2024-10-10 14:40:27

CLRS:sorting in linear time O(n)的相关文章

线性时间排序 Sorting in linear time O(n)

Sorting In Linear Time 之前尝试过很多的排序算法, 都是基于比较的排序算法(base on comparing) Collection of algorithm for sorting (part one) http://blog.csdn.net/cinmyheart/article/details/39268783 Collection of algorithm for sorting (part two) http://blog.csdn.net/cinmyheart

算法导论部分总结图片版

我们的算法导论:(中文版下载链接) 目录(Table of Contents)前言(Preface)第一部分(Part I) 基础(Foundations)第一章 计算中算法的角色(The Role of Algorithms in Computing)第二章 开始(Getting Started)第三章 函数的增长率(Growth of Functions)第四章 递归(Recurrences)第五章 概率分析与随机化算法(Probabilistic Analysis and Randomiz

算法——计数排序与快速排序

计数排序是一种算法复杂度 O(n) 的排序方法,适合于小范围集合的排序.比如100万学生参加高考,我们想对这100万学生的数学成绩(假设分数为0到100)做个排序.我们如何设计一个 最高效的排序算法.本文不光给出计数排序算法的传统写法,还将一步步深入讨论算法的优化,直到时间复杂度和空间复杂度最优. 先看看计数排序的定义 Counting sort (sometimes referred to as ultra sort or math sort[1]) is a sorting algorith

(转)Awesome Courses

Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect

[Algorithm] 如何正确撸&lt;算法导论&gt;CLRS

其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话,很快就读完了.你所说的证明和推导大概也都是在第三章介绍了,可以回过头再认真看几遍. 至于课后题,比较难,我只做了前几章,如果要做完需要更多时间和精力.这可以通过之后做算法题来弥补,可以去leetcode等网站找一些经典的算法题做一做,加深理解. Facebook的工程师写的攻略,介绍了用算法导论来

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 63 Problem Description A topological sort or topological ordering of a directed gr