Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the Big O notation for each of the following code snippets: 1). public static void printHello(int n) { for(int i=1;i

What is the Big O notation for each of the following code snippets:

1).

public static void printHello(int n) { for(int i=1;i<=n;i++) { System.out.println("hello } } 2). 
static void singleLoop(int[] arr) { int numberOfOperations = 0; for (int i = 0; i < arr.length; i++) { numberOfOperations++; } System.out.println("Numberperations in singleLoop:" + numberOfOperations); } 3).
static void nestedLoop(int[] arr) { int numberOfOperations = 0; for (int i = 0; i < arr.length; i++) { for (int y = 0; y < arr.length; y++) { numberOfOperations++; } } System.out.println("Numberperations in nestedLoop:" + numberOfOperations); }

4).

static void getToPivot(int[] arr, int pivot) { int numberOfOperations = 0; int length = arr.length; int maxIndex = length - 1; int i = 0; int j = maxIndex; while (i < maxIndex && arr[i] < pivot) { numberOfOperations++; i++; } while (j > 0 && arr[j] > pivot) { numberOfOperations++; j--; } System.out.println("Numberperations in getToPivot:" + numberOfOperations); }

5).

static void smoosh(int[] arr) { int numberOfOperations = 0; int i = 0; int j = 0; while (i < arr.length) { arr[j] = arr[i]; do { i++; numberOfOperations++; } while ((i < arr.length) && (arr[i] == arr[j])); j++; } System.out.println("Numberperations in smoosh:" + numberOfOperations); }

6).

public boolean isFirstNumberEqualToOne(List numbers) {
return numbers.get(0) == 1;
}

7).

public boolean containsNumber(List numbers, int comparisonNumber) { for(Integer number : numbers) { if(number == comparisonNumber) { return true; } } return false; }

8).

public static boolean containsDuplicates(List input) { for (int outer = 0; outer < input.size(); outer++) { for (int inner = 0; inner < input.size(); inner++) { if (outer != inner && input.get(outer).equals(input.get(inner))) { return true; } } } return false; }

9).

public boolean containsNumber(List numbers, int comparisonNumber) { int low = 0; int high = numbers.size() - 1; while (low <= high) { int middle = low + (high - low) / 2; if (comparisonNumber < numbers.get(middle)) { high = middle - 1; } else if (comparisonNumber > numbers.get(middle)) { low = middle + 1; } else { return true; } } return false; } 10). 
public int fibonacci(int number) { if (number <= 1) { return number; } else { return fibonacci(number - 1) + fibonacci(number - 2); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions