一些非比较排序

在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,对话桶排序,基数排序和统计排序的更多相关文章

  1. 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较

    2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...

  2. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  3. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  4. Maximum Gap 典型线性排序

    https://leetcode.com/problems/maximum-gap/ Given an unsorted array, find the maximum difference betw ...

  5. Python线性时间排序——桶排序、基数排序与计数排序

    1. 桶排序 1.1 范围为1-M的桶排序 如果有一个数组A,包含N个整数,值从1到M,我们可以得到一种非常快速的排序,桶排序(bucket sort).留置一个数组S,里面含有M个桶,初始化为0.然 ...

  6. 桶排序/基数排序(Radix Sort)

    说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...

  7. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

  9. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

随机推荐

  1. poj1564 Sum It Up (zoj 1711 hdu 1258) DFS

    POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...

  2. 将OpenCV捕获的摄像头加载到picture控件中

    CRect rect; CStatic* pStc; CDC* pDC; HDC hDC; pStc = (CStatic*)GetDlgItem(IDC_CAM);//IDC_CAM是Picture ...

  3. java I/O库的设计模式

    在java语言 I/O库的设计中,使用了两个结构模式,即装饰模式和适配器模式.      在任何一种计算机语言中,输入/输出都是一个很重要的部分.与一般的计算机语言相比,java将输入/输出的功能和使 ...

  4. [ES7] Await multi promises sequentially or concurrently

    Somtime 'async await' can have a bad effect on code proferemence. Let's take a look the below exampl ...

  5. Qt5.4.2编译 qtpropertybrowser-2.51(从4修改到5的除错过程)

    qtpropertybrowser是一个洛基亚时代的一个产物,其实它就是QtDesigner侧栏那种样子而已.网上大部分的都只适用于Qt4,今天头脑发热想用Qt5编译一下,发觉一大堆错误,因为Qt4有 ...

  6. leveldb学习:Versionedit和Versionset

    VersionEdit: compact过程中会有一系列改变当前Version的操作(FileNumber添加.删除input的sstable,添加输出的sstable).为了缩小version切换的 ...

  7. Oracle 自己主动内存參数依赖性

    图例:在该图中使用了下面參数名称缩写: MT = MEMORY_TARGET MMT = MEMORY_MAX_TARGET ST = SGA_TARGET PAT = PGA_AGGREGATE_T ...

  8. python 多线程拷贝单个文件

    # -*- coding: utf-8 -*- # @author: Tele # @Time : 2019/04/04 下午 12:25 # 多线程方式拷贝单个文件 import threading ...

  9. tip of Firefox extention foxyproxy

    tip of Firefox extention foxyproxy

  10. Misultin, Mochiweb, Cowboy, NodeJS 及 Tornadoweb测评

    http://www.oschina.net/translate/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb ...