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个下标。

// 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 

计数排序与桶排序(bucket sort)的更多相关文章

  1. 计数排序和桶排序(Java实现)

    目录 比较和非比较的区别 计数排序 计数排序适用数据范围 过程分析 桶排序 网络流传桶排序算法勘误 桶排序适用数据范围 过程分析 比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比 ...

  2. 计数排序与桶排序python实现

    计数排序与桶排序python实现 计数排序 计数排序原理: 找到给定序列的最小值与最大值 创建一个长度为最大值-最小值+1的数组,初始化都为0 然后遍历原序列,并为数组中索引为当前值-最小值的值+1 ...

  3. 计数排序、桶排序python实现

    计数排序在输入n个0到k之间的整数时,时间复杂度最好情况下为O(n+k),最坏情况下为O(n+k),平均情况为O(n+k),空间复杂度为O(n+k),计数排序是稳定的排序. 桶排序在输入N个数据有M个 ...

  4. 【JS面试向】选择排序、桶排序、冒泡排序和快速排序简介

    新年伊始,又到了金三银四的时候了.面对前端越来越多的算法面试题,我简单的整理了一下几种比较常见的数组排序方式,分别介绍其基本原理和优劣势.(ps:才疏学浅,希望大家可以在issues下面指出问题) 选 ...

  5. 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)

    前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...

  6. 使用 js 实现十大排序算法: 桶排序

    使用 js 实现十大排序算法: 桶排序 桶排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  7. 排序:桶排序Bucket sort

    补充说明三点 1,桶排序是稳定的 2,桶排序是常见排序里最快的一种,比快排还要快…大多数情况下 3,桶排序非常快,但是同时也非常耗空间,基本上是最耗空间的一种排序算法 无序数组有个要求,就是成员隶属于 ...

  8. java-数组排序--计数排序、桶排序、基数排序

    计数排序引入 不难发现不论是冒泡排序还是插入排序,其排序方法都是通过对每一个数进行两两比较进行排序的,这种方法称为比较排序,实际上对每个数的两两比较严重影响了其效率,理论上比较排序时间复杂度的最低下限 ...

  9. 排序基础之非比较的计数排序、桶排序、基数排序(Java实现)

    转载请注明原文地址: http://www.cnblogs.com/ygj0930/p/6639353.html  比较和非比较排序 快速排序.归并排序.堆排序.冒泡排序等比较排序,每个数都必须和其他 ...

随机推荐

  1. Django_rest_framework_组件(authentication、permission、throttle)

    认证组件 说明 from rest_framework.authentication import BaseAuthentication class TestAuthentication(BaseAu ...

  2. “Hello World!”团队第七周召开的第六次会议

    博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八 .功能说明书 一.会议时间 2017年12月6日  11:20-12:00 二 ...

  3. Task 5.1 电梯调度程序需求调研报告

    1.任务概述: 1.1任务背景:试想一下,石家庄铁道大学基础教学楼的电梯配置如下:大厦有18层, 4部电梯,很多乘客使用这些电梯的日常(旅客重量:平均70公斤最大120公斤,最小45公斤).其他常量数 ...

  4. [不明所以]android 5.0 couldn't find "libmsc.so"

    用5.0 mi2调试的时候 search那边不行, 出现...couldn't find "libmsc.so" 我这边情况的解决方法是 在armeabi的libmsc.so复制一 ...

  5. “吃神么,买神么”的第一个Sprint计划(第五天)

    “吃神么,买神么”项目Sprint计划 ——5.25  星期一(第五天)立会内容与进度 摘要:logo2出来了,修改过不一样的风格,组内总体评价可以,但是颜色要改,色调没注意,统一决定改成与背景色一致 ...

  6. BETA-3

    前言 我们居然又冲刺了·三 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 一堆deadline截至前的两天,为了图形学和编译原理毅然决然地放弃冲刺 接下 ...

  7. week4f:个人博客作业

    8,工作中的照片 9,对方编程习惯总结 宋成鑫(以后简称老宋).老宋,对编程的思想看的比较重,具体什么是编程的思想,我是也不是很清楚.但是,在编程过程中,老宋的一些话给了我启示.这或许就是编程的思想吧 ...

  8. 怎样利用好单片机上的存储器资源来实现OD的存储与访问

    转自:http://www.cnblogs.com/winshton/p/4897789.html 我们知道OD(对象字典)是CANopen的核心,所有功能都是围绕它开展的,是协议栈的数据中心,良好的 ...

  9. vue-Slot分发内容

    ①概述: 简单来说,假如父组件需要在子组件内放一些DOM,那么这些DOM是显示.不显示.在哪个地方显示.如何显示,就是slot分发负责的活. ②默认情况下 父组件在子组件内套的内容,是不显示的. 例如 ...

  10. shutdown&&isTerminated

    shutdownvoid shutdown()启动一次顺序关闭,执行以前提交的任务,但不接受新任务.若已经关闭,则调用没有其他作用.抛出:SecurityException - 如果安全管理器存在并且 ...