程序的最大符号是什么?

发布于 2025-02-01 05:50:07 字数 2400 浏览 4 评论 0原文

我正在尝试确定该程序的算法复杂性:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class SuffixArray
{
    private String[] text;
    private int length;
    private int[] index;
    private String[] suffix;
 
    public SuffixArray(String text)
    {
        this.text = new String[text.length()]; 
 
        for (int i = 0; i < text.length(); i++)
        {
            this.text[i] = text.substring(i, i+1);
        } 
 
        this.length = text.length();
        this.index = new int[length];
        for (int i = 0; i < length; i++)
        {
            index[i] = i;
        } 
 
        suffix = new String[length];
    }
 
    public void createSuffixArray()
    {   
        for(int index = 0; index < length; index++) 
        {
            String text = "";
            for (int text_index = index; text_index < length; text_index++)
            {
                text+=this.text[text_index];
            } 
            suffix[index] = text;
        }
 
        int back;
        for (int iteration = 1; iteration < length; iteration++)
        {
            String key = suffix[iteration];
            int keyindex = index[iteration];
 
            for (back = iteration - 1; back >= 0; back--)
            {
                if (suffix[back].compareTo(key) > 0)
                {
                    suffix[back + 1] = suffix[back];
                    index[back + 1] = index[back];
                }
                else
                {
                    break;
                }
            }
            suffix[ back + 1 ] = key;
            index[back + 1 ] = keyindex;
        }
 
        System.out.println("SUFFIX \t INDEX");
        for (int iterate = 0; iterate < length; iterate++)
        {  
            System.out.println(suffix[iterate] + "\t" + index[iterate]);
        }
    }
 
 
    public static void main(String...arg)throws IOException
    {
        String text = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Text String ");
        text = reader.readLine();
 
        SuffixArray suffixarray = new SuffixArray(text);
        suffixarray.createSuffixArray();
    } 
}

我对Wikipedia进行了一些有关Big-O符号的研究,并使用不同尺寸的字符串运行代码。根据用不同尺寸的刺激来运行代码所需的时间,我认为它的复杂性可能为O(n^2)。我怎么知道?

任何帮助将不胜感激。

I am trying to determine the algorithmic complexity of this program:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class SuffixArray
{
    private String[] text;
    private int length;
    private int[] index;
    private String[] suffix;
 
    public SuffixArray(String text)
    {
        this.text = new String[text.length()]; 
 
        for (int i = 0; i < text.length(); i++)
        {
            this.text[i] = text.substring(i, i+1);
        } 
 
        this.length = text.length();
        this.index = new int[length];
        for (int i = 0; i < length; i++)
        {
            index[i] = i;
        } 
 
        suffix = new String[length];
    }
 
    public void createSuffixArray()
    {   
        for(int index = 0; index < length; index++) 
        {
            String text = "";
            for (int text_index = index; text_index < length; text_index++)
            {
                text+=this.text[text_index];
            } 
            suffix[index] = text;
        }
 
        int back;
        for (int iteration = 1; iteration < length; iteration++)
        {
            String key = suffix[iteration];
            int keyindex = index[iteration];
 
            for (back = iteration - 1; back >= 0; back--)
            {
                if (suffix[back].compareTo(key) > 0)
                {
                    suffix[back + 1] = suffix[back];
                    index[back + 1] = index[back];
                }
                else
                {
                    break;
                }
            }
            suffix[ back + 1 ] = key;
            index[back + 1 ] = keyindex;
        }
 
        System.out.println("SUFFIX \t INDEX");
        for (int iterate = 0; iterate < length; iterate++)
        {  
            System.out.println(suffix[iterate] + "\t" + index[iterate]);
        }
    }
 
 
    public static void main(String...arg)throws IOException
    {
        String text = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Text String ");
        text = reader.readLine();
 
        SuffixArray suffixarray = new SuffixArray(text);
        suffixarray.createSuffixArray();
    } 
}

I have done some research on Wikipedia about the Big-O notation and have run the code with strings of different sizes. Based on the time it takes to run the code with different-size stings, I feel its complexity might be O(n^2). How can I know for sure?

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

夏了南城 2025-02-08 05:50:07

创建的复杂性方法是o(n^2),您可以通过检查3个循环块来确定这一点。另外,由于在您的问题中,您说过,您已经阅读了Wikipedia文章,其中涵盖了Big-O符号,然后专注于其属性。它们是如何应用正确逻辑并计算算法的复杂性的核心。

在您的第一个块中,最内向的循环迭代长度又重复长度次,由于product,因此产生O(n^2)的复杂性属性 big-o符号的。

for(int index = 0; index < length; index++) 
{
    String text = "";
    for (int text_index = index; text_index < length; text_index++)
    {
        text+=this.text[text_index];
    } 
    suffix[index] = text;
}

在您的第二个区块中,尽管最内向的循环在开始时没有执行长度迭代,但其复杂性仍然倾向于O(n),由于o(n^2)的总体复杂性再次产生产品属性

for (int iteration = 1; iteration < length; iteration++)
{
    String key = suffix[iteration];
    int keyindex = index[iteration];

    for (back = iteration - 1; back >= 0; back--)
    {
        if (suffix[back].compareTo(key) > 0)
        {
            suffix[back + 1] = suffix[back];
            index[back + 1] = index[back];
        }
        else
        {
            break;
        }
    }
    suffix[ back + 1 ] = key;
    index[back + 1 ] = keyindex;
}

您的第三个也是最后一个块简单地迭代长度次,给我们O(n)的线性复杂性。

for (int iterate = 0; iterate < length; iterate++)
{  
    System.out.println(suffix[iterate] + "\t" + index[iterate]);
}

在此时,要获得方法复杂性,我们需要收集我们拥有的3个子复合物,并将其应用于他们sum属性,这将产生O(n^2)。

The complexity of your createSuffixArray method is O(n^2) and you can determine that by examining the 3 looping blocks. Also, since in your question you said you've read the Wikipedia article covering the Big-O notation, then focus on its properties. They are the core of how to apply the right logic and compute the complexity of an algorithm.

Within your first block, the innermost loop iterating length times is in turn repeated for length times, yielding a complexity of O(n^2) due to the Product Property of the Big-O notation.

for(int index = 0; index < length; index++) 
{
    String text = "";
    for (int text_index = index; text_index < length; text_index++)
    {
        text+=this.text[text_index];
    } 
    suffix[index] = text;
}

In your second block, although the innermost loop does not perform length iterations at the beginning, its complexity still tends to O(n), producing again an overall complexity of O(n^2) due to the Product Property.

for (int iteration = 1; iteration < length; iteration++)
{
    String key = suffix[iteration];
    int keyindex = index[iteration];

    for (back = iteration - 1; back >= 0; back--)
    {
        if (suffix[back].compareTo(key) > 0)
        {
            suffix[back + 1] = suffix[back];
            index[back + 1] = index[back];
        }
        else
        {
            break;
        }
    }
    suffix[ back + 1 ] = key;
    index[back + 1 ] = keyindex;
}

Your third and last block simply iterates length times, giving us a linear complexity of O(n).

for (int iterate = 0; iterate < length; iterate++)
{  
    System.out.println(suffix[iterate] + "\t" + index[iterate]);
}

At this point to get the method complexity, we need to gather the 3 sub-complexities we've got and apply to them the Sum Property, which will yield O(n^2).

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