Difference between revisions of "Find Element x Using Binary Search"

From CompSciWiki
Jump to: navigation, search
Line 89: Line 89:
  
 
|Solution=
 
|Solution=
Create a method to search the array.  The method will accept accepts an array of integers and an integer value x.  The method will return an integer value indicating the number of times x occurs in the array
+
Create a method to search the array.  The method will accept accepts an array of integers and an integer value x.  The method will return true if the value x occurs in the array, otherwise the method should return false.
 
<pre>
 
<pre>
private static int countElementX (int [] array, int x)
+
private static boolean binarySearch (int [] array, int x)
 
{
 
{
 
}
 
}
 
</pre>
 
</pre>
Initialize the variables we will need.  We will need an integer to count the number of times x occurs in the array, and an integer to iterate through the array.
+
Declare the variables we will need.  We will integers to store the upper and lower bounds of our search.  We will need an integer to calculate the middle of our search bounds.  We will also need a boolean to indicate whether our x value has been found.
 
<pre>
 
<pre>
int count = 0;
+
boolean found = false;
int i;
+
int upperBound;
 +
int lowerBound;
 +
int mid;
 
</pre>
 
</pre>
  
Before trying to iterate through the array, make sure it is properly initialized.  If the array is null, we cannot search the array, so we will return 0.
+
Before trying to search the array, make sure it is properly initialized.  If the array is null, we cannot search the array, so we will return false.
 
<pre>
 
<pre>
 
if (array != null)
 
if (array != null)
Line 108: Line 110:
 
</pre>
 
</pre>
  
Iterate through each element of the array.  On each iteration, compare the current element with the value of x. If the values are equal, increment the counter variable.
+
Initialize the values of the bounds of our search.
<pre>
+
 
for (i = 0; i < array.length; i++){
+
    if (array [i] == x)
+
        ++counter;
+
} // for
+
</pre>
+
  
Return the number of times element x occured in the array.
+
Return true if the value x occurs in the array, otherwise return false.
 
<pre>
 
<pre>
return count;
+
return found;
 
</pre>
 
</pre>
 
}}
 
}}

Revision as of 13:30, 6 April 2010

Back to the Program-A-Day homepage

Problem

Consider the provided main method which provides a sorted array of integers, and an integer value x. Write a method which performs a binary search of the array for the value x.

    public static void main(String [] args)
    {
        int [] array = {1,2,3,3,3,5,6,7};   // the array to search
        int x = 4;                          // the element we are searching for
        boolean found = false;


        // search for element x
        found = binarySearch (array, x);

        // provide some nice output
        if (found)
            System.out.println("The element " + x + " exists in the array");
        else
        System.out.println("The element " + x + " does not exists in the array");

    } // main

The method should accepts an array of integers and an integer value x. The method should perform a binary search of the array. The method should return true if the value x occurs in the array, otherwise the method should return false.

 

...by students

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

Create a method to search the array. The method will accept accepts an array of integers and an integer value x. The method will return true if the value x occurs in the array, otherwise the method should return false.

private static boolean binarySearch (int [] array, int x)
{
}

Declare the variables we will need. We will integers to store the upper and lower bounds of our search. We will need an integer to calculate the middle of our search bounds. We will also need a boolean to indicate whether our x value has been found.

boolean found = false;
int upperBound;
int lowerBound;
int mid;

Before trying to search the array, make sure it is properly initialized. If the array is null, we cannot search the array, so we will return false.

if (array != null)
{
}

Initialize the values of the bounds of our search.


Return true if the value x occurs in the array, otherwise return false.

return found;

Code

Solution Code

Back to the Program-A-Day homepage