是否有 O(n) 或更快的算法用于按 levenshtein 距离对列表进行排序?我看过一些关于 SO 的解决方案,但它们都调用了传统的排序。现在,假设您只是对输入的字节求和:您将获得几乎按它们的 levenshtein 距离排序的散列键。例如,我采用一组随机字符串并通过字节求和计算它们的哈希值:
[ { hash: 2826, val: 'LKAMFKLFUAHUHAUHAUHAU:ANGONEGANAILFJAL:' },
{ hash: 2829, val: 'LKAMFKLFLFUAHUAHUHAUAHANGONEGANAILFJAL:' },
{ hash: 2845, val: 'LKAMFKLFLFAKAKKAKAfiO:ANGONEGANAILFJAL:' },
{ hash: 3064, val: 'LKAMFKLFKKKlaNflanfiO:ANGONEGANAILFJAL:' },
{ hash: 3092, val: 'LKAMFKLFLFklaNflanfiO:ANGONEGANAILFJAL:' },
{ hash: 3203, val: 'LKAMFKLFLFklaNflanfiRSRSRSRSRRNAILFJAL:' },
{ hash: 3249, val: 'LKNFUU{N{UAFN{NF}FNPNF{FN{APNF{WNFF{NF' },
{ hash: 3843, val: 'ddddddddddaaaaaaaaaddddddddddaaaaaaaaaa' },
{ hash: 3858, val: 'safndjnjcmxn,znv,mnm,n,mvnm,vn,mznv,mvv' },
{ hash: 3934, val: 'nngnangngdsgsangnanwns.mnv.nv.xnjvnsf.,' },
{ hash: 3972, val: 'adadsadadsadadadsadsadadsadsadadsadsada' },
{ hash: 3992, val: 'adsadadadsadasdasdafadfasfdsafsafasfafd' },
{ hash: 4041, val: 'asfdsafasdfsafafasdfasdfafsdfdasfasfasf' },
{ hash: 4047, val: 'kkkkkkkkkkkdddddddddkkkkkkkkkkddddddddd' },
{ hash: 4058, val: 'jfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfj' },
{ hash: 4081, val: 'ioudnjkanfjfhjhfjhakfshfkjhdajhkjafhkjf' },
{ hash: 4082, val: 'ioudnjkanfjfhjhfjhakfshfkjhdakhkjafhkjf' },
{ hash: 4082, val: 'oisdnkbgjkbajkgkbgkjbkklgjklsbkbfkjafas' },
{ hash: 4090, val: 'ioudnjsanfjfhjhfjhakfshfkjhdakhkjafhkjf' },
{ hash: 4099, val: 'asldfjlkcmclmasldkkjflksajflkjaljfljlfa' },
{ hash: 4101, val: 'sidfjlasjflijflijlfjliafjdlifjlijfiljfl' },
{ hash: 4105, val: 'iousnjsanfjfhjhfjhakfshfkjhdakhkjafhkjf' },
{ hash: 4125, val: 'iousnjsanfjfhlhfjuakfshkkjhdakhkjafhkjf' },
{ hash: 4128, val: 'sadnfjnfjnjfnjsdfnjafnjkfnkfnjkansdfjkn' },
{ hash: 4143, val: 'dnsfanfjknasfjklnaskfnkfnklafnjkfnkldsn' },
{ hash: 4150, val: 'dskfoisandginsgnlgn:nglngbtbiybuburubsu' },
{ hash: 4155, val: 'afadfsfsfsdfffsfsfsfsdfsfsfsdfsfsfsfsfs' },
{ hash: 4166, val: 'kjdkljkljkljlkjkljlkjlkjlkjlkjljlkjljlk' },
{ hash: 4211, val: 'jsanjnvjksnfkjsanfuiawngingiuilugniugng' },
{ hash: 4229, val: 'kllnlknlknklnklnlnlknknklnlnlnklnlknlkn' },
{ hash: 4238, val: 'lsniorhgpwoiqutoiuieofnionofnoinfonfioa' },
{ hash: 4349, val: 'iasfioehwoptqpoituopqwtuoquweporuqiorur' },
{ hash: 4374, val: 'ioequroiqwuroiuriouroiuopriuprouqpourrq' },
{ hash: 4377, val: 'iiuouoiuoiuouoiuuououoiuououoiuououoiuo' } ]
结果几乎是排序的,这意味着插入排序可以非常快地完成工作(请参阅 参考资料)。
如果这种粗略的实验提供了这些结果,那么肯定有一些解决方案在它的答案中丢失了。可能是哪个?