Question
can anyone solve this in java with algorithm pseducode based on the code please
Description Perform an empirical study to compare the performance of binary and interpolation search for an application which
0 0
Add a comment Improve this question Transcribed image text
Answer #1

On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons.

The pseudocode of binary search algorithms should look like this −

Procedure binary_search
   A ← sorted array
   n ← size of array
   x ← value to be searched

   Set lowerBound = 1
   Set upperBound = n 

   while x not found
      if upperBound < lowerBound 
         EXIT: x does not exists.
   
      set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
      
      if A[midPoint] < x
         set lowerBound = midPoint + 1
         
      if A[midPoint] > x
         set upperBound = midPoint - 1 

      if A[midPoint] = x 
         EXIT: x found at location midPoint
   end while
   
end procedure

The pseudocode of interpolation search algorithms should look like this −

A → Array list
N → Size of A
X → Target Value

Procedure Interpolation_Search()

   Set Lo  →  0
   Set Mid → -1
   Set Hi  →  N-1

   While X does not match
   
      if Lo equals to Hi OR A[Lo] equals to A[Hi]
         EXIT: Failure, Target not found
      end if
      
      Set Mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo]) 

      if A[Mid] = X
         EXIT: Success, Target found at Mid
      else 
         if A[Mid] < X
            Set Lo to Mid+1
         else if A[Mid] > X
            Set Hi to Mid-1
         end if
      end if
   End While

End Procedure

Java example source code-

EmpiricalStudy.java

public class EmpiricalStudy {

   public static int binarySearch(int arr[], int first, int last, int key){
       int mid = (first + last)/2;
       while( first <= last ){
           if ( arr[mid] < key ){
               first = mid + 1;   
           }else if ( arr[mid] == key ){
               return mid;
           }else{
               last = mid - 1;
           }
           mid = (first + last)/2;
       }
       return -1;
   }
   public static int interpolationSearch(int arr[], int n, int x)
   {
       // Find indexes of two corners
       int lo = 0, hi = (n - 1);

       // Since array is sorted, an element present
       // in array must be in range defined by corner
       while (lo <= hi && x >= arr[lo] && x <= arr[hi])
       {
           if (lo == hi)
           {
               if (arr[lo] == x) return lo;
               return -1;
           }
           // Probing the position with keeping
           // uniform distribution in mind.
           int pos = (int) (lo + (((double)(hi - lo) /
                   (arr[hi] - arr[lo])) * (x - arr[lo])));

           // Condition of target found
           if (arr[pos] == x)
               return pos;

           // If x is larger, x is in upper part
           if (arr[pos] < x)
               lo = pos + 1;

           // If x is smaller, x is in the lower part
           else
               hi = pos - 1;
       }
       return -1;
   }

   public static void main(String[] args) {
       // Array of items on which search will
       // be conducted.
       int arr[] = new int[500000];

       for(int i=0;i<500000;i++)
       {
           arr[i]=i;
       }
       int n = arr.length;

       int x = 18; // Element to be searched

       long start = System.currentTimeMillis();
       int index = interpolationSearch(arr, n, x);
       long end = System.currentTimeMillis();
      
       // If element was found
       if (index != -1)
           System.out.println("Interpolation Search found element at index "+index+" in "+(end - start) + " millisec");
       else
           System.out.println("Element not found.");
      
       start = System.currentTimeMillis();
       index = binarySearch(arr, 0, n, x);
       end = System.currentTimeMillis();
      
       // If element was found
       if (index != -1)
           System.out.println("Binary Search found element at index "+index+" in "+(end - start) + "millisec");
       else
           System.out.println("Element not found.");
   }
}

Add a comment
Know the answer?
Add Answer to:
Description Perform an empirical study to compare the performance of binary and interpolation sea...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The objective of this Assignment is to compare the performance of some standard SORT algorithms. You...

    The objective of this Assignment is to compare the performance of some standard SORT algorithms. You should look at MergeSort, QuickSort, and Insertion Sort; you can also look at an additional Sort mechanism that isn't covered in class or that isn't covered in the book for extra credit. You will have to find the right implementations in Java so that you can make your comparisons. Things you will be comparing are the raw running time and the time complexity in...

  • Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description...

    Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description The shortest path problem is one of most important problems in graph theory and computer science in general. Shortest path problem is one of typical optimization problems. Given a graph G = (V,E), the goal is to nd a minimum cost path from s → t, s,t ∈ V . This variant is called one-to-one shortest path problem. Other variants are one-to-all (compute shortest...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • A test specification provides designers with what needs to be known in order to perform a...

    A test specification provides designers with what needs to be known in order to perform a specific test, and to validate and verify the requirement to be tested. The test script is divided into the test script, which is the generic condition to be tested, and one or more test cases within the test script. Provide a test script and test case for at least 3 of your requirements identified in your requirements specification. Provide the following format for an...

  • Performance Improvement programs and effective project management require an understanding of the four phases of the...

    Performance Improvement programs and effective project management require an understanding of the four phases of the project life-cycle: initiation, planning, execution/implementation, and closure. It is important to have each phase of the project mapped on a timeline and to be sure the project team has the appropriate members with the right skills to ensure a successful program. Using a case study from the text, students will demonstrate their ability to review and evaluate a performance improvement program by arranging the...

  • In today’s global and competitive business environment, many companies are finding that it is difficult to...

    In today’s global and competitive business environment, many companies are finding that it is difficult to determine whether employees have the capabilities needed for success. The necessary capabilities may vary from one business unit to another and even across roles within a business unit. As a result, many companies are using competency models to help them identify the knowledge, skills, and personal characteristics (attitudes, personality) needed for successful performance in a job. Competency models are also useful for ensuring that...

  • ENT 210: The Management Process of Entrepreneurs Week 4, Chapter 12 Case Study Assignment DUE DATE:...

    ENT 210: The Management Process of Entrepreneurs Week 4, Chapter 12 Case Study Assignment DUE DATE: Wednesday, February 13 25 Points There is a concluding case titled “The Law Offices of Jeter, Jackson, Guidry, and Boyer” at the end of the chapter on pages 465 - 466. As you read this short case, think about how the concepts from this chapter apply to what you are reading. CASE – 4 The Law Offices of Jeter, Jackson, Guidry, and Boyer THE...

  • 4. Perform a SWOT analysis for Fitbit. Based on your assessment of these, what are some strategic options for Fitbit go...

    4. Perform a SWOT analysis for Fitbit. Based on your assessment of these, what are some strategic options for Fitbit going forward? 5. Analyze the company’s financial performance. Do trends suggest that Fitbit’s strategy is working? 6.What recommendations would you make to Fitbit management to address the most important strategic issues facing the company? Fitbit, Inc., in 2017: Can Revive Its Strategy and It Reverse Mounting Losses? connect ROCHELLE R. BRUNSON Baylor University MARLENE M. REED Baylor University in the...

  • I have this case study to solve. i want to ask which type of case study...

    I have this case study to solve. i want to ask which type of case study in this like problem, evaluation or decision? if its decision then what are the criterias and all? Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...

  • What an Executive Summary Is An executive summary is a specific type of document that does...

    What an Executive Summary Is An executive summary is a specific type of document that does two things: it summarizes a research article, and it offers recommendations as to how information from the article can be used. Some long reports can contain an executive summary section, as indicated in the Pearson handbook. Write a 2 pahe Executive Summary In business contexts, an executive summary is always written for a specific purpose: to explain the information in the article to a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT