Exercise - 2 (Operations, Expressions, Control-flow, Strings)
a). Write a JAVA program to search for an element in a given list of elements using binary
search mechanism.
import java.util.Scanner;
import java.io.*;
class Binarys
{
public static void main(String args[])
{
int c,first,last,middle,n,search,array[];
boolean status=false;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of elements:");
n=s.nextInt();
array=new int[n];
System.out.println("Enter"+n+"integer:");
for(c=0;c<n;c++)
array[c]=s.nextInt();
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Enter value to find:");
search=s.nextInt();
first=0;
last=n-1;
middle=(first+last)/2;
for(int i=0;i<n;i++)
{
if(first<=last)
{
if(array[middle]<search)
first=middle+1;
else if(array[middle]==search)
{
status=true;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
}
if(status==true)
{
System.out.println(search+"found at location"+(middle+1));
}
else
System.out.println(search+"is not found in the list");
}
}
b). Write a JAVA program to sort for an element in a given list of elements using bubble sort.
import java.util.Scanner;
public class Bubblesort
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the size of the array:");
int size=s.nextInt();
System.out.println("enter the values into the array:");
int arr[]=new int[size];
for(int i=0;i<size;i++)
{
arr[i]=s.nextInt();
}
sorting(arr);
}
public static void sorting(int arr[])
{
int n=arr.length;
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(arr[j-1]>arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("the sorted arraty is:");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
}
}
(c). Write a JAVA program to sort for an element in a given list of elements using merge sort.
import java.util.Scanner;
class merge
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter size of the array:");
int size=s.nextInt();
int arr[]=new int[size];
System.out.println("enter elements into the array:");
for(int i=0;i<size;i++)
{
arr[i]=s.nextInt();
}
int start=0;
int end=size-1;
sort(arr,start,end);
System.out.println("the sorted array is:");
for(int j=0;j<size;j++)
{
System.out.print(arr[j]+"\t");
}
}
public static void sort(int arr[],int start,int end)
{
if(start<end)
{
int middle=(start+end)/2;
sort(arr,start,middle);
sort(arr,middle+1,end);
merge(arr,start,middle,end);
}
}
public static void merge(int arr[],int start,int middle,int end)
{
int n1 = middle -start + 1;
int n2 = end - middle;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
{
L[i] = arr[start + i];
}
for (int j=0; j<n2; ++j)
{
R[j] = arr[middle + 1+ j];
}
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
}
(d) Write a JAVA program using StringBufferto delete, remove character.
import java.util.Scanner;
import java.lang.*;
class stringbuffer
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a string you like:");
String str=s.nextLine();
StringBuffer b=new StringBuffer(str);
System.out.println("enter the index you want to delete from:");
int i1=s.nextInt();
System.out.println("to:");
int i2=s.nextInt();
b.delete(i1,i2);
System.out.println("after deletion the new buffer is:"+b);
}
}
a). Write a JAVA program to search for an element in a given list of elements using binary
search mechanism.
import java.util.Scanner;
import java.io.*;
class Binarys
{
public static void main(String args[])
{
int c,first,last,middle,n,search,array[];
boolean status=false;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of elements:");
n=s.nextInt();
array=new int[n];
System.out.println("Enter"+n+"integer:");
for(c=0;c<n;c++)
array[c]=s.nextInt();
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
System.out.println("Enter value to find:");
search=s.nextInt();
first=0;
last=n-1;
middle=(first+last)/2;
for(int i=0;i<n;i++)
{
if(first<=last)
{
if(array[middle]<search)
first=middle+1;
else if(array[middle]==search)
{
status=true;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
}
if(status==true)
{
System.out.println(search+"found at location"+(middle+1));
}
else
System.out.println(search+"is not found in the list");
}
}
b). Write a JAVA program to sort for an element in a given list of elements using bubble sort.
import java.util.Scanner;
public class Bubblesort
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the size of the array:");
int size=s.nextInt();
System.out.println("enter the values into the array:");
int arr[]=new int[size];
for(int i=0;i<size;i++)
{
arr[i]=s.nextInt();
}
sorting(arr);
}
public static void sorting(int arr[])
{
int n=arr.length;
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(arr[j-1]>arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("the sorted arraty is:");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+"\t");
}
}
}
(c). Write a JAVA program to sort for an element in a given list of elements using merge sort.
import java.util.Scanner;
class merge
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter size of the array:");
int size=s.nextInt();
int arr[]=new int[size];
System.out.println("enter elements into the array:");
for(int i=0;i<size;i++)
{
arr[i]=s.nextInt();
}
int start=0;
int end=size-1;
sort(arr,start,end);
System.out.println("the sorted array is:");
for(int j=0;j<size;j++)
{
System.out.print(arr[j]+"\t");
}
}
public static void sort(int arr[],int start,int end)
{
if(start<end)
{
int middle=(start+end)/2;
sort(arr,start,middle);
sort(arr,middle+1,end);
merge(arr,start,middle,end);
}
}
public static void merge(int arr[],int start,int middle,int end)
{
int n1 = middle -start + 1;
int n2 = end - middle;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
{
L[i] = arr[start + i];
}
for (int j=0; j<n2; ++j)
{
R[j] = arr[middle + 1+ j];
}
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
}
(d) Write a JAVA program using StringBufferto delete, remove character.
import java.util.Scanner;
import java.lang.*;
class stringbuffer
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a string you like:");
String str=s.nextLine();
StringBuffer b=new StringBuffer(str);
System.out.println("enter the index you want to delete from:");
int i1=s.nextInt();
System.out.println("to:");
int i2=s.nextInt();
b.delete(i1,i2);
System.out.println("after deletion the new buffer is:"+b);
}
}
No comments:
Post a Comment