0% found this document useful (0 votes)
41 views4 pages

Exp 2

The document describes 4 experiments on data structures and algorithms in Java: 1) A binary search program to find an element in a sorted list. It provides the code and output. 2) A bubble sort program to sort a list of elements. It provides the code and output. 3) A merge sort program to sort two sorted lists. It provides the code to merge the lists and the output. 4) A StringBuffer program using methods like delete and deleteCharAt to manipulate strings. It provides the code and output.

Uploaded by

Jayanth Ranga
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)
41 views4 pages

Exp 2

The document describes 4 experiments on data structures and algorithms in Java: 1) A binary search program to find an element in a sorted list. It provides the code and output. 2) A bubble sort program to sort a list of elements. It provides the code and output. 3) A merge sort program to sort two sorted lists. It provides the code to merge the lists and the output. 4) A StringBuffer program using methods like delete and deleteCharAt to manipulate strings. It provides the code and output.

Uploaded by

Jayanth Ranga
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/ 4

EXPERIMENT - 2

a) Implementation of Binary search mechanism


AIM: To write a JAVA program to search for an element in a given list of elements using
binary search mechanism
SOURCE-CODE:
import java.util.Scanner;
class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last, middle;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of elements:");
n = s.nextInt();
System.out.println("Enter elements in sorted order:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.println("Enter the search value:");
num = s.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( a[middle] < num )
first = middle + 1;
else if ( a[middle] == num )
{
System.out.println("number found");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println( " Number is not found");
}
}
OUT-PUT:
Enter total number of elements:
5
Enter elements:
24689
Enter the search value:
8
WWW.KVRSOFTWARES.BLOGSPOT.COM
number found
b) Bubble sort
AIM: To write a JAVA program to sort for an element in a given list of elements using
bubble sort
SOURCE-CODE:
import java.util.Scanner;
class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of elements:");
n = s.nextInt();
System.out.println("Enter elements:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}
OUT-PUT:
Enter total number of elements:
10
Enter elements:
3257689140
The sorted elements are:
0123456789
c) Merge sort:
AIM: To write a JAVA program to sort for an element in a given list of elements using
merge sort
SOURCE-CODE:
import java.util.*;
class mergedemo
WWW.KVRSOFTWARES.BLOGSPOT.COM
{
public static void main(String args[])
{
int n1,n2,i,j,k;
int a[ ]=new int[20];
int b[ ]=new int[20];
int c[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter number of elements in first array:");
n1 = s.nextInt();
System.out.println("Enter sorted elements of first array:");
for (i = 0; i < n1; i++)
a[i] = s.nextInt();
System.out.println("Enter number of elements in second array:");
n2 = s.nextInt();
System.out.println("Enter sorted elements of second array:");
for (j = 0; j < n2; j++)
b[j] = s.nextInt();
i = 0;
j = 0;
k = 0;
while((i < n1) && (j <n2))
{
if(a[i] > b[j])
c[k++] = b[j++];
else
c[k++] = a[i++];
}
while(i < n1)
c[k++] = a[i++];
while(j < n2)
c[k++] = b[j++];
System.out.println("After merging the elements are:\n");
for(i = 0; i < (n1 + n2); i++)
System.out.print("\t"+c[i]);
}
}
OUT-PUT:
Enter number of elements in first array:
6
Enter elements of first array:
8 9 12 13 15 18
Enter number of elements in second array:
5
Enter elements of second array:
6 7 10 11 20
After merging the elements are:
6 7 8 9 10 11 12 13 15 18 20

WWW.KVRSOFTWARES.BLOGSPOT.COM
d) Implementing StringBuffer
AIM: To write a JAVA program using StringBuffer to delete, remove character
SOURCE-CODE:
class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello World");
sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
System.out.println(sb2);
sb2.delete(0, sb2.length());
System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
OUT-PUT:
World
Some Content
ello World

WWW.KVRSOFTWARES.BLOGSPOT.COM

You might also like