Lab: Search Time
The difference in performance between linear and binary search is stark. In this lab you are to create a program to compute the worst case number of probes needed for a linear and binary search and output the number of probes binary search will save. That is, your program will take in as input a positive integer that represent the size of a sorted list and output the difference in the the worst case number of probes needed for a linear and binary search on a list of that size.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
final Scanner cin = new Scanner(System.in);
final int n = cin.nextInt();
if(n==0){
System.out.println(0);
}
else{
if(n%2==0){
System.out.println(n-(int)(Math.log(n)/Math.log(2))-1);
}
else
System.out.println(n-(int)(Math.log(n+1)/Math.log(2)));
}
}
}