Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Binary Search Base Case help
Author Message
2sticks

Posted: Tue Oct 24, 2017 7:13 pm   Post subject: Binary Search Base Case help

import java.io.*;
import java.util.*;
import java.text.*;

public class binarySearch
{

static final int MAX = 1500; // 1500 Records

//Linear Search void Method
static void linearSearch (String[] phoneBook) throws IOException
{
String search;

int counter = 0; // A counter for the number of comparisons made

System.out.println ("Enter the phone number to be searched for: "); // Get the requested number

for (int i = 0 ; i < MAX ; i++)
{
counter++;
if (phoneBook [i].equals (search)) //If linear search method found the number
{
System.out.println ("The phone number was found at Index number: " + i);
System.out.println ("There were " + counter + " comparisons made.");
break;
}

}
if (counter == 1500) //Linear search did not find the number
{

System.out.println ("There were " + counter + " comparisons made.");

}
}

//Binary Search void Method
public static int binarySearchMethod (String[] phoneBook, String search, int min, int max, int count) throws IOException
{

count++; // Count the number of comparisons

int mid = (min + max) / 2;

if (phoneBook [mid].compareTo (search) > 0)
return binarySearchMethod (phoneBook, search, min, mid, count);

else if (phoneBook [mid].compareTo (search) < 0)
return binarySearchMethod (phoneBook, search, mid, max, count);

if (mid == 1)
return 0;

}

public static void main (String str[]) throws IOException
{
DecimalFormat df = new DecimalFormat ("#");
String phoneNumber, searchOption;
String line;
int counter = 0;
int select;

String[] phoneBook = new String [MAX]; //"Phone book" array

//Inputs all the records from the text file and stores it into an array

{
phoneBook [counter] = line;
counter++;
}

System.out.println ("How would you like to search for the phone number? \n 1)Linear Search \n 2)Binary Search");

//Choice of method of searching
switch (select)
{
case 1: //Linear

linearSearch (phoneBook);

break;

case 2: //Binary
String search;
System.out.println ("Enter the phone number to be searched for: ");
int count = 0;
int position = binarySearchMethod (phoneBook, search, 0, MAX - 1, count);

System.out.println ("Search term is at: " + position + "\n Number of comparisions: " + count);

break;

// A form of validation if the user does not pick one of the options
default:
System.out.println ("Pick again");
break;

}
}
}

The stuff above is the code, how would I add for a base case in the binary search method to stop the infinite loop if there is no number present in the array. Also why is the count++ not working as well?

Insectoid

Posted: Wed Oct 25, 2017 9:28 am   Post subject: RE:Binary Search Base Case help

What happens to min and max as the tree progresses in the search?
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: