算法复杂度

发布于 2024-08-13 15:26:23 字数 750 浏览 4 评论 0原文

我正在尝试计算以下算法的复杂度,

private static List<int> GetIndexes(string strippedText, string searchText)
    {
        List<int> count = new List<int>();
        int index = 0;
        while (strippedText.Length >= index && index != -1)
        {
            index = strippedText.IndexOf(searchText.Trim(), index,
                                         StringComparison.OrdinalIgnoreCase);
            if (index != -1)
            {
                count.Add(index);
                index++;
            }
            else continue;
        }
        return count;
    }

我知道如果 count 在每次迭代中增加 1,则循环的复杂度为 O(n) ,但是增量取决于找到的索引。在每次迭代中,它可以增加 1 或 strippedText.lenght()

有什么想法吗?

I am trying to calculate the complexity of the following algorithm

private static List<int> GetIndexes(string strippedText, string searchText)
    {
        List<int> count = new List<int>();
        int index = 0;
        while (strippedText.Length >= index && index != -1)
        {
            index = strippedText.IndexOf(searchText.Trim(), index,
                                         StringComparison.OrdinalIgnoreCase);
            if (index != -1)
            {
                count.Add(index);
                index++;
            }
            else continue;
        }
        return count;
    }

I know that the loop has a complexity of O(n) if count increments by 1 on each iteration but, the increments depends from the indexes found. on each iteration it could increment 1 or strippedText.lenght().

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

与酒说心事 2024-08-20 15:26:23

尽管如此,最坏的情况仍然是 O(n)。

Still, the worst case is O(n).

影子是时光的心 2024-08-20 15:26:23

它仍然是 O(n) - 这是因为它的渐近增长速度与 O(n)

大 O 表示法用于算法增长的上限- 也就是说,算法保证以与它相同或更慢的速度增长的函数。

在平均情况下,您的算法将以 O(n/m) 的速度增长,其中 m 是对字符串中索引密度的估计(0 = 无索引) , 1 = 每个字符的索引)。假设在 n 上大致恒定,您可以忽略 m 并且仍然说该算法为 O(n)

事实上,在现实世界中,您的算法几乎肯定会比 O(n) 更快,但这并不能阻止它开始一个 O(n) 函数。


查看此页面,特别是:

符号 O 用于用另一个更简单的函数来描述函数大小的渐近上限。
这意味着对于x> k,当x趋于无穷大时,f(x)的值将始终低于C *g(x)(其中C为常数)。

It is still O(n) - this is because it grows asymptotically at the same rate as O(n)

Big O notation is used for the upper-bound of algorithmic growth - that is, it's a function that the algorithm is guaranteed to grow at the same rate as, or slower than.

In the average case your algorithm will grow at rate O(n/m) where m is some estimate of the how dense the indexes are in your strings (0 = no indexes, 1 = index at every character). Assuming that's roughly constant over n you can ignore the m and still say the algorithm is O(n).

The fact that, in the real world, your algorithm will almost certainly be faster than O(n) doesn't stop it begina n O(n) function.


Take a look at this page, particularly:

The symbol O is used to describe an asymptotic upper bound for the magnitude of a function in terms of another, simpler function.
This means that for x > k, when x tends to infinity, the value f(x) will always be inferior to C *g(x) (with C a constant).

末が日狂欢 2024-08-20 15:26:23

O(n),其中 n 是 strippedText 字符串的长度。

最坏的情况,每个字符都等于 searchText 并会导致 n 次迭代,但即使情况并非如此(我假设不会),您的平均情况将是 n 的某个因子 c其中 c 大于 0 但小于 1,因此循环次数将为 cn,但常数因子 n 仍将表示为 O(n)。

O(n) where n is the length of the strippedText string.

Worst case, every character will be equal to the searchText and will result in n itterations, but even if that isn't the case (which I am assuming it won't be) your average case will be some factor, c, of n where c is greater than zero but less than 1, so the number of loops will be cn, but a constant factor of n will still be represented as O(n).

独孤求败 2024-08-20 15:26:23

我仍然会说它是 O(n) (或者至少是最坏的 O(n),除非你的索引可以小于 -1(比如 -10),这会进一步重置它)

但是是的 O(n)。

I would still say that it is O(n) (or at least At worst O(n), unless your index can be made less than -1 say -10, which would reset it further)

But yes O(n).

猫性小仙女 2024-08-20 15:26:23

实际上它是 O(strippedText.Length*searchText.Length),因为索引对 searchText 和 strippedText 中的字符进行比较。

Actually it's O(strippedText.Length*searchText.Length) since index of makes a comparison between characters in searchText and strippedText.

年少掌心 2024-08-20 15:26:23

这完全取决于字符串 IndexOf() 方法的实现方式。基本上,您只需使用 IndexOf() 搜索整个字符串 - 其效率取决于该方法的效率。

有几种不同复杂度的字符串搜索算法,范围从O(m+n)O(m*n)mn 是两个字符串的长度)。

It all depends on how the strings IndexOf() method is implemented. Basically you just search through the whole string with IndexOf() - how efficient that is depends on the efficiency of that method.

There are several string search algorithms of different complexities ranging from O(m+n) to O(m*n) (m and n being the length of the two strings).

九局 2024-08-20 15:26:23

我想说的是 O(n/m) 其中 strippedText.Length == nm前缀的长度code>searchTextsearchText 的后缀(如果不存在这样的前缀,则为 searchText 的长度)。

示例:

searchText = 'ababab' --> m == 2

searchText = 'aaaaaa' --> m == 1

searchText = 'abcabc' --> m == 3

searchText = 'abcdefg' -->米==7

I'd say that it's O(n/m) where strippedText.Length == n and m is the length of a prefix of searchText that is a suffix of searchText (if no such prefix exist it's the length of searchText).

Examples:

searchText = 'ababab' --> m == 2

searchText = 'aaaaaa' --> m == 1

searchText = 'abcabc' --> m == 3

searchText = 'abcdefg' --> m == 7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文