我有一个单词列表, array = ['alien','and','alien','aggressive','alien','creature','and']
但是,每次我对它进行排序时,我都会 array = ['aggressive',' alien ',' and ',' and ',' alien ' ,“ 外星人”,“生物”] 这是错误的,因为“外星人”用“和”分隔(相同的单词应分组在一起)。
有人可以指出我的基数排序问题是什么给我这个问题吗?预先感谢!
def wordSort(word_list):
max_len_word = find_max_char(word_list)
radix_sort(word_list, max_len_word)
def find_max_char(word_list):
max = 0
for word in word_list:
if max < len(word):
max = len(word)
return max
def radix_pass(array, base, digit):
counter = [0 for i in range(base-1)]
for i in range(1, len(array)):
counter[get_digit(array[i], digit, True)] += 1
position = [0 for i in range(base)]
for value in range(base):
position[value] = position[value-1] + counter[value-1]
temp = [0 for i in range(len(array))]
for i in range(len(array)):
temp[position[get_digit(array[i], digit, True)]] = array[i]
position[get_digit(array[i], digit, True)] += 1
return temp
def get_digit(array_i, digit, boolean):
if boolean == True:
if len(array_i) - 1 < digit:
return 0
else:
return ord(array_i[digit]) - 96
def radix_sort(array, digit):
base = 27
for i in range(digit-1, -1, -1):
sorted_array = radix_pass(array, base, i)
return sorted_array