繁体   English   中英

存储数组元素的时间复杂度

[英]Time complexity for storing array elements

这是我的程序,我可以部分地获得时间复杂度,任何人都可以帮助我:

我可以得到其中的一些,但是任何人都可以验证我的方法是否正确

package sd;

public class Max {

    public static void main(String args[]) {
        int i;
        int large[] = new int[5];
        int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 };
        int max = 0, index = 0;

        // O(5)
        for (int j = 0; j < 5; j++) {

            max = array[0]; // Assuming max to be first element

            // Comparing 1st element with max O(n)
            for (i = 1; i < array.length; i++) {
                if (max < array[i]) {
                    max = array[i]; // Replace if greater
                    index = i;
                }
            }
            large[j] = max;
            array[index] = Integer.MIN_VALUE; // Find max and replace with least
                                                // possible value to avoid
                                                // duplicate max

            System.out.println("Largest 5 amoung 10 : " + large[j]); // Time
                                                                        // complexity:
                                                                        // O(5)
                                                                        // *
                                                                        // O(n)
                                                                        // =
                                                                        // O(n)
        }
    }

}

这里的复杂度是O(5N)

有关查找复杂性的更多信息,请检查此问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM