0% found this document useful (0 votes)
2 views

Array-1D

The document contains multiple Java classes that demonstrate various programming concepts such as finding minimum and maximum values in an array, merging arrays, sorting, searching, and calculating ticket prices with discounts. Each class includes a main method that executes specific tasks, such as inputting data, processing it, and displaying results. The examples cover basic array manipulation, linear and binary search algorithms, and sorting techniques like selection and bubble sort.

Uploaded by

dakshbhardwajjjj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array-1D

The document contains multiple Java classes that demonstrate various programming concepts such as finding minimum and maximum values in an array, merging arrays, sorting, searching, and calculating ticket prices with discounts. Each class includes a main method that executes specific tasks, such as inputting data, processing it, and displaying results. The examples cover basic array manipulation, linear and binary search algorithms, and sorting techniques like selection and bubble sort.

Uploaded by

dakshbhardwajjjj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

//Write a program to initalize the given data in an array & find the min.

&max. value along with the sum


//of the given elements.
//Numbers: 2 5 4 1 3
//Output: Min value = 1, Max value = 5, Sum of the elements = 15

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;

InputStreamReader I = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(I);

System.out.println("Enter the elements in p:");


for(int i=0;i<6;i++)
{
p[i]=Integer.parseInt(br.readLine());
}

System.out.println("Enter the elements in q:");


for(int i=0;i<4;i++)
{
q[i]=Integer.parseInt(br.readLine());
}

for(int i=0;i<6;i++)
{
r[i]=p[i];
c++;
}

for (int i=0; i<4; i++)


{
r[c+i]=q[i];
}

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="";

InputStreamReader I = new InputStreamReader(System.in);


BufferedReader br =new BufferedReader(I);

System.out.println("Enter the names & marks:");


for(int i=0; i<5; i++)
{
name[i]=br.readLine();
marks[i]=Integer.parseInt(br.readLine());
}

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;

for(int i=0; i<l; i++)


{
if(arr[i]%2==0)
{
even[e++]=arr[i];
}
else
{
odd[o++]=arr[i];
}
}

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;

InputStreamReader I=new InputStreamReader(System.in);


BufferedReader br= new BufferedReader(I);
System.out.println("Enter the name first & then ticket");
for(int i=0; i<5;i++)
{
n[i]=br.readLine();
t[i]=Integer.parseInt(br.readLine());
}

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;

InputStreamReader I = new InputStreamReader(System.in);


BufferedReader br =new BufferedReader(I);

System.out.println("Enter the array");


for(int i=0; i<5; i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("Enter the value which you want to search");
x=Integer.parseInt(br.readLine());

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("Unsorted Array"); //unsorted array


for(int i=0; i<l; i++)
{
System.out.print(number[i]+" ");
}

for(int i=0; i<l; i++) //sorting technique


{
for(int j=i+1; j<l; j++)
{
if(number[i]>number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}

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;

InputStreamReader I=new InputStreamReader(System.in);


BufferedReader br= new BufferedReader(I);

System.out.println("Enter Elements in Array");


for(int i=0; i<5; i++)
{
n[i]=Integer.parseInt(br.readLine());
}

System.out.println("Before Sorted Order");


for(int i=0;i<5;i++)
{
System.out.print(n[i] + " ");
}

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;

InputStreamReader I=new InputStreamReader(System.in);


BufferedReader br= new BufferedReader(I);
System.out.println("Enter number to search:");
x=Integer.parseInt(br.readLine());

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;

InputStreamReader in=new InputStreamReader(System.in);


BufferedReader br=new BufferedReader(in);

System.out.println("Enter 5 Names in array:");


for(int i=0;i<5;i++)
{
name[i] = br.readLine();
}

System.out.println("Enter name to Search:");


s = br.readLine();

// 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]+" ");
}

for(int i=0; i<l; i++)


{
x=a[i];
for(int j=0; j<l; j++)
{
if(x==a[j])
{
c=c+1;
}
}
b[i]=c;
c=0;
}

System.out.println();
System.out.println("Frequency Array");
for(int i=0; i<l; i++)
{
System.out.print(b[i]+" ");
}

for(int i=0; i<l; i++)


{
if(b[i]>max)
{
max=b[i];
pos=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));

}
}

You might also like