@zqbinggong
2018-03-10T15:10:26.000000Z
字数 2650
阅读 995
决策树模型
计数排序
基数排序
桶排序
算法导论
Counting-Sort(A,B,k)——
let C[0...k] be a new array with each element equal to 0
for j = 1 to A.length
C[A[j]] = C[A[j]] + 1 #让C[i]表示A中值等于i的元素的个数
for i = 1 to k
C[i] = C[i] + C[i-1] # 让C[i]表示A中值不大于i的元素的个数
for j = A.length to 1 # 反序以保持稳定性
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] - 1
Bucket-Sort(A)——
n = A.length
let B[0...n-1] be a new array
for i = 0 to n-1
make B[i] an empty list
for i = 1 to n
insert A[i] into list B[nA[i]] #向下取整
for i = 1 to n-1
sort list with insertion sort
contatenate the lists together in order
原址但不稳定
Counting-Sort-inPlace(A,k)
let C[0...k] be a new array with each element equal to 0
for j = 1 to A.length
C[A[j]] = C[A[j]] + 1 #让C[i]表示A中值等于i的元素的个数
p = 0 # 标记将要改写数据的位置
for i = 0 to k
for j = 0 to C[i]
p = p + 1
A[p] = i
a. You are given an array of integers, where different integers may have different numbers of digits, but the total number of digits over all the integers in the array
is n. Show how to sort the array in time.
1. counting every number's digits --
2. sort the integers by number of digits by using counting-sort --
3. use radix-sort to sort each group of integers with the same digits
b. You are given an array of strings, where different strings may have different
numbers of characters, but the total number of characters over all the strings
is n. Showhowto sort the strings in time.
(Note that the desired order here is the standard alphabetical order; for example,
dfuioasdfiousiodf
1. using counting-sort to sort the strings bt first letter--
2. putting the strings with the same first letter into one group and remove the first letter
3. using counting-sort to sort each group on the way of 1 and reapeat until each group has only one string
Match-Jugs(R,B)
if |R| = 0
error "empty"
else if |R| = 1
let R = {r}, B = {b}
output (r,b)
return
else
r = random(R)
B+,B-,b
R+,R-,r
output (r,b)
Match-Jugs(R+,B+)
Match-Jugs(R-,B-)
思路2:
用数组,加上快排的思想。具体做法就是,同时对两个数组进行partition,两者是用的主元相同;同时还需要再partition过程中增加根据主元的值寻找主元在数组中的位置这一操作,为此额外操作付出的时间代价为,
值得注意的是,这里很容易出错的地方在于,快速排序中的partition,在确定主元后,应立即将主元换到数组的尾端,否则就会出错,因而在这里需要加上在数组中找值的位置这一额外操作
思路
1. 将n个数分成k个子数组,对每个子数组进行排序——
2. 对每个子数组,如,将插入到的位置