[CLRS] "Introduction to Algorithms" study notes (c): Counting Sort (Counting sort), radix sort (Radix sort) and sort barrels (Bucket sort) ...

Counting sequencing (Counting sort)

Wikipedia: http://en.wikipedia.org/wiki/Counting_sort

Time complexity: O (n)

 

The basic idea of ​​sorting techniques: for each input element x, x is smaller than the number of elements is determined. Using this information, it can be directly x into its position in the output array on.

 

Pseudo-code:

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 (Radix sort)

Wikipedia: http://en.wikipedia.org/wiki/Radix_sort

Worst time complexity: O (kn), where k is the number of bits

Radix sort Category: LSD (Least Significant Digital) and MSD (Most Signifacant Digital)

 

LSD pseudo-code:

RADIX-SORT(A, d)

  for i = 1 to d

    use a stable sort to sort array A on digit i

 

Bucket sort (Bucket sort)

Wikipedia: http://en.wikipedia.org/wiki/Bucket_sort

The average time complexity: O (n)

 

Bucket sort works as follows:

  1. Set up an array of initially empty "buckets."
  2. Scatter: Go over the original array, putting each object in its bucket.
  3. Sort each non-empty bucket.
  4. Gather: Visit the buckets in order and put all elements back into the original array.

 

Pseudo-code:

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 

 

Reproduced in: https: //www.cnblogs.com/dyingbleed/archive/2013/03/14/2956558.html

Guess you like

Origin blog.csdn.net/weixin_34392843/article/details/93301855