Question

In: Computer Science

1. What is the Big-O run-time of linear search and binary search in the best, average...

1. What is the Big-O run-time of linear search and binary search in the best, average and worst cases?

      Linear Search                  Binary Search

Best:

Worst:

Average:

2. What is the Big-O runtime of the following linked list methods?

  • addFirst()
  • toString()

What is the Big-O runtime of the below Stack method?

  • isSorted(Node n)

3. What is the Big-O runtime of the below methods:

Method A: Stack Copy Constructor Option 1

public Stack(Stack<T> original) { 
    if(original == null) {
       return;
    } else if (original.length == 0) {
       this();
    } else {
       Node current = original.top;
       Stack<T> temp = new Stack<>();
       while (current != null) {
          temp.push(current.data);
          current = current.next;
       }       current = temp.top;
       while (temp != null) {
           this.push(current.data);
           current = current.next;
       }
       this.length = original.length;
    }
}

Method B: Stack Copy Constructor Option 2
public Stack(Stack<T> original) {
   if (original.length == 0) {
      top = null;
      length = 0;
   } else {
      top = new Node(original.top.data);
      Node temp1 = original.top;
      Node temp2 = top;
      for (int i = 0; i < original.getLength() - 1; i++) {
         temp1 = temp1.next;
         temp2.next = new Node(temp1.data);
         temp2 = temp2.next;
      }
      length = original.length;
   }
}
Method C: Stack Copy Constructor Option 3:

public Stack(Stack<T> original) {
   if (original == null) {
       return;
   } else if (original.length == 0) {
      this.length = 0;
      this.top = null;
   } else { 
      int count = original.getLength();
      while (count > 0) {
         count--;
         Node temp = original.top;
         int i = 0;
         while (temp.next != null && i < count) {
            i++;
            temp = temp.next;
         } //end inner while
         push(temp.data);
      } //end outer while
   } //end else
}//end method

Solutions

Expert Solution

Q.No.1:-

Linear Search:-                 

Best: O(1)               {element to be searched is the first element of the list }               

Average: O(n/2) = O(n) {element is present at the middle of the list}

Worst: O(n)    {element to be searched is the last element of the list or the element is not present in the list }      

Binary Search:-{ it cuts down the search to half at every iteration, but given array must be sorted}

Best: O(1)          {element to be searched is the middle most element, searches an element in constant time }                           

Average: O(logn) {search starts at middle}

Worst: O(logn){wost case that is the maximum number of comparisons to search element }

Q.No.2:- (a) addFirst() has O(1) compelxity..infact every 'insert' operation whether it is at begining or at any position of the linked list complexity is O(1) complexity....

(b) O(n^2) in worst case as two while loops are to be used in tostring()...

METHOD A:- O(n) as not nested while

METHO B:- O(n) where n is the length inputted

METHOD C:-O(n^2) as two while loops are to be used...

kindly comment if you need further elaboration ..please give like if you find it needfull..


Related Solutions

What characteristic of the binary search algorithm results in its speed? What is big O for...
What characteristic of the binary search algorithm results in its speed? What is big O for binary search? Binary search has a significant disadvantage -- what is it?
Q1 A- How linear search and binary search work? What is their time complexity? B- What...
Q1 A- How linear search and binary search work? What is their time complexity? B- What is a single linked list? What are the pros and cons of the linked list when comparing to parallel arrays? Q2 A- What is a register? How registers work together with ALU and Control Unit to execute a program? What is the fetch-execute cycle? B- What is the difference to implement a control unit using microprogrammed or hardwired approaches? What is clock cycle? What...
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
What is the Big O of the following algorithms along with the worst and average cases:...
What is the Big O of the following algorithms along with the worst and average cases: Euclid's Algorithm Brute-Force Matching Topological Sort Lomuto Partition Russian Peasant Algorithm
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
What is the time complexity of the following code? (in big-O notaion) sum = 0 var...
What is the time complexity of the following code? (in big-O notaion) sum = 0 var = 0 product = 0 for i = 1 to (n+2){ sum = sum + 2i for j = i to [(n*n)+i]{ var = sum + var for k = sum + var{ product = var++ } } } for m + 1 to (j*i){ sum = sum + product }
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f(x, y) is O(g(x, y)) means that there exist constants C, k1, and k2 such that|f(x, y)|≤C|g(x, y)|whenever x > k1 and y > k2] 2. Find a div b and a mod b when: (a) a = 30303, b = 333 (b) a = −765432, b = 3827 3. Convert...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT