由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序
在LeetCode中有个题目叫Maximum Gap。是求一个非排序的正数数列中按顺序排列后的最大间隔。这个题用桶排序和基数排序都能够实现。以下说一下桶排序、基数排序和计数排序这三种非比較排序。
桶排序
这样的排序的主要思想是。把数列分配到多个桶中,然后再在各个桶中使用排序算法进行排序。当然也能够继续使用桶排序。
如果数组的最大值是A,最小值是B,长度是L,则每一个桶的大小能够是S=Max(1,(A-B)/(L-1))则能够分为(A-B)/S+1个桶。
对于数列中的数字x。用(x-B)/S 来得到x应该在的桶的序号,然后把x放在对应的桶中。
循环上面步骤。
桶排序对于数列中的数字是均匀排列的情况很适用。
以下贴一个Leetcode Maximum Gap 的代码,当中用了桶排序的思想
- class Solution:
- # @param num, a list of integer
- # @return an integer
- def maximumGap(self, num):
- N = len(num)
- if N < 2:
- return 0
- A = min(num)
- B = max(num)
- bucketRange = max(1, int((B - A - 1) / (N - 1)) + 1) #ceil( (B - A) / (N - 1) )
- bucketLen = ((B - A) / bucketRange + 1)
- buckets = [None] * bucketLen
- for K in num:
- loc = (K - A) / bucketRange
- bucket = buckets[loc]
- if bucket is None:
- bucket = {'min' : K, 'max' : K}
- buckets[loc] = bucket
- else:
- bucket['min'] = min(bucket['min'], K)
- bucket['max'] = max(bucket['max'], K)
- maxGap = 0
- for x in range(bucketLen):
- if buckets[x] is None:
- continue
- y = x + 1
- while y < bucketLen and buckets[y] is None:
- y += 1
- if y < bucketLen:
- maxGap = max(maxGap, buckets[y]['min'] - buckets[x]['max'])
- x = y
- return maxGap
基数排序
基数排序严蔚敏的数据结构中有具体介绍。主要是思想是用数字的keyword基数进行分配。这里的keyword基数一般是是进制的基数。比方十进制的话就是10,二进制就是2。通常使用链式基数排序,主要就是分配和和收集的过程。分配是里按keyword的不同值分配到各个队列中。然后收集。反复运行这个过程。
计数排序
计数排序是直接计算数字x在数列中应该在的位置。统计比它小的数字有多少个就知道它的位置了。
一个简单的C代码
- void COUNTINGSORT(int *A, int *B, int array_size, int k)
- {
- int C[k+1], i, value, pos;
- for(i=0; i<=k; i++)
- {
- C[i] = 0;
- }
- for(i=0; i< array_size; i++)
- {
- C[A[i]] ++;
- }
- for(i=1; i<=k; i++)
- {
- C[i] = C[i] + C[i-1];
- }
- for(i=array_size-1; i>=0; i--)
- {
- value = A[i];
- pos = C[value];
- B[pos-1] = value;
- C[value]--;
- }
- }
最后贴一张各个排序算法的时间复杂度
參考文献:
1、 http://www.cnblogs.com/kaituorensheng/archive/2013/02/23/2923877.html
2、http://bookshadow.com/weblog/2014/12/14/leetcode-maximum-gap/
3、http://hxraid.iteye.com/blog/646760
版权声明:本文博客原创文章,博客,未经同意,不得转载。
由Maximum Gap,对话桶排序,基数排序和统计排序的更多相关文章
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...
- Maximum Gap 典型线性排序
https://leetcode.com/problems/maximum-gap/ Given an unsorted array, find the maximum difference betw ...
- Python线性时间排序——桶排序、基数排序与计数排序
1. 桶排序 1.1 范围为1-M的桶排序 如果有一个数组A,包含N个整数,值从1到M,我们可以得到一种非常快速的排序,桶排序(bucket sort).留置一个数组S,里面含有M个桶,初始化为0.然 ...
- 桶排序/基数排序(Radix Sort)
说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
随机推荐
- C++学习笔记(达内视频版)
达内C++(陈宗权主讲) 第一天: 课程分为Core C++(标准C++.不依赖操作系统)和Unix C++. 1.配置bash,运行.sh文件. vi bash_profile 在"pat ...
- php课程 8-28 php如何绘制生成显示图片
php课程 8-28 php如何绘制生成显示图片 一.总结 一句话总结:gd库轻松解决 1.php图片操作生成的图的两种去向是什么? 一种在页面直接输出,一种存进本地磁盘 2.php操作图片的库有哪些 ...
- 【Codeforces Round #439 (Div. 2) A】The Artful Expedient
[链接] 链接 [题意] [题解] 暴力 [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #include <bits/stdc++.h> using namespa ...
- 大数据(十四) - Storm
storm是一个分布式实时计算引擎 storm/Jstorm的安装.配置.启动差点儿一模一样 storm是twitter开源的 storm的特点 storm支持热部署,即时上限或下线app 能够在st ...
- POJ 1287 Networking (ZOJ 1372) MST
http://poj.org/problem?id=1287 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=372 和上次那题差 ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- 【solr专题之三】Solr常见异常 分类: H4_SOLR/LUCENCE 2014-07-19 10:30 3223人阅读 评论(0) 收藏
1.RemoteSolrException: Expected mime type application/octet-stream but got text/html 现象: SLF4J: Fail ...
- 解决Eclipse中文乱码 分类: B1_JAVA 2014-06-25 11:23 336人阅读 评论(0) 收藏
使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置编码 格式解决乱码问题.在Eclipse可以从几个层面设置编 ...
- linux下dd命令详解及应用实例
名称: dd使用权限: 任何使用者dd 这个指令在 manual 里的定义是 convert and copy a file使用方式:dd [option]查看帮助说明dd --help或是info ...
- iOS 一个简单的单例
比如我有一个Singleton的类(DemoStatusManage),他有一个实例方法currentStatus会返回一个1-100的随机数. @interface DemoStatusManage ...