Sort radix sort python

ALGORITHM

By bit-wise comparison radix sort (typically started from the lowest bit) placed in the element according to the minimum bit number of the tub 10, when all the elements are thus once processed, in order from 0 to 9 in each bucket elements and then taken out (do not focus on other bits, only concerned with the current position) this completes the ordering of all elements of the lowest level, and then continue to repeat the above steps, know the highest level of all the elements have been dealt with.

 

Algorithm steps

Initialization barrel, a total of 10, were stored current bit elements 0-9

Starting last element, according to the last digit of a place it with the corresponding element. After each element in the list above operation, the tub starts from 0, the element taken out from the tub, thus completing a sort of median

Repeat the previous process, if we find elements of the highest level have been processed, they took him to add to the final result

 

Algorithm

The main problem is that the algorithm to get the current position

For positive numbers

(Element // divisor)% 10 # #divisor result is the current number of bits represent the current bit, a bit is 1, 10 is ten, one hundred # @ 100 is rounded down to the meaning

  The result is over-element // divisor 0 represents the actual result is less than 1, i.e., the current bit already zero

 

For negative

collection [j] // i == - 1 # Representative negative

  Get current position

(10-math.ceil (element / divisor )% 10)% 10 # math.ceil () is rounded up to 
# 10% final result is to prevent the front case 10 appears =

 

Algorithm

radix_sort3 DEF (Collection): 
    '' 'to write their own, the fastest, can take positive and negative numbers, consider whether you can be a negative number by a positive number to determine the abs into 
        the outer loop control carry, that is the lowest level of the first row, then row penultimate position .. up to the highest bit of each processing element, up to treatment, into the final result set 
        to traverse the inner loop control array elements 
        for each array element, and the first 20 minutes is greater than 0 to less than case, because it involves arithmetic logic find the lowest digit positive and negative sample sizes 
        for a positive number, the carry into the current at this time there are elements into temporary variables, the current carry this is the last one when placed on the final result set, explained in the corresponding version determination logic 0 
        negative almost above and also 
        later for the inner end, the temporary variable required to take out the elements' '' 
    result_negative = [] 
    result_positive = [ ] 
    divisor = [POW (10, I) for I in Range (10)] 
    for I in divisor: 
        bucket = [[] Range for J in (10)] 
        IF len (Collection) == 0: 
            BREAK 
        for J in Range (len (c ollection)): 
            IF Collection [J] // I> 0:
                bucket [(Collection [J] // I) 10%] .append (Collection [J]) 
                Continue 
            elif Collection [J] == 0 // I: 
                result_positive.append (Collection [J]) 
                Continue 
            # negative 
            # elif Collection [J] // I <-1: 
            # bucket [(10-Math.ceil (Collection [J] / I) 10%) 10%] .append (Collection [J]) 
            # Continue 
            # elif Collection [J] // i == - 1: # will bug, -100 / 100 = -1, then the final result is placed, but in fact this should not be a 
            # if math.ceil (collection [j] / i) == -. 1: 
            # bucket [(10-Math.ceil (Collection [J] / I) 10%) 10%] .append (Collection [J])  
            # Continue
            # result_negative.insert (0, Collection [J])
            #     continue
        collection=[]
        for k in bucket:
            if k:
                collection.extend(k)
    return result_negative+result_positive

 

Efficiency Analysis

Time Complexity: k-cycles with respect to the number of bits in each cycle there is a cycle, to be put on the N elements barrels, total circulation kN

 

Compared

 

Guess you like

Origin www.cnblogs.com/Gaoqiking/p/11402392.html