Wu Yuxiong - born natural data structure: Ten classic sort algorithm - bucket sort

Bucket sort is counting sequencing upgrade. It uses the function mapping relationship, the key lies in whether or not effectively determine the mapping function. In order to make more efficient bucket sort, we need to do two things: 

at adequate additional space, as far as increasing the number of buckets 
N evenly allocated data mapping function can be used to input the K bucket 
Meanwhile, Sort bucket elements, choose which sort algorithm compares crucial for the performance. 
When the fastest 
data even when the input can be assigned to each barrel. 
When the slowest 
data when the input is assigned to the same bucket.
Code for 
the JavaScript 
function bucketSort (ARR, bucketSize) { 
    IF (arr.length === 0) {
       return ARR; 
    } 

    var I; 
    var the minValue = ARR [0]; 
    var maxValue = ARR [0];
     for (I =. 1 ; I <arr.length; I ++ ) {
       IF (ARR [I] < the minValue) { 
          the minValue = ARR [I]; // minimum value of input data 
      } the else  IF (ARR [I]> maxValue) { 
          maxValue = ARR [ i]; // input maximum data 
      } 
    }

    // tub initialization 
    var DEFAULT_BUCKET_SIZE =. 5; // number of default settings for the tub. 5 
    bucketSize = bucketSize || DEFAULT_BUCKET_SIZE; 
    var bucketCount = Math.floor ((maxValue - the minValue) / bucketSize) +. 1 ;   
    var buckets = new new the Array ( bucketCount);
     for (I = 0; I <buckets.length; I ++ ) { 
        buckets [I] = []; 
    }

     // use of a mapping function data assigned to each tub
     for (I = 0; I <arr.length ; I ++ ) { 
        buckets [Math.floor ((ARR [I] - the minValue) / bucketSize)] Push (ARR [I]);. 
    }

    arr.length = 0;
     for (I = 0; I <buckets.length; I ++ ) { 
        InsertionSort (buckets [I]);                       // sort each bucket, as used herein, insertion sort
         for (var J = 0; J <buckets [I] .length; J ++ ) { 
            arr.push (buckets [I] [J]);                       
        } 
    } 

    return ARR; 
}
Java
public class BucketSort implements IArraySort {

    private static final InsertSort insertSort = new InsertSort();

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        return bucketSort(arr, 5);
    }

    private int[] bucketSort(int[] arr, int bucketSize) throws Exception {
        if (arr.length == 0) {
            return arr;
        }

        int minValue = arr[0];
        int maxValue ) {= ARR [0];
         for (int value: ARR) {
             IF (value < the minValue) { 
                the minValue = value; 
            } the else  IF (value> maxValue) { 
                maxValue = value; 
            } 
        } 

        int bucketCount = (int) Math.floor ( (maxValue - the minValue) / bucketSize) +. 1 ; 
        int [] [] buckets = new new int [bucketCount] [0];

         // use of a mapping function data assigned to each tub
         for (int I = 0; I <ARR. length; I ++
            index int = (int) Math.floor ((ARR [I] - the minValue) / bucketSize); 
            buckets [index] = arrAppend (buckets [index], ARR [I]); 
        } 

        int arrIndex = 0;
         for (int [ ] bucket: buckets) {
             IF (bucket.length <= 0) {
                 Continue ; 
            }
             // sort each bucket, insertion sort used herein 
            bucket = insertSort.sort (bucket);
             for (int value: bucket) { 
                ARR [arrIndex ++] = value; 
            } 
        }

        return  ARR;
    }

     / ** 
     * automatic expansion, and save the data
      * 
     * @param ARR
      * @param value
      * / 
    Private int [] arrAppend (int [] ARR, int value) { 
        ARR = Arrays.copyOf (ARR, arr.length + . 1 ); 
        ARR [arr.length -. 1] = value;
         return ARR; 
    } 

}
PHP
function bucketSort($arr, $bucketSize = 5)
{
    if (count($arr) === 0) {
      return $arr;
    }

    $minValue = $arr[0];
    $maxValue = $arr[0];
    for ($i = 1; $i < count($arr); $i++) {
      if ($arr[$i] < $minValue) {
          $minValue = $arr[$i];
      } else if ($arr[$i] > $maxValue) {
          $maxValue = $arr[$i];
      }
    }

    $bucketCount = floor(($maxValue - $minValue) / $bucketSize) + 1;
    $buckets = array();
    for ($i = 0; $i < count($buckets); $i++) {
        $buckets[$i] = [];
    }

    for ($i = 0; $i < count($arr); $i++) {
        $buckets[floor(($arr[$i] - $minValue) / $bucketSize)][] = $arr[$i];
    }

    $arr = array();
    for ($i = 0; $i < count($buckets); $i++) {
        $bucketTmp = $buckets[$i];
        sort($bucketTmp);
        for ($j = 0; $j < count($bucketTmp); $j++) {
            $arr[] = $bucketTmp[$j];
        }
    }

    return $arr;
}
C++
#include<iterator>
#include<iostream>
#include<vector>
using namespace std;
const int BUCKET_NUM = 10;

struct ListNode{
        explicit ListNode(int i=0):mData(i),mNext(NULL){}
        ListNode* mNext;
        int mData;
};

ListNode* insert(ListNode* head,int val){
        ListNode dummyNode;
        ListNode *newNode = new ListNode(val);
        ListNode *pre,*curr;
        dummyNode.mNext = head;
        pre = &dummyNode;
        curr = head;
        while(NULL!=curr && curr->mData<=val){
                pre = curr;
                curr = curr->mNext;
        }
        newNode->mNext = curr;
        pre->mNext = newNode;
        return dummyNode.mNext;
}


ListNode* Merge(ListNode *head1,ListNode *head2){
        ListNode dummyNode;
        ListNode *dummy = &dummyNode;
        while(NULL!=head1 && NULL!=head2){
                if(head1->mData <= head2->mData){
                        dummy->mNext = head1;
                        head1 = head1->mNext;
                }else{
                        dummy->mNext = head2;
                        head2 = head2->mNext;
                }
                dummy = dummy->mNext;
        }
        if(NULL!=head1) dummy->mNext = head1;
        if(NULL!=head2) dummy->mNext = head2;
       
        return dummyNode.mNext;
}

void BucketSort(int n,int arr[]){
        vector<ListNode*> buckets(BUCKET_NUM,(ListNode*)(0));
        for(int i=0;i<n;++i){
                int index = arr[i]/BUCKET_NUM;
                ListNode *head = buckets.at(index);
                buckets.at(index) = insert(head,arr[i]);
        }
        ListNode *head = buckets.at(0);
        for(int i=1;i<BUCKET_NUM;++i){
                head = Merge(head,buckets.at(i));
        }
        for(int i=0;i<n;++i){
                arr[i] = head->mData;
                head = head->mNext;
        }
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/11973839.html