简体   繁体   中英

Radix Sort for Strings by length and alphabetical order

I am trying to implement Radix Sort for string, however I managed only to do it by the string length. I would like to sort the array of strings by both, the length and alphabetical order. Is it even possible to do it with Radix Sort?

This is my code:

def flatten(arr):
    flatten_arr = []
    for item_arr in arr:
        for item in item_arr:
            flatten_arr.append(item)
    return flatten_arr


count_size = 256


def get_max_length(book_content_arr):
    size = 0
    for word in book_content_arr:
        word_size = len(word)
        if word_size > size:
            size = word_size
    return size


def radix_sort(arr):
    word_length = get_max_length(arr)
    for index in range(0, word_length):
        buckets = [[] for i in range(count_size)]
        for item in arr:
            if len(item) > index:
                num = ord(item[index])
                buckets[num].append(item)
            else:
                buckets[0].append(item)

        arr = flatten(buckets)
    return arr
    
    
example = ["A", "Z", "AB", "EWASADAS", "BY", "SDA" "ZA", "BD", "BA", "DSADSA", "BZ", "KA", "ES"]
print(radix_sort(example))

My example array is like this

example = ["A", "Z", "AB", "EWASADAS", "BY", "SDA" "ZA", "BD", "BA", "DSADSA", "BZ", "KA", "ES"]

And the expected output:

["A", "Z", "AB", "BD", "BY", "BZ", "ES", "KA", "DSADSA", "EWASADAS"]

Seems like I was able to sort it out.

I just put the words into buckets according to their size, then I sort each of the bucket using just regular radix sort with counting sort as subroutine, and then I flatten the array.

I'm posting the code, so that it might be useful for somebody

signs = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]  
def flatten(arr):
    flatten_arr = []
    for item_arr in arr:
        for item in item_arr:
            flatten_arr.append(item)
    return flatten_arr


def get_max_length(arr):
    size = 0
    for word in arr:
        word_size = len(word)
        if word_size > size:
            size = word_size
    return size

def counting_sort_for_letters(arr, index):
    count = [0] * len(signs)
    output = [0] * len(arr)

    for item in arr:
        idx = signs.index(item[index])
        count[idx] += 1

    for i in range(1, len(count)):
        count[i] += count[i - 1]

    for j in range(len(arr) - 1, -1, -1):
        idx = signs.index(arr[j][index])
        count[idx] -= 1
        output[count[idx]] = arr[j]
    return output


def radix_sort(arr, world_length):
    for i in range(world_length - 1, -1, -1):
        arr = counting_sort_for_letters(arr, i)
    return arr
    

def custom_sort(arr):
    word_length = get_max_length(arr)
    buckets = [[] for i in range(word_length)]
    for item in arr:
        num = len(item) - 1
        buckets[num].append(item)
    for j in range(0, len(buckets)):
        buckets[j] = radix_sort(buckets[j], j + 1)
        
    arr = flatten(buckets)
    return arr
    
    
example = ["A", "Z", "AB", "EWASADAS", "BY", "SDA" "ZA", "BD", "BA", "DSADSA", "BZ", "KA", "ES"]
print(custom_sort(example))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM