Python algorithm learning base sorting instance

Source: Internet
Author: User

The base sorting method is also called bucket sort or bin sort. As the name suggests, it distributes the elements to be sorted to some "buckets" based on some key-value information, base sorting is a stable sorting. the time complexity is O (nlog (r) m), where r is the base number, m indicates the number of heaps. In some cases, the base sorting method is more efficient than other comparative sorting methods.

Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-

Def _ counting_sort (A, I ):
"Is sorted by count, in the I-bit order, applicable to the base sorting.
Args:
A (Sequence): Sorting Array
I (int): The number of digits, starting from 0 instead of 1.
"""
C = [0] * 10 # the range of any bit value is []
A = [(a/(10 ** I) % 10, a) for a in A] # array of element I bit values and their tuples
For k, a in:
C [k] = C [k] + 1
For I in xrange (1, 10 ):
C [I] = C [I] + C [I-1]
B = [0] * len (A) # result Array
For k, a in A [:-1]:
B [C [k]-1] =
C [k] = C [k]-1
Return B

Def radix_sort (A, d ):
"Base sorting, from the ranking bit until the highest bit:
RADIX-SORT (A, d)
1 for I limit 1 to d
2 do use a stable sort to sort array A on digit I

Args:
A (Sequence): Sorting Array
D (int): Maximum number of digits
"""
For I in xrange (d): # Number of traversal digits, from low to high
A = _ counting_sort (A, I)
Return

Def rsort (A, d ):
"Base sorting (bucket sorting version )"""
For I in xrange (d): # Number of traversal digits, from low to high
S = [[] for _ in xrange (10)] # store the elements corresponding to the [0, 9] bits ([0-9] 10 buckets)
For a in A: # traverse Elements
S [a/(10 ** I) % 10]. append (a) # store the elements of the corresponding bit value (the bucket in which the current bit value of the element is stored)
A = [a for B in S for a in B] # A sorted by the current BIT (take the elements out of each bucket in sequence)
Return

If _ name _ = '_ main __':
Import random, timeit

Items = range (1, 10000)
Random. shuffle (items)

Def test_sorted ():
Print (items)
Sorted_items = sorted (items)
Print (sorted_items)

Def test_radix_sort ():
Print (items)
Sorted_items = radix_sort (items, 4) # [0, 9999], 4-digit
Print (sorted_items)

Test_methods = [test_sorted, test_radix_sort]
For test in test_methods:
Name = test. _ name _ # test. func_name
T = timeit. Timer (name + '()', 'From _ main _ import '+ name)
Print (name + 'takes time: % F' % t. timeit (1 ))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.