Array-1D
Array-1D
class Q1
{
public static void main(String args[])
{
int N[]= {2,5,4,1,3};
int max=N[0];
int min=N[0];
int s=0;
for(int i=0;i<5;i++)
{
s=s+N[i];
if(max<N[i])
{
max=N[i];
}
if(min>N[i])
{
min=N[i];
}
}
System.out.println("MAX:" +max);
System.out.println("MIN:" +min);
System.out.println("SUM:"+ s);
}
}
import java.io.*;
class Q2
{
public static void main(String args[])throws IOException
{
int p[] = new int [6];
int q[] = new int [4];
int r[] = new int [10];
int c=0;
for(int i=0;i<6;i++)
{
r[i]=p[i];
c++;
}
System.out.println("Merged Array");
for(int i=0; i<10; i++)
{
System.out.println(r[i]);
}
}
}
import java.io.*;
class Q3
{
public static void main(String args[])throws IOException
{
String name[]=new String[5];
int marks[]=new int[5];
int max;
String nm="";
max=marks[0]; //assumed
for (int i=0;i<5;i++)
{
if(max<marks[i])
{
max=marks[i];
nm=name[i];
}
}
System.out.println("Name:" +nm);
System.out.println("Marks:" +max);
}
}
//WAP to input an array arr=[1,2,3,4,5,6,7,8,9,10]
//find the even numbers/elements from the array and insert them in another
array called even=[]
//rest of the numbers/elements should inserted on another array called
odd=[]
class Q4
{
public static void main(String args[])
{
int arr[]={1,2,3,4,5,6,7,8,9,10};
int even[]= new int[5];
int odd[]= new int [5];
int e=0;
int o=0;
int l = arr.length;
System.out.println("EVEN");
for(int i=0; i<5;i++)
{
System.out.println(even[i]);
}
System.out.println("ODD");
for(int i=0; i<5;i++)
{
System.out.println(odd[i]);
}
}
import java.io.*;
class Q5
{
public static void main(String args[])throws IOException
{
String n[]= new String[5];
int t[]= new int[5];
double d=0.0;
double net=0.0;
System.out.println("S.No"+"\t"+"Name"+"\t"+"ticket"+"\t"+"Net");
for(int i=0;i<5; i++)
{
if(t[i]>70000)
{
d=t[i]*0.18;
}
else if(t[i]>55001 && t[i]<70000)
{
d=0.16*t[i];
}
else if(t[i]>35001 && t[i]<55000)
{
d=0.12*t[i];
}
else if(t[i]>25001 && t[i]<35000)
{
d=0.10*t[i];
}
else
{
d=0.02*t[i];
}
net=t[i]-d;
System.out.println((i+1)+"\t"+n[i]+"\t"+t[i]+"\t"+net);
}
}
}
//linear search
import java.io.*;
class Q6
{
public static void main(String args[])throws IOException
{
int arr[]=new int[5];
int x;
int pos=0;
for(int i=0;i<5;i++)
{
if(arr[i]==x)
{
pos=i;
System.out.println("Element:" +arr[i]);
System.out.println("POS:" +(pos+1));
break;
}
}
}
}
//selection sort: Ascending
class Q7
{
public static void main(String[] args)
{
int number[]={10, 9, 4, 13, 8, 15, 7, 1, 6, 2};
int l=number.length;
int temp=0;
System.out.println();
System.out.println("Sorted Array"); //print of sorted array
for(int i=0; i<l; i++)
{
System.out.print(number[i]+" ");
}
}
}
//bubble sort: Ascending
import java.io.*;
class Q8
{
public static void main(String args[])throws IOException
{
int n[]=new int[5];
int t;
System.out.println();
for(int i=0;i<4;i++)
{
for(int j=0;j<4-i;j++)
{
if(n[j]>n[j+1])
{
t=n[j];
n[j]=n[j+1];
n[j+1]=t;
}
}
}
System.out.println("Sorted Order");
for(int i=0;i<5;i++)
{
System.out.print(n[i] + " ");
}
}
}
//binary search
import java.io.*;
public class Q10
{
public static void main(String arg[])throws IOException
{
int n[]={1,4,6,7,8,9,10,11,14,15,16};
int low, high, mid, p=0, l, x, f=0;
l=n.length;
low=0;
high=l-1;
while(low<=high)
{
mid=(low+high)/2;
if(x<n[mid])
{
high=mid-1;
}
else if(x>n[mid])
{
low=mid+1;
}
else if(x==n[mid])
{
f=1;
p=mid+1;
break;
}
}
if(f==1)
{
System.out.println(x+" is located at "+p);
}
else
{
System.out.println(x+" is not found");
}
}
}
//string sort & search
import java.io.*;
public class Q11
{
public static void main(String args[]) throws IOException
{
String name[] = new String[5];
String s, t;
int low, high, mid, pos;
// Sorting of array
for(int i=0;i<4;i++)
{
for(int j=0;j<4-i;j++)
{
if(name[j].compareTo(name[j+1])>0)
{
t = name[j];
name[j] = name[j+1];
name[j+1] = t;
}
}
}
System.out.println("Sorted array:");
for(int i=0;i<5;i++)
{
System.out.print(name[i] + " ");
}
// Binary Search
high=5;
low=0;
pos=0;
while(low <= high)
{
mid = (low + high) / 2;
if(s.compareTo(name[mid])<0)
{
high = mid - 1;
}
else if(s.compareTo(name[mid])>0)
{
low = mid + 1;
}
else if(s.compareTo(name[mid])==0)
{
pos = mid + 1;
break;
}
}
System.out.println("\n Search result");
System.out.println(s+" is located at " + pos);
}
}
class Q12
{
public static void main(String args[])
{
int a[]={1, 2, 3, 4, 2, 5, 3, 1, 6, 3, 7, 3, 9};
int l=a.length;
int temp=0;
int x;
int c=0;
int b[]=new int[l];
int max=0;
int pos=0;
System.out.println("Unsorted Array");
for(int i=0; i<l; i++)
{
System.out.print(a[i]+" ");
}
//sorting technique
for(int i=0; i<l; i++)
{
for(int j=i+1; j<l; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println();
System.out.println("Sorted Array");
for(int i=0; i<l; i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Frequency Array");
for(int i=0; i<l; i++)
{
System.out.print(b[i]+" ");
}
System.out.println();
System.out.println("Mode of the Array");
System.out.println(a[pos]+" is occured max time in the array at position
"+ (pos+1));
}
}