Saturday 20 July 2019

Big O

Algorithm

The step by step code you know those instructions we tell to a computer in a particular program is basically

called an algorithm.

For example

1.  O(n)
  public int countEvents(int[] events)
    {
        int count = 0;
        for (int i = 0; i < events.length; i++) {
            if (events[i]%2 == 0) {
                count++;
            }
        }
        return count;
    }

2. Constant function O(1)

 public int getElementFrom(int[] events,int index)
    {
        return events[index];
    }

3. Quadratic functions O(n^2)
public int countDuplicates(int[] item1,int[] item2)
    {
        int count = 0;
        for (int i = 0; i < item1.length; i++) {
            for (int j = 0; j <item2.length ; j++) {
                if (item2[i] == item1[i] ) {
                    count++;
                }
            }

        }
        return count;
    }