0

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

}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
  • Perhaps you should specify what parameters of your code are constant and which are not. Otherwise your code seems not to take any input data, so it is completely independent of input size, so it might run in `O(1)` :-) – vojta Sep 04 '15 at 12:47

1 Answers1

0

Complexity here is O(5N).

For further info about finding complexity check this question.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109