Download code

Jump to: navigation, search

Back to Bucket_sort_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

bucketsort.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Bucket_sort_(Python)?oldid=13661
14 
15 def bucketSort(arr):
16     count = [0] * len(arr)
17     for value in arr:
18         count[value] += 1
19     arr = []
20     for nr, amount in enumerate(count):
21         arr.extend([nr] * amount)
22     return arr
23 
24 
25 arr = [1,3,4,6,4,2,9,1,2,9]
26 
27 for val in arr:
28     print val,
29 print
30 
31 arr = bucketSort(arr)
32 
33 for val in arr:
34     print val,
35 print


hijacker
hijacker
hijacker
hijacker