0% found this document useful (0 votes)
16 views24 pages

Java 1D Array Operations Assignment

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
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)
16 views24 pages

Java 1D Array Operations Assignment

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
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

//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];
}
}
[Link]("MAX:" +max);
[Link]("MIN:" +min);
[Link]("SUM:"+ s);
}
}
import [Link].*;
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([Link]);


BufferedReader br = new BufferedReader(I);

[Link]("Enter the elements in p:");


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

[Link]("Enter the elements in q:");


for(int i=0;i<4;i++)
{
q[i]=[Link]([Link]());
}

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

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


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

[Link]("Merged Array");
for(int i=0; i<10; i++)
{
[Link](r[i]);
}

}
}
import [Link].*;
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([Link]);


BufferedReader br =new BufferedReader(I);

[Link]("Enter the names & marks:");


for(int i=0; i<5; i++)
{
name[i]=[Link]();
marks[i]=[Link]([Link]());
}

max=marks[0]; //assumed
for (int i=0;i<5;i++)
{
if(max<marks[i])
{
max=marks[i];
nm=name[i];
}
}
[Link]("Name:" +nm);
[Link]("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 = [Link];

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


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

[Link]("EVEN");
for(int i=0; i<5;i++)
{
[Link](even[i]);
}
[Link]("ODD");
for(int i=0; i<5;i++)
{
[Link](odd[i]);
}

}
import [Link].*;
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([Link]);


BufferedReader br= new BufferedReader(I);
[Link]("Enter the name first & then ticket");
for(int i=0; i<5;i++)
{
n[i]=[Link]();
t[i]=[Link]([Link]());
}

[Link]("[Link]"+"\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;
[Link]((i+1)+"\t"+n[i]+"\t"+t[i]+"\t"+net);
}
}
}
//linear search
import [Link].*;
class Q6
{
public static void main(String args[])throws IOException
{
int arr[]=new int[5];
int x;
int pos=0;

InputStreamReader I = new InputStreamReader([Link]);


BufferedReader br =new BufferedReader(I);

[Link]("Enter the array");


for(int i=0; i<5; i++)
{
arr[i]=[Link]([Link]());
}
[Link]("Enter the value which you want to search");
x=[Link]([Link]());

for(int i=0;i<5;i++)
{
if(arr[i]==x)
{
pos=i;
[Link]("Element:" +arr[i]);
[Link]("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=[Link];
int temp=0;

[Link]("Unsorted Array"); //unsorted array


for(int i=0; i<l; i++)
{
[Link](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;
}
}
}

[Link]();
[Link]("Sorted Array"); //print of sorted array
for(int i=0; i<l; i++)
{
[Link](number[i]+" ");
}
}
}
//bubble sort: Ascending

import [Link].*;
class Q8
{
public static void main(String args[])throws IOException
{
int n[]=new int[5];
int t;

InputStreamReader I=new InputStreamReader([Link]);


BufferedReader br= new BufferedReader(I);

[Link]("Enter Elements in Array");


for(int i=0; i<5; i++)
{
n[i]=[Link]([Link]());
}

[Link]("Before Sorted Order");


for(int i=0;i<5;i++)
{
[Link](n[i] + " ");
}

[Link]();
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;
}
}
}

[Link]("Sorted Order");
for(int i=0;i<5;i++)
{
[Link](n[i] + " ");
}
}
}
//binary search

import [Link].*;
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=[Link];

low=0;
high=l-1;

InputStreamReader I=new InputStreamReader([Link]);


BufferedReader br= new BufferedReader(I);
[Link]("Enter number to search:");
x=[Link]([Link]());

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)
{
[Link](x+" is located at "+p);
}
else
{
[Link](x+" is not found");
}
}

}
//string sort & search

import [Link].*;
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([Link]);


BufferedReader br=new BufferedReader(in);

[Link]("Enter 5 Names in array:");


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

[Link]("Enter name to Search:");


s = [Link]();

// 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;
}
}
}

[Link]("Sorted array:");
for(int i=0;i<5;i++)
{
[Link](name[i] + " ");
}

// Binary Search
high=5;
low=0;
pos=0;
while(low <= high)
{
mid = (low + high) / 2;
if([Link](name[mid])<0)
{
high = mid - 1;
}
else if([Link](name[mid])>0)
{
low = mid + 1;
}
else if([Link](name[mid])==0)
{
pos = mid + 1;
break;
}
}
[Link]("\n Search result");
[Link](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=[Link];
int temp=0;

int x;
int c=0;
int b[]=new int[l];
int max=0;
int pos=0;

[Link]("Unsorted Array");
for(int i=0; i<l; i++)
{
[Link](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;
}
}
}

