Question

In: Computer Science

Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...

Implement a method that meets the following requirements:

(a) Takes as parameter 1) an int array A 2) an int number x to search for

(b) determines whether x exists in A, and prints a message indicating the result

(c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains n items.)

(d) In comments above the method, explain what its algorithmic complexity is and why (constant, logarithmic, linear, quadratic...)

Solutions

Expert Solution

CODE IN JAVA:

Search.java file:


public class Search {
   //the time complexity of this method is linear
   public static void linearSearch(int[] arr, int num) {
       boolean flag = true;
       int ind = -1;
       for(int i=0;i<arr.length;i++) {
           if(arr[i]==num) {
               System.out.println("Your number is found at index "+i);
               flag = false;
               break;
           }
       }
       if(flag)
           System.out.println("You number is not found");
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int arr[] = {9,1,5,3,8,10,34,65,3};
       int num = 10;
       System.out.println("The array is:");
       for(int i=0;i<arr.length;i++)
           System.out.print(arr[i]+" ");
       System.out.println("\nThe number to be searched is:"+num);
       linearSearch(arr,num);

   }

}
OUTPUT:


Related Solutions

. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list...
. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
Implement a method that meets the following requirements: (a) Has the same requirements as the above...
Implement a method that meets the following requirements: (a) Has the same requirements as the above method, but works with an int array that is sorted in increasing order. Attempt your best complexity (b) In comments above the method, explain what its algorithmic complexity is and why (constant, logarithmic, linear, quadratic...)
1. Implement a method that meets the following requirements: (a) Do not reuse any code for...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for the following: i. Try to write this method with as few lines of code as you can ii. Sorts a group of three integers, x,y and z, into decreasing order (they do not have to be in a sequence). iii. Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and...
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this...
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this method with as few lines of code as you can (b) Sorts a group of three integers, x,y and z, into increasing order (they do not have to be in a sequence). (c) Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and z (none of them contain the same...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be passed in on the stack. The answer should be returned in eax. The subroutine should not disturb any registers except eax, ecx, and edx. (Save any registers on the stack and restore them before exiting the subroutine.) Upon entry to the subroutine, push ebp, etc., to access the parameter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT