2010-11-27 130 views

回答

1

我想这可以发生在嵌套binary searches,这是O(log n)

这是一个愚蠢的例子,使用C#:

public class SuperHeroComparer : IComparer<string> 
{ 
    public int Compare(string first, string second) 
    { 
     int firstLimboIndex = Limbo.BinarySearch(first); 
     int secondLimboIndex = Limbo.BinarySearch(second); 
     if (firstLimboIndex < 0 && secondLimboIndex >= 0) { 
      return 1; 
     if (firstLimboIndex >= 0 && secondLimboIndex < 0) { 
      return -1; 
     return String.Compare(first, second); 
    } 
} 

public static class Continuity 
{ 
    public static int IndexOfSuperHero(string name) 
    { 
     return SuperHeroes.BinarySearch(name, new SuperHeroComparer()); 
    } 
} 

在上面的代码,Continuity.IndexOfSuperHero()将有O(log n)²复杂。

相关问题