计数排序与桶排序(bucket sort)

时间:2022-06-18 00:09:13

Bucket Sort is a sorting method that subdivides the given data into various buckets depending on certain characteristic order, thus
partially sorting them in the first go.
Then depending on the number of entities in each bucket, it employs either bucket sort again or some other ad hoc sort.

BUCKET SORT (A)
1. n ← length [A]
2. For i = 1 to n do
3. Insert A[i] into list B[A[i]/b] where b is the bucket size
4. For i = 0 to n-1 do
5. Sort list B with Insertion sort
6. Concatenate the lists B[0], B[1], . . B[n-1] together in order.
Assumptions  from geeksforgeeks:

桶排序主要用于一定区间内的数据的排序,比如学生成绩[0~100]。

比如数组arr[10],是一组小数{0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68},下图A代表数组arr,二维数组B的每一行代表存储的一定区间的数据,这里下标为arr[i]*10,其中i为数组第i个下标。

计数排序与桶排序(bucket sort)

// C++ program to sort an array using bucket sort
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

// Function to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{
    // 1) Create n empty buckets
    vector<float> b[n];//相当于b[n][N]二维数组,N为任意正整数。

    // 2) Put array elements in different buckets
    ; i<n; i++)
    {
       int bi = n*arr[i]; // Index in bucket
       b[bi].push_back(arr[i]);
    }

    // 3) Sort individual buckets。排序一个桶内的数据(一个桶实际是一个区间)
    ; i<n; i++)
       sort(b[i].begin(), b[i].end());

    // 4) Concatenate all buckets into arr[]拼接所有桶内的数据。
    ;
    ; i < n; i++)
        ; j < b[i].size(); j++)
          arr[index++] = b[i][j];
}

/* Driver program to test above funtion */
int main()
{
    float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
    ]);
    bucketSort(arr, n);

    cout << "Sorted array is \n";
    ; i<n; i++)
       cout << arr[i] << " ";
    cout << endl;
    ;
}

如果把每个元素看成是一个桶,那么计数排序就是桶排序的特化.

Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.

Let us understand it with the help of an example.

For simplicity, consider the data in the range 0 to 9.
Input data: 1, 4, 1, 2, 7, 5, 2
  1) Take a count array to store the count of each unique object.
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  2  0   1  1  0  1  0  0

  2) Modify the count array such that each element at each index
  stores the sum of previous counts.
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  4  4  5  6  6  7  7  7

The modified count array indicates the position of each object in
the output sequence.

  3) Output each object from the input sequence followed by
  decreasing its count by 1.
  Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 2.
  Put data 1 at index 2 in output. Decrease count by 1 to place
  next data 1 at an index 1 smaller than this index.
// C++ Program for counting sort
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define RANGE 255  

// The main function that sort
// the given string arr[] in
// alphabatical order
void countSort(char arr[])
{
    // The output character array
    // that will have sorted arr
    char output[strlen(arr)];  

    // Create a count array to store count of inidividul
    // characters and initialize count array as 0
    ], i;
    memset(count, , sizeof(count));  

    // Store count of each character
    ; arr[i]; ++i)
        ++count[arr[i]];  

    // Change count[i] so that count[i] now contains actual
    // position of this character in output array
    ; i <= RANGE; ++i)
        count[i] += count[i-];  

    // Build the output character array
    ; arr[i]; ++i)
    {
        output[count[arr[i]]-] = arr[i];
        --count[arr[i]];
    }  

    /*
    For Stable algorithm
    for (i = sizeof(arr)-1; i>=0; --i)  // 逆序
    {
        output[count[arr[i]]-1] = arr[i];
        --count[arr[i]];
    }  

    For Logic : See implementation
    */

    // Copy the output array to arr, so that arr now
    // contains sorted characters
    ; arr[i]; ++i)
        arr[i] = output[i];
}  

// Driver  code
int main()
{
    char arr[] = "geeksforgeeks"; 

    countSort(arr);  

    cout<< "Sorted character array is " << arr;
    ;
}  

// This code is contributed by rathbhupendra