高位优先字符串排序

func TestHeight(t *testing.T) {
    a := []string{
        "she",
        "sky",
        "selle",
        "sell",
        "by",
        "the",
        "an",
        "a",
    }

    N := len(a)
    fmt.Println(sortMSD(a, 0, N-1, 0))
}

var MAX_R int    //最大字母数字
var aux []string //结果数组

func sortMSD(a []string, lo int, hi int, d int) []string {
    //根据第d个字母字符用键索引排序(从前面)
    count := map[int][]string{}
    for i := lo; i <= hi; i++ {
        temp := 0
        if len(a[i]) > d { //兼容字符长度不一样的
            temp = cast.ToInt(a[i][d] + 1)
            count[temp] = append(count[temp], a[i])
        } else {
            temp = 0
            count[temp] = append(count[temp], a[i])
        }

        if temp > MAX_R {
            MAX_R = temp
        }
    }

    //回写
    for r := 0; r < MAX_R; r++ {
        if len(count[r]) > 1 { //有多个值,需要再次排序
            //递归排序
            sortMSD(count[r], 0, len(count[r])-1, d+1)
        } else {
            if len(count[r]) == 1 { //合并数据
                aux = append(aux, count[r][0])
            }
        }
    }

    return aux
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!