前端学习

哈希&计数排序和桶排序&基数排序

2018-12-13  本文已影响0人  本来无一物_f1f2
哈希index2 == max(67)是可以出现的,所以index2<68

length在大部分语言里是最大的数字下标+1

完整的计数排序

a <- {'0':0, '1':2,'.':56,'4':4,'5':67,'6':3,
'length':67}
hash<-{}
index <- 0
while(index < a['length'])
    number = a[index]
    if hash[number] == undefined //hash[number] 不存在
        hash[number] = 1
        else // hash[number]存在
        hash[number] = hash[number] + 1
    end
    index <- index + 1
end
index2 <- 0
max <- findmax(a)
index2 <- 1
while index < findmax['length']
    if findmax[index2] < max
        max <- findmax[index2]
    end
    index2 <- index2 + 1
end
nawarr <- {}
while(index2 < max + 1)
    count=hash[index2]
    if count !=undefined
        countIndex = 0
        while(countIndex < count)
            newArr.push(index2)
            countIndex <- countIndex + 1
        end
    end
    index2 <- index2 + 1
end
print newArr

桶排序

桶排序是计数排序的升级版
排序,要么浪费时间,要么浪费空间
桶排序里可以进行二次排序


桶排序

基数排序

基数排序适合万位数以下,适应与大基数的排序
基数排序就是10进制排序
出桶和入桶顺序顺序
先按照个位数进行排序
十位数入桶
按照十位数进行排序
百位数入桶
按照百位数进行排序
千位数入桶
按照千位数进行排序
万位数入桶
按照万位数进行排序

上一篇 下一篇

猜你喜欢

热点阅读