简体   繁体   中英

Time complexity for storing array elements

Here's my program, i'm able to get the time complexity partially can anyone help me with this:

I'am able to get some of them, but can anyone verify if my approach is right

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)
        }
    }

}

Complexity here is O(5N) .

For further info about finding complexity check this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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