0% found this document useful (0 votes)
8 views3 pages

Bubble Binary

Uploaded by

Sohini G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Bubble Binary

Uploaded by

Sohini G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Class BubbleBinary contains an array of n integers(n<=100).

Class name: BubbleBinary


Data memebers/instance variables :
list: an array of n integers
n : size of the array
Member functions/ methods :
BubbleBinary(int n1) : constructor to initialize array to 0 .
n1 to n;
void readarray( ) : input n integers in array
void displayarray( ) : display the sorted array
void bubble( ) : sort the array in bubble sort method
void binarysearch( int x ) : search the value x in the array with
the help of binary search technique
and display it with its position.

import java.util.*;
class BubbleBinary
{
int list[];
int n;

BubbleBinary(int n1)
{
list= new int[100];
n=n1;
for(int i=0;i<n;i++)
list[i]=0;
}

void readarray()
{
Scanner sc = new Scanner(System.in);
for(int i=0;i<n;i++)
{
list[i]=sc.nextInt();
}
}
void bubble()
{
int i,j,temp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (list[j + 1] < list[j])
{
temp = list[j + 1];
list[j + 1] = list[j];
list[j] = temp;
}
}
}

}
void binarysearch( int x )
{
int start,p=-1;
start = 0;
int last = n-1, mid = 0;
while (start <= last)
{
mid = (start + last) / 2;
if (list[mid] > x)
{
last = mid - 1;
}
else if (list[mid] < x)
{
start = mid + 1;
}
else
{
p=mid+1;
break;
}
}
if (p>=0)
{
System.out.println("Number "+x+" Found At " + p + " position");
}
else
{
System.out.println("Number not Found In The List!");
}
}
void displayarray()
{
for (int i = 0; i < n; i++)
{
System.out.println(list[i]);
}
}
public static void main()
{
BubbleBinary bb = new BubbleBinary(5);
bb.readarray();
bb.bubble();
bb.displayarray();
bb.binarysearch(10);

}
}

You might also like