I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and over and i cant figure out why :sad: . Can any one help?

import java.util.Random;

public class BinarySearch
{
Random RandomGenerator; // declares the random generator that generates the array
int maxLength; // initialises the varible max lenght
private int[] a; // the array is declared

public BinarySearch()
{
RandomGenerator = new Random(); // create random generator
maxLength = 100; // set the maximum value for max lenght
}

public void BinarySearchFunction(int n)
{
a = new int[n]; // create array
for (int i=0; i<n; i++)
{
a = RandomGenerator.nextInt(maxLength); // new random numbers
}

print(a);

{
int in, out;

for(out=1; out < a.length; out++) // out is dividing line
{
int temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item

}

print(a); // prints sorted numbers
}
}

public void BinarySearch(int key)
{
int low = 0;
int high = a.length - 1; //zero based array
int middle;

while( low <= high )
{
middle = ( low + high ) / 2;

if( key == a[ middle ] ) {//match
System.out.println("Search key " + a[middle] + " was found at index: " + middle); }
else if( key < a[ middle ] ){
high = middle - 1; //search low end of array
}
else{
low = middle + 1; //search high end of array
}
}

}

public void print(int[] a)
{
for (int i = 0; i < a.length; i++)
System.out.print(a + " ");
System.out.println();
}
}

## All 2 Replies

I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and over and i cant figure out why :sad: . Can any one help?

import java.util.Random;

public class BinarySearch
{
Random RandomGenerator; // declares the random generator that generates the array
int maxLength; // initialises the varible max lenght
private int[] a; // the array is declared

public BinarySearch()
{
RandomGenerator = new Random(); // create random generator
maxLength = 100; // set the maximum value for max lenght
}

public void BinarySearchFunction(int n)
{
a = new int[n]; // create array
for (int i=0; i<n; i++)
{
a = RandomGenerator.nextInt(maxLength); // new random numbers
}

print(a);

{
int in, out;

for(out=1; out < a.length; out++) // out is dividing line
{
int temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item

}

print(a); // prints sorted numbers
}
}

public void BinarySearch(int key)
{
int low = 0;
int high = a.length - 1; //zero based array
int middle;

while( low <= high )
{
middle = ( low + high ) / 2;

if( key == a[ middle ] ) {//match
System.out.println("Search key " + a[middle] + " was found at index: " + middle); }
else if( key < a[ middle ] ){
high = middle - 1; //search low end of array
}
else{
low = middle + 1; //search high end of array
}
}

}

public void print(int[] a)
{
for (int i = 0; i < a.length; i++)
System.out.print(a + " ");
System.out.println();
}
}

Ifinite loop maybe? Low is always less than high?

You never increment low so it's always the same which causes the infinite loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.