manthanthakker40

Linear+BinarySearch

Jan 3rd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package searchingtechniques;
  7.  
  8. import java.util.Arrays;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author manth
  14.  */
  15. public class SearchingTechniques {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     static double input[];
  21.     static int n;
  22.     static String ALGO="BINARY SEARCH";
  23.     public static void main(String[] args) {
  24.         // TODO code application logic here
  25.         Scanner sc=new Scanner(System.in);
  26.         randinput();
  27.         System.out.println(" INPUT IS ");
  28.         Arrays.sort(input);
  29.         display(input);
  30.          System.out.println("Which value to find? ");
  31.         double to_find=Double.parseDouble(sc.nextLine());
  32.         System.out.println(" Search by which algorithm? \n1.LinearSearch\n2.BinarySearch");
  33.         long startTime=0 ;
  34.         int at_location=0;
  35.         switch(sc.nextLine())
  36.         {
  37.             case "1":
  38.                 startTime= System.currentTimeMillis();
  39.                  at_location=linearsearch(input,to_find);
  40.                 break;
  41.             case "2":
  42.                 startTime= System.currentTimeMillis();
  43.                 at_location=binarysearch(input,to_find);
  44.         }
  45.        
  46.        
  47.        
  48.        
  49.        
  50.         long endTime=System.currentTimeMillis();
  51.         System.out.println("\n\ntime elapsed in sorting is "+ (endTime-startTime)+" Milliseconds \n\n");
  52.        
  53.         System.out.println("The number "+to_find+" is at location  "+ at_location+" using algorithm "+ALGO);
  54.        
  55.        
  56.        
  57.     }
  58.     public static int binarysearch(double input[],double to_find)
  59.     {
  60.         int index=0;
  61.         int left=0;
  62.      
  63.         int right=input.length-1;
  64.         int mid=0;
  65.      
  66.         while(left<=right)
  67.         {
  68.             mid=(left+right)/2;
  69.             if(to_find==input[mid])
  70.                 return mid+1;
  71.             if(to_find<input[mid])
  72.                 right=mid-1;
  73.             if(to_find>input[mid])
  74.                 left=mid+1;
  75.            
  76.         }
  77.        
  78.        
  79.        
  80.         return -1;
  81.     }
  82.     public static int linearsearch(double input[],double to_find)
  83.     {
  84.             int index=0;
  85.             for(int i=0;i<input.length;i++)
  86.             {
  87.                 if(input[i]==to_find)
  88.                 {
  89.                     return i+1;
  90.                 }
  91.             }
  92.            
  93.         return -1;
  94.     }
  95.     public static void display(double [] input)
  96.     {
  97.        
  98.         for(int i=0;i<input.length;i++)
  99.         {
  100.             System.out.println(input[i]+" ");
  101.         }
  102.     }
  103.      public static void randinput()
  104.     {
  105.         Scanner sc=new Scanner(System.in);
  106.         System.out.println(" BUBBLE SORT \n ENTER TOTAL NUMBERS TO SORT");
  107.         try{
  108.         n=Integer.parseInt(sc.nextLine());
  109.         }catch(Exception e)
  110.         {
  111.             System.out.println(" Please Enter a valid integer "+e);
  112.         }  
  113.         input=new double[n];
  114.         for(int i=0;i<n;i++)
  115.         {
  116.             input[i]=Math.random()*1000;
  117.         }
  118.     }
  119.     public static void input()
  120.     {
  121.         Scanner sc=new Scanner(System.in);
  122.         System.out.println(" \n ENTER TOTAL NUMBERS TO SORT");
  123.         try{
  124.         n=Integer.parseInt(sc.nextLine());
  125.         }catch(Exception e)
  126.         {
  127.             System.out.println(" Please Enter a valid integer "+e);
  128.         }  
  129.         input=new double[n];
  130.  
  131.         for(int i=0;i<n;i++)
  132.         {
  133.             input[i]=sc.nextInt();
  134.         }
  135.         System.out.println(" INPUT RECORDED ");
  136.         System.out.println(" Numbers are getting sorted by algorithm: "+ ALGO);
  137.  
  138.     }
  139.    
  140. }
Advertisement
Add Comment
Please, Sign In to add comment