[Link]();
[Link]("Sorted Array");
for(int i=0; i<l; i++)
{
[Link](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;
}

[Link]();
[Link]("Frequency Array");
for(int i=0; i<l; i++)
{
[Link](b[i]+" ");
}

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


{
if(b[i]>max)
{
max=b[i];
pos=i;
}
}

[Link]();
[Link]("Mode of the Array");
[Link](a[pos]+" is occured max time in the array at position
"+ (pos+1));

}
}

Common questions

Powered by AI

Q2 merges two arrays by first copying all elements of the first array into a new array, 'r', then appending all elements of the second array. This is achieved using a loop that iterates through each element of the first array, storing each in 'r', followed by a similar loop for the second array, using an incrementing counter to track the insertion index in 'r'. This technique ensures all elements are sequentially added to the new array .

The purpose of calculating element frequency and determining the mode, as shown in the code snippet, is to identify which element appears most frequently in the dataset. This analysis provides insights into data distribution, highlighting dominant values, which can be crucial for understanding underlying patterns and making data-driven decisions. Such frequency analysis helps in statistical summarization of datasets by identifying the most recurrent element, thereby identifying potential biases or anomalies .

In Q4, the method to distinguish between even and odd numbers involves iterating through each element of the input array and applying modulus operation by 2. If the result is zero, the number is even and added to the 'even' array; otherwise, it is odd and added to the 'odd' array. Separate indices are used for inserting into the 'even' and 'odd' arrays to ensure proper sequential storage of categorized elements .

The approach used in Q1 to determine the minimum and maximum values of an array involves initializing two variables, each to the first element of the array. As the program traverses the array, each element is compared with the current minimum and maximum. If an element is smaller than the current minimum, it replaces the minimum; similarly, if it is larger, it replaces the maximum. This one-pass traversal efficiently checks each element against the current min/max values and updates accordingly .

Q6 performs a linear search by iterating through the array elements sequentially and comparing each with the target value. If a match is found, its position is reported, ending the search. The time complexity is O(n), as each element may need to be checked before finding the target or confirming its absence. Linear search is effective for small or unsorted arrays, where setup time for more complex searching algorithms isn't justified. It's straightforward but not suitable for large datasets due to its inefficiency .

String sorting in Q11 using Bubble Sort (O(n^2)) is inefficient for large datasets, primarily due to its non-linear scalability where each comparison involves multiple character evaluations. Once sorted, binary searching becomes efficient (O(log n)), capable of quickly locating strings. Therefore, the combination is initially inefficient for large datasets due to the sorting stage, but the efficiency improves significantly for lookups post-sorting, suggesting this approach is justified when multiple searches are expected over the sorted list .

In Q5, tax is calculated based on predefined brackets relative to ticket prices. Different rates (0.02, 0.10, 0.12, 0.16, 0.18) are applied depending on the price range, systematically reducing the ticket price by the corresponding tax amount. This tiered approach serves to compute the net ticket price after tax deduction, simulating real-world scenarios where taxes are applied differently according to price thresholds. It demonstrates how financial calculations can vary based on input parameters .

Q7 implements the Selection Sort algorithm, which is characterized by its O(n^2) computational complexity. This sorting technique involves iterating over the array and repeatedly identifying the smallest remaining element to place it in its correct position. Despite its simplicity, Selection Sort is not efficient for large datasets due to its quadratic time complexity. It is, however, noted for its easy-to-understand mechanism of repeatedly finding the minimum and swapping it to sort the array .

Q8 uses the Bubble Sort algorithm, which repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until no swaps are needed, indicating the array is sorted. The main difference from Q7's Selection Sort is that Bubble Sort sorts incrementally with each pass potentially bubbling the largest unsorted element to its correct position. Although both have O(n^2) complexity, Bubble Sort can be optimized to stop early if no swaps are made in a pass, providing potential advantages in partially sorted datasets compared to Selection Sort .

Binary search, as implemented in Q10, is significantly more efficient than linear search, featuring a time complexity of O(log n) due to its divide-and-conquer approach. It repeatedly divides the sorted array in half, discarding one half depending on the comparison result with the midpoint. This rapid reduction in search space makes it preferred over linear search for larger, sorted datasets. Binary search requires a sorted array to enable the midpoint comparison to definitively exclude one half of the array from further consideration, which is fundamental to its efficiency .

You might also like