import java.util.*;
import java.util.Random;
class producer extends Thread
{
Queue q;
producer(Queue q1)
{
q = q1;
}
public void run()
{
int i;
int [] a;
int element;
// while(true)
//
{
element = q.binarySearch(2);
//}
// System.out.println(element);
if (element!=-1)
{
System.out.println("ELEMENT Found");
}
else
{
System.out.println("ELEMENT not Found");
}
}
}
class consumer extends Thread
{
Queue q;
consumer(Queue q1)
{
q = q1;
}
public void run()
{
int i;
int [] a;
int element;
//
while(true)
// {
System.out.println("Thread2 running");
System.out.println("enter the elements");
// Scanner
sc=new Scanner(System.in);
// a[i]=sc.nextInt();
element = q.binarySearch(67);
// }
// System.out.println(element);
if (element!=-1)
{
System.out.println("ELEMENT Found");
}
else
{
System.out.println("ELEMENT not Found");
}
}
}
class Queue
{
int a[];
// int f,r,n;
Queue()
{
a = new int[5];
a[0]=19;
a[1]=45;
a[2]=56;
a[3]=78;
a[4]=90;
//
n=10;
// f=r=0;
}
// public void input( )
// {
// for(int i=0;i<a.length - 1;i++)
// {
// System.out.println("enter
the elements");
// Scanner
sc=new Scanner(System.in);
// a[i]=sc.nextInt();
// }
//
// for(int i=0;i<a.length -
1;i++)
// {
// System.out.println("The
elements are");
// Scanner
sc=new Scanner(System.in);
// System.out.println(a[i]);
// }
// }
//
public int binarySearch(int x )
{
int lowerVal = 0;
int higherVal = a.length - 1;
int midVal;
while( lowerVal <= higherVal )
{
System.out.println("Lower val "+lowerVal+ " Higher val "+higherVal );
midVal = ( lowerVal + higherVal ) /
2; // Finding mid value using lower value plus higher
value divided by two.
System.out.println("Mid val :"+midVal );
System.out.println("Search Item :
" + x );
if( a[ midVal] <
0 ){ // Searching in lower half using compareTo method.
System.out.println("Searching in lower half : " + lowerVal );
lowerVal = midVal + 1;
}
else if( a[ midVal ]
> 0 ){ // Searching in upper half using compareTo method
higherVal = midVal - 1;
System.out.println("Searching in upper half : " + higherVal );
}
else
return midVal;
}
return -1; // NOT_FOUND = -1
}
}
public class one {
public static void main(String[] args)
{
Queue q1 = new Queue();
producer p = new producer(q1);
consumer c = new consumer(q1);
// q1.input();
System.out.println("Thread1 running");
p.start();
c.start();
}
}
OUTPUT:
Thread1 running
Lower val 0 Higher val 4
Mid val :2
Search Item : 2
Searching in upper half : 1
Lower val 0 Higher val 1
Mid val :0
Search Item : 2
Searching in upper half : -1
ELEMENT not Found
Thread2 running
enter the elements
Lower val 0 Higher val 4
Mid val :2
Search Item : 67
Searching in upper half : 1
Lower val 0 Higher val 1
Mid val :0
Search Item : 67
Searching in upper half : -1
ELEMENT not Found
No comments:
Post a Comment