【CLRS】《算法导论》读书笔记(三):计数排序(Counting sort)、基数排序(Radix sort)和桶排序(Bucket sort)
2013-03-14 21:25
656 查看
计数排序(Counting sort)
维基百科:http://en.wikipedia.org/wiki/Counting_sort
时间复杂度:O(n)
技术排序的基本思想:对每一个输入元素 x ,确定小于 x 的元素个数。利用这一信息,就可以直接把 x 放到它在输出数组中的位置上了。
伪码:
COUNTING-SORT(A, B, k)
let C[0 .. k] be a new array
for i = 0 to k
C[i] = 0
for j = 1 to A.length
C[A[j]] = C[A[j]] + 1
// C[i] now contains the number of elements equal to i.
for i = 1 to k
C[i] = C[i] + C[i - 1]
// C[i] now contains the number of elements less than or equal to i.
for j = A. length downto 1
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] - 1
基数排序(Radix sort)
维基百科:http://en.wikipedia.org/wiki/Radix_sort
最差时间复杂度:O(k n),其中k为位数
基数排序分类:LSD(Least Significant Digital)和MSD(Most Signifacant Digital)
LSD伪码:
RADIX-SORT(A, d)
for i = 1 to d
use a stable sort to sort array A on digit i
桶排序(Bucket sort)
维基百科:http://en.wikipedia.org/wiki/Bucket_sort
平均时间复杂度:O(n)
Bucket sort works as follows:
Set up an array of initially empty "buckets."
Scatter: Go over the original array, putting each object in its bucket.
Sort each non-empty bucket.
Gather: Visit the buckets in order and put all elements back into the original array.
伪码:
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 = 0 to n -1
sort list B[i] with insertion sort
concatenate the list B[0], B[1], ..., B[n -1] together in order
维基百科:http://en.wikipedia.org/wiki/Counting_sort
时间复杂度:O(n)
技术排序的基本思想:对每一个输入元素 x ,确定小于 x 的元素个数。利用这一信息,就可以直接把 x 放到它在输出数组中的位置上了。
伪码:
COUNTING-SORT(A, B, k)
let C[0 .. k] be a new array
for i = 0 to k
C[i] = 0
for j = 1 to A.length
C[A[j]] = C[A[j]] + 1
// C[i] now contains the number of elements equal to i.
for i = 1 to k
C[i] = C[i] + C[i - 1]
// C[i] now contains the number of elements less than or equal to i.
for j = A. length downto 1
B[C[A[j]]] = A[j]
C[A[j]] = C[A[j]] - 1
基数排序(Radix sort)
维基百科:http://en.wikipedia.org/wiki/Radix_sort
最差时间复杂度:O(k n),其中k为位数
基数排序分类:LSD(Least Significant Digital)和MSD(Most Signifacant Digital)
LSD伪码:
RADIX-SORT(A, d)
for i = 1 to d
use a stable sort to sort array A on digit i
桶排序(Bucket sort)
维基百科:http://en.wikipedia.org/wiki/Bucket_sort
平均时间复杂度:O(n)
Bucket sort works as follows:
Set up an array of initially empty "buckets."
Scatter: Go over the original array, putting each object in its bucket.
Sort each non-empty bucket.
Gather: Visit the buckets in order and put all elements back into the original array.
伪码:
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 = 0 to n -1
sort list B[i] with insertion sort
concatenate the list B[0], B[1], ..., B[n -1] together in order
相关文章推荐
- 计数排序(Counting Sort)、桶排序(Bucket Sort)和基数排序(Radix Sort)
- 基于非比较的排序:计数排序(countSort),桶排序(bucketSort),基数排序(radixSort)
- 基于非比較的排序:计数排序(countSort),桶排序(bucketSort),基数排序(radixSort)
- 计数排序(Counting Sort)与基数排序(Radix Sort)小结
- 桶排序(Bucket Sort),基数排序(Radix_Sort)
- 线性排序:计数排序 Counting Sort 和 基数排序 Radix Sort
- 线性时间的排序 - Decision Tree Model & Counting Sort & Radix Sort & Bucket Sort
- 【CLRS】《算法导论》读书笔记(一):堆排序(Heapsort)
- 排序之线性排序(counting sort, radix-sort, bucket-sort)
- 算法导论 第八章计数排序(counting sort)
- 算法导论 第八章计数排序(counting sort)
- radix sort 基数排序 和 counting sort 计数排序
- 算法导论 第八章基数排序(radix sort)
- 算法导论 第八章基数排序(radix sort)
- radix sort &&bucket sort
- Java排序算法(八)--基数排序(RadixSort)
- 桶排序(bucket sort)
- 内部排序之五:计数排序、基数排序和桶排序
- 计数排序,基数排序和桶排序
- 计数排序 (Counting Sort)