Questions raised:
The following data will be:
6, 8, 2, 3, 4, 0, 9, 1, 5,1
Arranged by small arrival.
Bucket sorting principle:
A bucket sort is also called a sort of count, simply by enumerating all the elements in the dataset in order, and then counting the number of occurrences of the element. Finally, the elements inside the dataset are output sequentially.
The sorting process is as follows:
First, the size of the initialized bucket
Consider each element in the dataset as a bucket, as the above problem shows, the original data range is between 0--9, so I need to have 10 buckets, such as
The first behavior initializes a count of 0, and the second behavior of each element.
Second, Count
Next read the first raw data is 6, then add 1 to the bucket labeled 6, such as:
When you read the next raw data to 8, add 1 to the bucket labeled 8, for example:
And so on, when you finally traverse all the raw data, the count of 10 buckets is as follows:
Third, output data
After the traversal count of the raw data is completed, the next step is to traverse the buckets and output the data:
Element 0 is counted as 1, then output 0,
Element 1 is counted as 2, then output 1 1,
Element 2 is counted as 1, then output 2,
Element 3 is counted as 1, then output 3,
Element 4 is counted as 1, then output 4,
Element 5 is counted as 1, then output 5,
Element 6 is counted as 1, then output 6,
Element 7 is counted as 0, the element is not output,
Element 8 is counted as 1, then output 8,
Element 9 is counted as 1, then output 9,
The final result output is: 0, 1, 1, 2, 3, 4, 5, 6, 8, 9
Code implementation
As can be seen from the above principle, bucket sequencing requires the following three steps:
1. Apply an array containing all the elements and initialize it.
2. Traverse the raw data and count.
3. Iterate through the individual array elements after the count is complete, outputting the data.
The following is the implementation of the Python code:
1 #!/usr/bin/env python2 #-*-Coding:utf8-*-3 4 classBucketsort (object):5 " "6 Self.datas: List of data to sort7 self.bucketsize: The size of the bucket (the range of data sets, such as bucketsize=10,8 indicates that the dataset has a range of 0-9)9 Self.result: Saving sorted resultsTen Self.bucket: Represents a bucket, which refers to all elements within a dataset One _sort (): Sort function A Show (): function to output results - - Usage: the Bucketsort (datas, size) or Bucketsort (datas), the default value for size is - - Bucketsort (datas). _sort () This is the start sort. - Bucketsort (datas). Show () so you can output the sorted results + " " - def __init__(Self, datas, size=100): +Self.datas =datas ASelf.bucketsize =size atSelf.result = [0 forIinchRange (len (datas))] -Self.bucket = [0 forIinchrange (self.bucketsize)] - - def_sort (self): - #read each element and count it in the corresponding position, when the element in the bucket is not 0 - #save it in result . in forNuminchSelf.datas: -Self.bucket[num] + = 1 toj =0 + forIinchRange (self.bucketsize): - while(Self.bucket[i]): theSELF.RESULT[J] =I *Self.bucket[i]-= 1 $J + = 1Panax Notoginseng - defShow (self): the Print "Resutl is:", + forIinchSelf.result: A PrintI, the Print "' + - $ if __name__=='__main__': $ Try: -Size = Raw_input ("Please input size (default=100):") - ifSize: theSize =int (size) -Datas = Raw_input ('Please input some number:')WuyiDatas =Datas.split () thedatas = [Int (datas[i]) forIinchRange (len (datas))] - exceptException: Wu Pass - ifSize: AboutBKS =Bucketsort (datas, size) $ Else: -BKS =Bucketsort (datas) - Bks._sort () -Bks.show ()
View Code
Summarize:
1. The advantage of barrel sequencing is that it is particularly fast and really fast! Very fast! Special block!
2. The disadvantage is the special consumption of resources, if the value of the data range is 0---1010, you need to apply for a size of 1010 array, think this is more memory space. Wide fear!
3. The program I wrote above is also only a demonstration, the loophole is very many, currently can only sort more than 0 integers.
Finally interested students can pay attention to my public number, can always be timely and convenient to read my article. *^_^*
Scan code follow or search number: King_diary
Python and bucket sorting