practixdf
practixdf
Output
If ‘c’ is passed in the argument for method check
Result: c is a Letter
9
Write a program to initialize a single dimensional array x[] of 10 integer elements. Arrange the array in ascending
order using selection sort method. Print the array before and after sorting. Each array should be printed in single
line with a suitable space between each element.
import java.io.*;
public class selectionsort
{ public void sorting()
{
int x[]={22,8,4,11,5,78,2,7,15,44};
int i,j,index,small,temp;
System.out.println("Unsorted array or array before sorting");
for(i=0;i<10;i++)
{System.out.print(x[i]+"\t");
}
for(i=0;i<10-1;i++)
{
small=x[i];
index=i;
for(j=i+1;j<10;j++)
{
if(x[j]<small)
{
small=x[j];
index=j;
}
}
temp=x[i];
x[i]=x[index];
x[index]=temp;
}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(x[i]+"\t");
}
}}
output
Unsorted array or array before sorting
22 8 4 11 5 78 2 7 15 44
sorted array or array after sorting
2 4 5 7 8 11 15 22 44 78
10 Write a program to initialize a single dimensional array x[] of 10 integer elements. Arrange the array in ascending
order using bubble sort method. Print the array before and after sorting. Each array should be printed in a single
line with suitable space between each element.
import java.io.*;
public class bubblesort
{
public void sorting()
{
int x[]={22,8,4,11,5,78,2,7,15,44};
int i,j,temp;
System.out.println("Unsorted array or array before sorting");
for(i=0;i<10;i++)
{System.out.print(x[i]+"\t");
}
for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(x[i]+"\t");
}
}
}
output
Unsorted array or array before sorting
22 8 4 11 5 78 2 7 15 44
sorted array or array after sorting
2 4 5 7 8 11 15 22 44 78
import java.io.*;
public class Bin_Search
{
public void BinarySearch(int val)
{
int num[]={22,12,10,8,4,11,5,78,2,3,7,15,44,52,25};
int j,i,pos=0,temp;
int found=0;
for(i=0;i<15-1;i++)
{
for(j=0;j<15-i-1;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
int s=0,e=14,mid=0;
while(s<=e)
{
mid=(s+e)/2;
if(val==num[mid])
{
found=1;
pos=mid;
break;}
else if(val>num[mid])
s=mid+1;
else if(val<num[mid])
e=mid-1;
}
System.out.println("\n sorted array or array after sorting");
for(i=0;i<10;i++)
{
System.out.print(num[i]+"\t");
}
System.out.println();
if(found==1)
System.out.println(val+" is found at"+pos+"index/position");
else
System.out.println(val+"not found in the list");
}
}
output:
Solution:
import java.io.*;
public class problem10
{
public void check()
{
char x[]={'m','n','o','p'};
char y[]={'m','n','o','p'};
int size1=x.length;
int size2=y.length;
boolean result=true;
System.out.println("elements of array x=");
for(int i=0;i<size1;i++)
{
System.out.print(x[i]+"\t");
}
System.out.println("\nelements of array y=");
for(int i=0;i<size2;i++)
{
System.out.print(y[i]+"\t");
}
if(size1!=size2)
System.out.println("The array are not identical, because size of both array are not same");
else
for(int i=0;i<size1;i++)
{
if(x[i]!=y[i])
{
result=false;
break;
}
}
if(result==true)
System.out.println("\n Both the array are identical");
else
System.out.println("The given array are not identical");
}
}
output:
elements of array x=
m n o p
elements of array y=
m n o p
Both the array are identical
Variable description table
Variable name Datatype Purpose
x[] char Character array having
characters
y[] char Character array having
characters
size1 int To find number of elements
ofx[]
size2 int To find number of elements
of y[]
result Boolean To store value true or false
i int Loop variable