In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of bucket sorting in web", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample analysis of bucket sorting in web".
Bucket sorting is to store the elements in the same range in the set to be sorted into the same bucket, that is, to split the set into multiple regions according to the value characteristics of the elements, then the multiple buckets formed after the split are in an ordered state from the point of view of the value range. Sorts the elements in each bucket, the collection of elements in all buckets is sorted.
To make bucket sorting more efficient, we need to do these two things:
When the extra space is sufficient, the mapping function used to maximize the number of buckets can evenly distribute the N input data to K buckets.
At the same time, for the sorting of elements in the bucket, the choice of comparison sorting algorithm is very important to the performance. 1. When is the fastest time when the input data can be evenly distributed to each bucket. two。 When is the slowest time when the input data is allocated to the same bucket. 3. Schematic elements are distributed in buckets: then, elements are sorted in each bucket:
Code implementation of JavaScript
Example
Function bucketSort (arr, bucketSize) {if (arr.length = 0) {return arr;} var i; var minValue = arr [0]; var maxValue = arr [0]; for (I = 1; I if (arr [I] else if (arr [I] > maxValue) {maxValue = arr [I]; / / maximum of input data}} / / initialization of the bucket var DEFAULT_BUCKET_SIZE = 5 / / set the default number of buckets to 5 bucketSize = bucketSize | | DEFAULT_BUCKET_SIZE; var bucketCount = Math.floor ((maxValue-minValue) / bucketSize) + 1; var buckets = new Array (bucketCount); for (I = 0; I for (var j = 0; j return arr;} Java)
Example
Public class BucketSort implements IArraySort {private static final InsertSort insertSort = new InsertSort (); @ Override public int [] sort (int [] sourceArray) throws Exception {/ / copy a pair of arr without changing the parameter content 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 else if (value > maxValue) {maxValue = value;}} int bucketCount = (int) Math.floor ((maxValue-minValue) / bucketSize) + 1; int [] [] buckets = new int [bucketCount] [0] / / use the mapping function to assign the data to each bucket for (int I = 0; I for (int [] bucket: buckets) {if (bucket.length continue;} / / sort each bucket), where the insertion sort bucket = insertSort.sort (bucket) is used. For (int value: bucket) {arr [arrIndex++] = value;}} return arr;} / * automatically expand capacity and save 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
Example
Function bucketSort ($arr, $bucketSize = 5) {if (count ($arr) = 0) {return $arr;} $minValue = $arr [0]; $maxValue = $arr [0]; for ($I = 1; $I $arr); $iTunes +) {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 $buckets); $buckets [$I] = [];} for ($I = 0; $I $arr); $buckets +) {$buckets [floor (($arr [$I]-$minValue) / $bucketSize)] [] = $arr [$I];} $arr = array (); for ($I = 0; $I $buckets) $buckets +) {$bucketTmp = $buckets [$I]; sort ($bucketTmp); for ($j = 0; $j $bucketTmp); $arr +) {$arr [] = $bucketTmp [$j];}} return $arr;} C++
Example
# include#include#includeusing namespace std;const int BUCKET_NUM = 10 ListNode* insert struct ListNode {explicit ListNode (int iTun0): 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 blank curr & & curr- > mDatamNext;} newNode- > mNext = curr; pre- > mNext = newNode; return dummyNode.mNext;} ListNode* Merge (ListNode* head1,ListNode * head2) {ListNode dummyNode; ListNode* dummy = & dummyNode; while (Null > mData mData) {dummy- > mNext = head1) {if Head1 = head1- > mNext;} else {dummy- > mNext = head2; head2 = head2- > mNext;} dummy = dummy- > mNext;} if dummy- > mNext = head1; if dummy- > mNext = head2; return dummyNode.mNext } void BucketSort (int njinint arr []) {vector buckets (BUCKET_NUM, (ListNode*) (0)); for (int item0 political data; head = head- > mNext;}}) these are all the contents of the article "sample analysis of bucket sorting in web". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.