Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 该算法的时间复杂度?(大O)_Algorithm_Performance_Big O - Fatal编程技术网

Algorithm 该算法的时间复杂度?(大O)

Algorithm 该算法的时间复杂度?(大O),algorithm,performance,big-o,Algorithm,Performance,Big O,作为我作业的一部分,我正试图找出这段代码的时间复杂性。下面是代码的外观: public int solvePuzzle( ) { searchAlg = BINARY_SEARCH ? new BinarySearch() : new LinearSearch(); int matches = 0; for( int r = 0; r < rows; r++ ) for( int c = 0; c < columns; c++ )

作为我作业的一部分,我正试图找出这段代码的时间复杂性。下面是代码的外观:

public int solvePuzzle( )
{
    searchAlg = BINARY_SEARCH ? new BinarySearch() : new LinearSearch();
    int matches = 0;
    for( int r = 0; r < rows; r++ )
        for( int c = 0; c < columns; c++ )
            for( int rd = -1; rd <= 1; rd++ )
                for( int cd = -1; cd <= 1; cd++ )
                    if( rd != 0 || cd != 0 )
                        matches += solveDirection( r, c, rd, cd );
    searchAlg.printStatistics();
    return matches;
}

你在正确的轨道上


然而,关键的见解是,对于任何常数k(=独立于输入的大小),O(k)=O(1)。因此,您的O(N·N·3·3)与O(N·N)相同。这个结果需要与搜索结果相乘,这是您正确完成的。

您走对了方向


然而,关键的见解是,对于任何常数k(=独立于输入的大小),O(k)=O(1)。因此,您的O(N·N·3·3)与O(N·N)相同。这个结果需要和搜索结果相乘,这是正确的。

是的,它是朝着正确的方向进行的。我应该在问题中添加代码吗?
O(3)
?从未听说过这样的事情,然而
O(1)
意味着在固定时间内运行。所以,如果你知道它总是循环3次,它会在固定的时间运行。这和C++有什么关系?@ SeNeWalk实际上没有什么,只是认为C++程序员也会理解代码。我应该取下标签吗?@ClarkKent哦,好的,我明白了,谢谢。是的,它是朝这个方向的。我应该在问题中添加代码吗?
O(3)
?从未听说过这样的事情,然而
O(1)
意味着在固定时间内运行。所以,如果你知道它总是循环3次,它会在固定的时间运行。这和C++有什么关系?@ SeNeWalk实际上没有什么,只是认为C++程序员也会理解代码。我应该删除标记吗?@ClarkKent哦,好的,我明白了,谢谢。那么,这是否意味着T(M,N)=O(N^2*M)和T(M,N)=O(N^2*log M)?另外,当我测试不同大小的代码N时,N中的double给出的运行时间比使用二进制或线性的要大4倍或8倍search@CarltonJr. 是的,这些是运行时。关于您的测试运行,请注意logM运行时看起来很像常量运行时,除非采集了很多样本,所以我并不奇怪输入大小加倍“仅”慢4倍。另请参见此问题/答案:好的,非常感谢您的帮助和链接!只是一个简单的问题,你为什么不喜欢Java?(在你的个人资料上写着,只是好奇地想知道为什么)@CarltonJr。因为与其他语言相比,它非常臃肿,需要编写大量的内部代码(而不是解决实际问题的代码)。它还强制执行一个非常糟糕的面向对象版本,并鼓励(同样,臃肿的)设计模式。这就是说,Java7极大地改进了这一点,Java可以成为一种非常高效的语言。那么,这是否意味着T(M,N)=O(N^2*M)和T(M,N)=O(N^2*logm)?另外,当我使用不同大小的N测试代码时,N中的double会根据使用的是二进制还是线性,使运行时间增加4倍或8倍search@CarltonJr. 是的,这些是运行时。关于您的测试运行,请注意logM运行时看起来很像常量运行时,除非采集了很多样本,所以我并不奇怪输入大小加倍“仅”慢4倍。另请参见此问题/答案:好的,非常感谢您的帮助和链接!只是一个简单的问题,你为什么不喜欢Java?(在你的个人资料上写着,只是好奇地想知道为什么)@CarltonJr。因为与其他语言相比,它非常臃肿,需要编写大量的内部代码(而不是解决实际问题的代码)。它还强制执行一个非常糟糕的面向对象版本,并鼓励(同样,臃肿的)设计模式。也就是说,Java7极大地改进了这一点,Java可以成为一种非常高效的语言。
private int solveDirection( int baseRow, int baseCol, int rowDelta, int colDelta )
{
    String charSequence = "";
    int numMatches = 0;
    int searchResult;

    charSequence += theBoard[ baseRow ][ baseCol ];

    for( int i = baseRow + rowDelta, j = baseCol + colDelta;
             i >= 0 && j >= 0 && i < rows && j < columns;
             i += rowDelta, j += colDelta )
    {
        charSequence += theBoard[ i ][ j ];
        if ( charSequence.length() > maxWordLength )
            break;

        searchResult = searchAlg.search( theWords, charSequence );

        if( searchResult == theWords.length ) {   // corrected by UH 2007-05-02
            // either linear searched failed or binary search failed because charSequence
            // is larger than the largest word in theWords
            if ( searchAlg instanceof BinarySearch )
                break;    // binary search failed and it makes no sense to extend charSequence any further
            else
                continue; // linear search failed but an extension of charSequence may succeed
        }

        // precondition: 0 <= searchResult < theWords.length
        // At this point one, and only one, of three conditions holds:
        // 1. Linear search succeeded
        // 2. Binary search succeded
        // 3. Binary search failed at the insertion point for charSequence, 
        //    which means that theWords[ searchResult ] is the least element greater than charSequence

        if( PREFIX_TESTING && ! theWords[ searchResult ].startsWith( charSequence ) )
            break;

        if( theWords[ searchResult ].equals( charSequence ) ) {
//              if( theWords[ searchResult ].length( ) < 2 )
//                  continue;
            numMatches++;
            if ( PRINT_WORDS ) 
                System.out.println( "Found " + charSequence + " at " +
                        baseRow + " " + baseCol + " to " + i + " " + j );
        }
    }

    return numMatches;
}