int BinarySearch(int *array, int number_of_elements, int key)
{
int low = 0, high = number_of_elements-1, mid;
while(low <= high)
{
mid = (low + high)/2;
if(array[mid] < key)
{
low = mid + 1;
}
else if(array[mid] == key)
{
return mid;
}
else if(array[mid] > key)
{
high = mid-1;
}
}
return -1;
}
int main()
{
int number_of_elements;
scanf("%d",&number_of_elements);
int array[number_of_elements];
int iter;
for(iter = 1;iter < number_of_elements;iter++)
{
if(array[iter] < array[iter - 1])
{
printf("Given input is
not sorted\n");
return 0;
}
}
int key;
scanf("%d",&key);
/* Calling this functions searches for the key and returns its index. It returns -1 if key is not found.*/
int index;
index = BinarySearch(array,number_of_elements,key);
if(index==-1)
{
printf("Element not found\n");
}
else
{
printf("Element is at index %d\n",index);
}
return 0;
}
Showing posts with label SEARCHING. Show all posts
Showing posts with label SEARCHING. Show all posts
Thursday, January 9, 2014
TO IMPLEMENT BINARY SEARCH ALGORITHM IN C
Friday, October 25, 2013
DEPTH FIRST SEARCH IN JAVA.
/*
TOPIC: DEPTH FIRST SEARCH IN JAVA.
SEIT
DATE:13/9/2012
*/
import java.io.*;
class Stack
{
int stk[]=new int[20];
int top;
Stack()
{
top=-1;
}
void push(int i)
{
if(top==19)
System.out.println("Stack Overflow!");
else
stk[++top]=i;
}
int peek()
{
return stk[top];
}
int pop()
{
if(this.isEmpty())
{
System.out.println("underflow");
return 0;}
else
{
System.out.println(stk[top]);
return stk[top--];
}}
boolean isEmpty()
{
if(top<0)
return true;
else
return false;
}
}
class vertex
{
int no;
boolean isv;
vertex()
{
isv=false;
}
vertex(int b)
{
no=b;
isv=false;
}
}
class graph
{
vertex vl[];
int adjm[][];
int nv=0;
Stack s;
graph()
{
System.out.println("our default size of vertex list is 20 and matrix is 20x20");
vl=new vertex[20];
adjm=new int[20][20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
adjm[i][j]=0;
}
}
s=new Stack();
}
void addVertex(int l)
{
vl[nv++]=new vertex(l);
}
void addEdge(int a,int b)throws Exception
{ if(a>20 || b>20)
throw new Exception("Vertex cannot be defined out of the boundary");
adjm[a][b]=1;
adjm[b][a]=1;
}
void display()
{
for (int i=0;i<nv;i++)
System.out.print(vl[i].no+" ");
}
void display(int v)
{
System.out.println(vl[v].no);
}
void dfs()
{
vl[0].isv=true;
display (0);
s.push(0);
for(int j=1;j<nv;j++)
{
int v=getAdj(s.peek());
if(v==-1)
s.pop();
else
{
vl[v].isv=true;
display (v);
s.push(v);
}
}
for(int i=0;i<nv;i++)
{
vl[i].isv=false;
}
}
int getAdj(int v)
{
for(int i=0;i<nv;i++)
if(v!=i)
if(adjm[v][i]==1 && vl[i].isv==false)
return i;
return -1;
}
}
class main
{
public static void main(String args[])throws Exception
{
graph g=new graph();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int ch;
PrintStream p=System.out;
one: for(int i=10;i>0;i++)
{
p.println();
p.println("1>INSERT VERTEX");
p.println("2>DISPLAY");
p.println("3>Depth First Search");
p.println("4>ADD EDGE");
p.println("5>EXIT");
ch=Integer.parseInt(br.readLine());
switch (ch)
{
case 1:
int o;
p.println("enter object");
o=Integer.parseInt(br.readLine());
g.addVertex(o);
break;
case 2:
g.display();
break;
case 3:
g.dfs();
break;
case 4:
p.println("enter start vertex");
int a=Integer.parseInt(br.readLine());
p.println("enter end vertex ");
int b=Integer.parseInt(br.readLine());
g.addEdge(a,b);
break;
case 5:
g=null;
System.gc();
break one;
}
}
System.out.println("thank u coded by TG");
}
}
/*
OUTPUT:
our default size of vertex list is 20 and matrix is 20x20
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
1
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
2
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
4
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
1
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
2
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
1
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
2
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
2
1 2 3 4
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
3
1
2
4
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
5
thank u coded by TG
*/
TOPIC: DEPTH FIRST SEARCH IN JAVA.
SEIT
DATE:13/9/2012
*/
import java.io.*;
class Stack
{
int stk[]=new int[20];
int top;
Stack()
{
top=-1;
}
void push(int i)
{
if(top==19)
System.out.println("Stack Overflow!");
else
stk[++top]=i;
}
int peek()
{
return stk[top];
}
int pop()
{
if(this.isEmpty())
{
System.out.println("underflow");
return 0;}
else
{
System.out.println(stk[top]);
return stk[top--];
}}
boolean isEmpty()
{
if(top<0)
return true;
else
return false;
}
}
class vertex
{
int no;
boolean isv;
vertex()
{
isv=false;
}
vertex(int b)
{
no=b;
isv=false;
}
}
class graph
{
vertex vl[];
int adjm[][];
int nv=0;
Stack s;
graph()
{
System.out.println("our default size of vertex list is 20 and matrix is 20x20");
vl=new vertex[20];
adjm=new int[20][20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
adjm[i][j]=0;
}
}
s=new Stack();
}
void addVertex(int l)
{
vl[nv++]=new vertex(l);
}
void addEdge(int a,int b)throws Exception
{ if(a>20 || b>20)
throw new Exception("Vertex cannot be defined out of the boundary");
adjm[a][b]=1;
adjm[b][a]=1;
}
void display()
{
for (int i=0;i<nv;i++)
System.out.print(vl[i].no+" ");
}
void display(int v)
{
System.out.println(vl[v].no);
}
void dfs()
{
vl[0].isv=true;
display (0);
s.push(0);
for(int j=1;j<nv;j++)
{
int v=getAdj(s.peek());
if(v==-1)
s.pop();
else
{
vl[v].isv=true;
display (v);
s.push(v);
}
}
for(int i=0;i<nv;i++)
{
vl[i].isv=false;
}
}
int getAdj(int v)
{
for(int i=0;i<nv;i++)
if(v!=i)
if(adjm[v][i]==1 && vl[i].isv==false)
return i;
return -1;
}
}
class main
{
public static void main(String args[])throws Exception
{
graph g=new graph();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int ch;
PrintStream p=System.out;
one: for(int i=10;i>0;i++)
{
p.println();
p.println("1>INSERT VERTEX");
p.println("2>DISPLAY");
p.println("3>Depth First Search");
p.println("4>ADD EDGE");
p.println("5>EXIT");
ch=Integer.parseInt(br.readLine());
switch (ch)
{
case 1:
int o;
p.println("enter object");
o=Integer.parseInt(br.readLine());
g.addVertex(o);
break;
case 2:
g.display();
break;
case 3:
g.dfs();
break;
case 4:
p.println("enter start vertex");
int a=Integer.parseInt(br.readLine());
p.println("enter end vertex ");
int b=Integer.parseInt(br.readLine());
g.addEdge(a,b);
break;
case 5:
g=null;
System.gc();
break one;
}
}
System.out.println("thank u coded by TG");
}
}
/*
OUTPUT:
our default size of vertex list is 20 and matrix is 20x20
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
1
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
2
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
1
enter object
4
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
1
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
2
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
1
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
2
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
2
1 2 3 4
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
3
1
2
4
3
1>INSERT VERTEX
2>DISPLAY
3>Depth First Search
4>ADD EDGE
5>EXIT
5
thank u coded by TG
*/
BREADTH FIRST SEARCH IN JAVA.
/*
TOPIC: BREADTH FIRST SEARCH IN JAVA.
DATE:13/9/2012
*/
import java.io.*;
class Node
{
int element;
Node next;
Node()
{
this(0,null);
}
Node(int e,Node n)
{
element=e;
next=n;
}
int getElement()
{
return element;
}
Node getNext()
{
return next;
}
void setElement(int newEl)
{
element=newEl;
}
void setNext(Node newNext)
{
next=newNext;
}
}
class Queue
{
Node tail;
Node head;
int size;
Queue()
{
this(null,null,0);
}
Queue(Node h,Node t,int size)
{
head=h;
tail=t;
this.size=size;
System.out.println("NEW QUEUE CREATED");
}
void insert(int obj)
{
Node node=new Node();
node.setElement(obj);
node.setNext(null);
if(size==0)
head=node;
else
tail.setNext(node);
tail=node;
size++;
}
boolean isEmpty()
{
if (size==0)
return true;
else
return false;
}
int remove() throws Exception
{
int obj;
if(isEmpty())
throw new Exception("Queue is empty action prohibited");
obj=head.getElement();
head=head.getNext();
size--;
if(isEmpty())
tail=null;
return obj;
}
void display() throws Exception
{
Node q=head;
if(isEmpty())
throw new Exception("cannot display as Queue is empty");
else
{
for(int i=0;q.next!=null;i++)
{
System.out.println(q.getElement());
q=q.next;
}
}
}
void destroy()throws Exception
{
if(isEmpty())
throw new Exception("Queue is empty");
head=null;
tail=null;
size=0;
}
}
class vertex
{
int no;
boolean isv;
vertex()
{
isv=false;
}
vertex(int b)
{
no=b;
isv=false;
}
}
class graph
{
vertex vl[];
int adjm[][];
int nv=0;
Queue q;
graph()
{
System.out.println("our default size of vertex list is 20 and matrix is 20x20");
vl=new vertex[20];
adjm=new int[20][20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
adjm[i][j]=0;
}
}
q=new Queue();
}
void addVertex(int l)
{
vl[nv++]=new vertex(l);
}
void addEdge(int a,int b)throws Exception
{ if(a>20 || b>20)
throw new Exception("Vertex cannot be defined out of the boundary");
adjm[a][b]=1;
adjm[b][a]=1;
}
void display()
{
for (int i=0;i<nv;i++)
System.out.print(vl[i].no+" ");
}
void display(int v)
{
System.out.println(vl[v].no);
}
void bfs() throws Exception
{
vl[0].isv=true;
display (0);
q.insert(0);
int v2;
while(!q.isEmpty())
{
int v1=q.remove();
while((v2=getAdj(v1))!=-1)
{
vl[v2].isv=true;
display(v2);
q.insert(v2);
}
}
for(int i=0;i<nv;i++)
{
vl[i].isv=false;
}
}
int getAdj(int v)
{
for(int i=0;i<nv;i++)
if(v!=i)
if(adjm[v][i]==1 && vl[i].isv==false)
return i;
return -1;
}
}
class main
{
public static void main(String args[])throws Exception
{
graph g=new graph();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int ch;
PrintStream p=System.out;
one: for(int i=10;i>0;i++)
{
p.println();
p.println("1>INSERT VERTEX");
p.println("2>DISPLAY");
p.println("3>Breadth First Search");
p.println("4>ADD EDGE");
p.println("5>EXIT");
ch=Integer.parseInt(br.readLine());
switch (ch)
{
case 1:
int o;
p.println("enter int");
o=Integer.parseInt(br.readLine());
g.addVertex(o);
break;
case 2:
g.display();
break;
case 3:
g.bfs();
break;
case 4:
p.println("enter start vertex");
int a=Integer.parseInt(br.readLine());
p.println("enter end vertex ");
int b=Integer.parseInt(br.readLine());
g.addEdge(a,b);
break;
case 5:
g=null;
System.gc();
break one;
}
}
System.out.println("thank u coded by 6483");
}
}
/*
OUTPUT:
our default size of vertex list is 20 and matrix is 20x20
NEW QUEUE CREATED
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
11
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
22
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
33
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
1
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
2
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
1
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
2
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
2
11 22 33 44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
3
11
22
33
44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
5
*/
TOPIC: BREADTH FIRST SEARCH IN JAVA.
DATE:13/9/2012
*/
import java.io.*;
class Node
{
int element;
Node next;
Node()
{
this(0,null);
}
Node(int e,Node n)
{
element=e;
next=n;
}
int getElement()
{
return element;
}
Node getNext()
{
return next;
}
void setElement(int newEl)
{
element=newEl;
}
void setNext(Node newNext)
{
next=newNext;
}
}
class Queue
{
Node tail;
Node head;
int size;
Queue()
{
this(null,null,0);
}
Queue(Node h,Node t,int size)
{
head=h;
tail=t;
this.size=size;
System.out.println("NEW QUEUE CREATED");
}
void insert(int obj)
{
Node node=new Node();
node.setElement(obj);
node.setNext(null);
if(size==0)
head=node;
else
tail.setNext(node);
tail=node;
size++;
}
boolean isEmpty()
{
if (size==0)
return true;
else
return false;
}
int remove() throws Exception
{
int obj;
if(isEmpty())
throw new Exception("Queue is empty action prohibited");
obj=head.getElement();
head=head.getNext();
size--;
if(isEmpty())
tail=null;
return obj;
}
void display() throws Exception
{
Node q=head;
if(isEmpty())
throw new Exception("cannot display as Queue is empty");
else
{
for(int i=0;q.next!=null;i++)
{
System.out.println(q.getElement());
q=q.next;
}
}
}
void destroy()throws Exception
{
if(isEmpty())
throw new Exception("Queue is empty");
head=null;
tail=null;
size=0;
}
}
class vertex
{
int no;
boolean isv;
vertex()
{
isv=false;
}
vertex(int b)
{
no=b;
isv=false;
}
}
class graph
{
vertex vl[];
int adjm[][];
int nv=0;
Queue q;
graph()
{
System.out.println("our default size of vertex list is 20 and matrix is 20x20");
vl=new vertex[20];
adjm=new int[20][20];
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
adjm[i][j]=0;
}
}
q=new Queue();
}
void addVertex(int l)
{
vl[nv++]=new vertex(l);
}
void addEdge(int a,int b)throws Exception
{ if(a>20 || b>20)
throw new Exception("Vertex cannot be defined out of the boundary");
adjm[a][b]=1;
adjm[b][a]=1;
}
void display()
{
for (int i=0;i<nv;i++)
System.out.print(vl[i].no+" ");
}
void display(int v)
{
System.out.println(vl[v].no);
}
void bfs() throws Exception
{
vl[0].isv=true;
display (0);
q.insert(0);
int v2;
while(!q.isEmpty())
{
int v1=q.remove();
while((v2=getAdj(v1))!=-1)
{
vl[v2].isv=true;
display(v2);
q.insert(v2);
}
}
for(int i=0;i<nv;i++)
{
vl[i].isv=false;
}
}
int getAdj(int v)
{
for(int i=0;i<nv;i++)
if(v!=i)
if(adjm[v][i]==1 && vl[i].isv==false)
return i;
return -1;
}
}
class main
{
public static void main(String args[])throws Exception
{
graph g=new graph();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
int ch;
PrintStream p=System.out;
one: for(int i=10;i>0;i++)
{
p.println();
p.println("1>INSERT VERTEX");
p.println("2>DISPLAY");
p.println("3>Breadth First Search");
p.println("4>ADD EDGE");
p.println("5>EXIT");
ch=Integer.parseInt(br.readLine());
switch (ch)
{
case 1:
int o;
p.println("enter int");
o=Integer.parseInt(br.readLine());
g.addVertex(o);
break;
case 2:
g.display();
break;
case 3:
g.bfs();
break;
case 4:
p.println("enter start vertex");
int a=Integer.parseInt(br.readLine());
p.println("enter end vertex ");
int b=Integer.parseInt(br.readLine());
g.addEdge(a,b);
break;
case 5:
g=null;
System.gc();
break one;
}
}
System.out.println("thank u coded by 6483");
}
}
/*
OUTPUT:
our default size of vertex list is 20 and matrix is 20x20
NEW QUEUE CREATED
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
11
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
22
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
33
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
1
enter int
44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
1
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
0
enter end vertex
2
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
1
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
4
enter start vertex
2
enter end vertex
3
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
2
11 22 33 44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
3
11
22
33
44
1>INSERT VERTEX
2>DISPLAY
3>Breadth First Search
4>ADD EDGE
5>EXIT
5
*/
Labels:
BFS,
BREADTH FIRST SEARCH,
Data Structures In JAVA,
JAVA,
NODE,
SEARCHING
BINARY SEARCH ALGORITHM IN JAVA.
/*
TOPIC: BINARY SEARCH ALGORITHM.
DATE:9/10/2012
*/
import java.util.*;
import java.io.*;
class binsearch
{
int a[];
int size,middle,left,right,key;
PrintStream p=System.out;
Scanner sc=new Scanner(System.in);
binsearch()
{
sc=new Scanner(System.in);
}
void getData()
{
p.println("enter array size");
size=sc.nextInt();
a=new int[size];
p.println("enter array the array");
for(int i=0;i<a.length;i++)
{
p.print("a["+(i)+"] =");
a[i]=sc.nextInt();
}
p.println("the array is");
for(int i=0;i<a.length;i++)
{
p.print(a[i]+" ");
}
p.println();
this.SelectionSort();
this.getKey();
}
void getKey()
{
p.print("enter key: ");
key=sc.nextInt();
this.binsearch();
}
void swap(int i,int j)
{
a[i]=a[i]*a[j];
a[j]=a[i]/a[j];
a[i]=a[i]/a[j];
}
void display()
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
void binsearch()
{
boolean flag=true;
left=0;
right=size-1;
while(left<=right)
{
middle=(left+right)/2;
if(key==a[middle])
{
flag=false;
p.println("the element is found at the "+middle+" position");
break;
}
if(key<a[middle])
{
right=middle - 1;
}
if(key>a[middle])
{
left=middle + 1;
}
}
if(flag)
p.println("element not found");
}
void SelectionSort()
{
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[i])
swap(j,i);
}
}
System.out.println("AFTER SORTING--we used seletion sort");
display();
}
public static void main(String args[])throws Exception
{
binsearch b=new binsearch();
b.getData();
}
}
/*
OUTPUT:
enter array size
8
enter array the array
a[0] =25
a[1] =1
a[2] =245
a[3] =655
a[4] =22
a[5] =885
a[6] =65
a[7] =36
the array is
25 1 245 655 22 885 65 36
AFTER SORTING--we used seletion sort
1 22 25 36 65 245 655 885
enter key: 655
the element is found at the 6
*/
TOPIC: BINARY SEARCH ALGORITHM.
DATE:9/10/2012
*/
import java.util.*;
import java.io.*;
class binsearch
{
int a[];
int size,middle,left,right,key;
PrintStream p=System.out;
Scanner sc=new Scanner(System.in);
binsearch()
{
sc=new Scanner(System.in);
}
void getData()
{
p.println("enter array size");
size=sc.nextInt();
a=new int[size];
p.println("enter array the array");
for(int i=0;i<a.length;i++)
{
p.print("a["+(i)+"] =");
a[i]=sc.nextInt();
}
p.println("the array is");
for(int i=0;i<a.length;i++)
{
p.print(a[i]+" ");
}
p.println();
this.SelectionSort();
this.getKey();
}
void getKey()
{
p.print("enter key: ");
key=sc.nextInt();
this.binsearch();
}
void swap(int i,int j)
{
a[i]=a[i]*a[j];
a[j]=a[i]/a[j];
a[i]=a[i]/a[j];
}
void display()
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
void binsearch()
{
boolean flag=true;
left=0;
right=size-1;
while(left<=right)
{
middle=(left+right)/2;
if(key==a[middle])
{
flag=false;
p.println("the element is found at the "+middle+" position");
break;
}
if(key<a[middle])
{
right=middle - 1;
}
if(key>a[middle])
{
left=middle + 1;
}
}
if(flag)
p.println("element not found");
}
void SelectionSort()
{
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[i])
swap(j,i);
}
}
System.out.println("AFTER SORTING--we used seletion sort");
display();
}
public static void main(String args[])throws Exception
{
binsearch b=new binsearch();
b.getData();
}
}
/*
OUTPUT:
enter array size
8
enter array the array
a[0] =25
a[1] =1
a[2] =245
a[3] =655
a[4] =22
a[5] =885
a[6] =65
a[7] =36
the array is
25 1 245 655 22 885 65 36
AFTER SORTING--we used seletion sort
1 22 25 36 65 245 655 885
enter key: 655
the element is found at the 6
*/
Labels:
BINARY SEARCH ALGORITHM.,
Data Structures In JAVA,
DSA,
JAVA,
SEARCHING,
SORTING
Subscribe to:
Posts (Atom)