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
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
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
If you run the binary search method on a set of unordered data, What happens? Why?...
If you run the binary search method on a set of unordered data, What happens? Why? How you would correct this issue?
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...
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...
Draw a single graph in Python showing the performance of both a linear and binary search...
Draw a single graph in Python showing the performance of both a linear and binary search algorithm against the number of elements (100,000). Consider the worst case scenario. In the graph we want to see: labeling of the axes, a legend, a title and the graphs for both linear and binary search, again correctly labeled. Explain what you see. Hint: Use Plot Functions (Plot Chart, series from functions) from the H!de Editor.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT