5)
Answer: Big Oh : O(n^2)
explanation:
in method m2
for loop runs from i=1 to n-1 // means n-1 times
and each time pM2 is called
in method pM2
for loop runs i=end to begin in worst case //means (end-begin)+1 times
//total complexity is
//total number of times
1+2+3+4+.......+n = n(n+1)/2 times
hence .....O(n^2)
4)
Answer: Big Oh : O(n^2)
explanation:
in method m1
for loop runs from i=0 to n-1 // means n-1 times
and each time pM1 is called //every time last will be n-1
in method pM1
for loop runs i=first+1 to last in worst case //means (last-(first+1))+1 times
//total complexity is
//total number of times
n-1+n-2+n-3+.......+3+2+1 = n(n-1)/2 times
hence .....O(n^2)
5. What is the Big Oh method m2? public static void m2(int[] arr, int n) for...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...
public static int max(int [] arr){ int max = arr[0]; for( int i = 1; i < arr.length; i++ ) if( arr[i] > max ) max = arr[i]; return max; } 1) Provide an input value for arr that causes the method to throw an IndexOutOfBounds exception. 2) Provide an input value for arr that causes the method to throw a NullPointerException.
Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...
Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) { if(arr == null || arr.length == 0) { return -1; } for (int i = 0; i < arr.length; i++) { if(arr[i] == ch) { return i; } } return -1; ...
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...
Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...
QUESTION 21 What exception will the following program raise? class Main f public static void main(Stringl args) try System.out.print Hello"1/0); catch ( e) f codes are not provided** DataFormatException InputMismatchException ArithmeticException DividedByZeroException QUESTION 19 Suppose we had the following method header: public void someMethod (int someNumber) Which of the following is an overloaded version of this method? public void someMethod (int someNumber, int anotherNumber) O public int someMethod (int anotherNumber) O public void someMethod (int anotherNumber) public void anotherMethod (int...
What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -
24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) ( int result 0: if (a <b) ( else if (a b) else ( return result: result mystery2 (a) mystery2 (a)i result - mystery2 (b) result-ab; public static int mystery2 (int x) f int countx for (int i 0; іск; i++) count +1: return counti What are the values stored in the variable result after the following method calls? a) int result mysteryl(4,1): b) int...