Big O is about how long an algorithm takes to run from start to end and how well it scales as the size of the input or dataset increases.

Big O is mostly measured based on the worst-case scenario even though some algorithms might finish earlier than others.

Here is another graph for more clarity.

public int getFirstItem(int[] items) { return items[0]; }

public void sendWelcomeNoteToUsers(User[] users) { for (User user : users) { emailService.sendNote(user); ... } }

public User getUserByEmail(User[] users, String email){ for (int i = 0; i < users.length; i++) { if (users[i].equals(email)) { return users[i]; } } }

public static void suspendAllUsersWithinPlans(Plan[] plans) { for (Plan plan : plans) { List<User> users = plan.getUsers(); for (User user : users) { // suspend logic goes here. } } }

private static void bubble_sort(int[] input, boolean ascending) { int inputLength = input.length; int temp; boolean is_sorted; for (int i = 0; i < inputLength; i++) { is_sorted = true; for (int j = 1; j < (inputLength - i); j++) { if (ascending) { if (input[j - 1] > input[j]) { temp = input[j - 1]; input[j - 1] = input[j]; input[j] = temp; is_sorted = false; } } else { if (input[j - 1] < input[j]) { temp = input[j - 1]; input[j - 1] = input[j]; input[j] = temp; is_sorted = false; } } } // is sorted? then break it, avoid useless loop. if (is_sorted) break; } }

As you can see, bubble sort has two for loops. An outer loop and an inner loop.

public static int binarySearch(int[] elements, int target) { int left = 0; int right = elements.length - 1; while (left <= right) { int middle = (left + right) / 2; if (target < elements[middle]) { right = middle - 1; } else if (target > elements[middle]) { left = middle + 1; } else { return middle; } } return -1; } public static void main(String[] args){ int[] arr1 = {-20, 3, 15, 81, 432}; // test when the target is in the middle int index = binarySearch(arr1,15); System.out.println(index); // test when the target is the first item in the array index = binarySearch(arr1,-20); System.out.println(index); // test when the target is in the array - last index = binarySearch(arr1,432); System.out.println(index); // test when the target is not in the array index = binarySearch(arr1,53); System.out.println(index); }

for (int i = 1; i <= n; i++){ for(int j = 1; j < 8; j = j * 2) { System.out.println("Hey - I'm busy looking at: " + i + " and " + j); } }

int k = 5; int n = 6; int power = (int)Math.pow(k, n); System.out.println("power: " + power); for (int i = 1; i <= power; i++){ System.out.println("Hey - I'm busy looking at: " + i); }

for (int i = 1; i <= factorial(n); i++) { System.out.println("Hey - I'm busy looking at: " + i); } .... public int factorial(int number) { int fact = 1; for (int i = 1; i <= number; i++) { fact = fact * i; } return fact; }

public static int getLargestItem(int[] items) { int largest = Integer.MIN_VALUE; for (int item : items) { if (item > largest) { largest = item; } } return largest; }