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

3 Final Arrays in Java 2024 25

Uploaded by

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

3 Final Arrays in Java 2024 25

Uploaded by

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

Arrays in Java

▪ Arrays are used to store multiple values in a


single variable, instead of declaring separate
variables for each value.
▪ To declare an array, define the variable type
with square brackets:
▪ String cars[] = {"Volvo", "BMW", "Ford",
"Mazda"};
▪ int myNum[] = {10, 20, 30, 40};
Access the Elements of an Array

▪ String cars[] = {"Volvo", "BMW", "Ford",


"Mazda"};
System.out.println(cars[0]);
Changing the array element
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo
Declaration

▪ <Data type><variable name>[]=new


<Data type><number of elements>
▪ Ex:
▪ int m[]=new int[10];
▪ double m[]=new double[10];
▪ char m[]=new char[10];
▪ String m[]=new String[10];
Using array elements

▪ int m[]={1,5,6,8,78,89};
▪ Or
▪ int m[]=new int[10];
▪ int i;
▪ Scanner sc=new Scanner(System.in);
▪ System.out.println(“enter array
▪ for(i=0;i<10;i++)
▪ {
▪ m[i]=sc.nextInt();
▪ }
length vs length():

▪ length: length is an attribute i.e. a data


member of array.
▪ It gives the length of an array i.e. the number
of elements stored in an array.
▪ length() is a member method of String It
gives the number of characters present in a
string.
Subscript vs Subscripted
variable
▪ Subscript is the index of the element in the
array whereas Subscripted variable is the
name of the array when it is used with a
subscript to access a single element of the
array.
▪ arr[1]-1 is subscript,arr-subscriptedvariable
Guess the Output

▪ int a[] ={2,4,6,8,10};


▪ a[0]=23;
▪ a[3]=a[1];
▪ int c= a[0]+a[1]; (23+4=27)
▪ System.out.println("Sum = "+c);
▪ O/p??
▪ A Single Dimensional array contains N
elements. What will be the last subscript?
Guess the Output

▪ int a[ ]={2,4,6,8};
▪ for(i=0;i<=1;i++)
▪ {
▪ s=a[i]+a[3-i];
▪ System.out.println(s);
▪ }
▪ o/p??
Types: SDA,DDA

▪ String[] cars = {"Volvo", "BMW", "Ford",


"Mazda"};
▪ System.out.println(cars.length);
▪ // Outputs 4
▪ DDA
▪ int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
▪ int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
Linear search vs Binary
search
▪ A linear search scans one item at a time, without
jumping to any item .
▪ The worst case complexity is O(n), sometimes
known an O(n) search
▪ Time taken to search elements keep increasing as
the number of elements are increased.
▪ A binary search however, cut down your search to
half as soon as you find middle of a sorted list.
▪ The middle element is looked to check if it is greater
than or less than the value to be searched.
▪ Accordingly, search is done to either half of the given
list
Linear Search

▪ Linear Search to find the element “J” in a


given sorted list from A-X
Binary Search
Binary search-Ascending
order
Binary Descending order
Binary Search-Ascending order-int
import java.util.Scanner;
public class binarysearch
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int len=a.length;
int u=len-1,m,l=0;
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
int s=sc.nextInt();
int f=-1;
//Ascending
while(l<=u)
{
m=(l+u)/2;
if(s==a[m])
{
f=m;
break;
}
else if(s>a[m])
{
l=m+1;
Linear vs Binary
Important Differences
▪ Input data needs to be sorted in Binary Search and not in
Linear Search
▪ Linear search does the sequential access whereas Binary
search access data randomly.
▪ Time complexity of linear search -O(n) , Binary search has
time complexity O(log n).
▪ log(1) = 0 because 2 to the power of 0 is equal to 1: log(1) =
0 , 2⁰ = 1, other examples:
▪ The log of 8 would be: log(8) = 3, 2³ = 8
▪ The log of 16 would be: log(16) = 4, 2⁴ = 16
▪ so to find the logarithm or the binary logarithm We have
to ask ourselves, 2 to the power of what is equal to that
number? And if we solve this then we find log n.
Sorting vs Searching:
▪ Sorting means to arrange the elements of the array in ascending
or descending order. Bubble sort and Selection sort are examples
of sorting techniques.
▪ Searching means to search for a term or value in an array. Linear
search and Binary search are examples of search techniques
▪ Linear search and Binary search
▪ Linear Search :Linear search works on sorted and unsorted arrays
. Each element of the array is checked against the target value
until the element is found or end of the array is reached.
▪ Binary search works on only sorted arrays (ascending or
descending). Array is successively divided into 2 halves and the
target element is searched either in the first half or in the second
half.
▪ Linear Search is slower, Binary Search is faster
Assignment

▪ for (int i = 0; i < a.length; i++)


▪ {
▪ a[i] = in.nextDouble();
▪ b[i] = (int)a[i];
▪ }
Sorting-Bubble Sort

▪ Bubble Sort- compare adjacent elements of


the array
▪ Bubble sort-Ascending, Descending on
int,float,double, char(==,>,< can be used to
compare )
▪ Bubble sort-Ascending, Descending on
Strings(equals() and compareTo()) functions
should be used to compare Strings.
Bubble Sort
import java.util.Scanner;
public class bubblesort //descending
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String a[]=new String[10];
int len=a.length;
String temp;
for(int i=0;i<len;i++)
{
a[i]=sc.next();
}
for(int i=0;i<len-1;i++)
{
for(int j=0;j<len-1-i;j++)
{
if(a[j].compareTo(a[j+1])<0) //swap condition for Descending order of strings
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
Sorting-Selection Sort

▪ In Ascending order-Find the index of the


smallest element in the inner loop and
Swap the smallest element with zero th
indexed element,
In the second pass, find the index of second
smallest element and swap second smallest
with 1 st indexed element , so on----
Selection Sort
▪ import java.util.Scanner;
▪ public class selectionsort //ascending
▪ {
▪ public static void main(String args[])
▪ {
▪ Scanner sc=new Scanner(System.in);
▪ String a[]=new String[10];
▪ int len=a.length,min;
▪ String temp;
▪ for(int i=0;i<len;i++)
▪ {
▪ a[i]=sc.next();
▪ }
▪ for(int i=0;i<len-1;i++)
▪ {
▪ min=i;
▪ for(int j=i+1;j<len;j++)
▪ {
▪ if(a[j].compareTo(a[min])<0)
▪ min=j;
▪ }
▪ temp=a[i];
▪ a[i]=a[min];
▪ a[min]=temp;
▪ }
▪ for(int i=0;i<len;i++)
Double dimensional arrays

▪ Declaration and Initialisation of DDA with all


data types of elements
▪ Accepting input in the nested for
▪ Outer loop runs on rows and inner loop on
columns
Sum of even numbers and sum of odd numbers in DDA
▪ import java.util.Scanner;
▪ public class arraydda1
▪ {
▪ public static void main(String args[]) {
▪ Scanner in = new Scanner(System.in);
▪ int N[][] = new int[4][4];
▪ long evenSum = 0, oddProd = 1;
▪ System.out.println("Enter the elements of 4x4 DDA: ");
▪ for (int i = 0; i < 4; i++) {
▪ for (int j = 0; j < 4; j++) {
▪ N[i][j] = in.nextInt();
▪ if (N[i][j] % 2 == 0)
▪ evenSum += N[i][j];
▪ else
▪ oddProd *= N[i][j];
▪ }
▪ }

▪ System.out.println("Sum of all even numbers = " + evenSum);
▪ System.out.println("Product of all odd numbers = " + oddProd);
▪ }
▪ }
DDA-Important points

▪ Declare,initialisation,accepting input and


displaying DDA
▪ Accessing elsements of double dimensional
arrays
▪ Sum of elements of each row, sum elements
of each column,sum of right diagonal
elements and sum of left diagonal elements
▪ Symmetric matrices-(a[i][j]==a[j][i]- only
square matrices)
▪ Sum and difference of two DDA

You might also like