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

3 - Arrays

Arrays allow storing multiple values in a single variable. An array declaration specifies the data type within square brackets. Elements can be accessed via an index and changed. Arrays have a length property. Linear search scans elements sequentially while binary search divides the array in half on each step.

Uploaded by

annwesa.panda
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)
86 views24 pages

3 - Arrays

Arrays allow storing multiple values in a single variable. An array declaration specifies the data type within square brackets. Elements can be accessed via an index and changed. Arrays have a length property. Linear search scans elements sequentially while binary search divides the array in half on each step.

Uploaded by

annwesa.panda
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
You are on page 1/ 24

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"};
[Link](cars[0]);
Changing the array element
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
cars[0] = "Opel";
[Link](cars[0]);
// Now output is Opel instead of Volvo
▪ <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([Link]);
▪ [Link](“enter array
▪ for(i=0;i<10;i++)
▪ {
▪ m[i]=[Link]();
▪ }
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(start counting from 1).
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[1] -
subscriptedvariable
Guess the Output

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


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

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

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


"Mazda"};
▪ [Link]([Link]);
▪ // Outputs 4
▪ DDA
▪ int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
▪ int x = myNumbers[1][2];
[Link](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
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
▪ for (int i = 0; i < [Link]; i++)
▪ {
▪ a[i] = [Link]();
▪ b[i] = (int)a[i];
▪ }
Selection Sort
Let’s say the unsorted array is : -

Now, for the first position in the sorted


array, the entire array is to be scanned sequentially.
At present, 12 is stored at the first position
, after searching the entire array,
it is found that 8 is the smallest value.
So, swap 12 with 8. After the first iteration, 8 will appear
at the first position in the sorted array.
After the first iteration, 8 will appear at the first position in the sorted array.

For the second position, where 29 is stored presently, we again sequentially scan the rest of the items of
unsorted array. After scanning,

we find that 12 is the second lowest element in the array that should be appeared at second position.

Now, swap 29 with 12. After the second iteration, 12 will appear at the second position in the sorted array.
So, after two iterations, the two smallest values are placed at the beginning in a sorted way.
same process is applied to the rest of the array elements. Now, we are showing a pictorial representation of the entire sorting process.

Now, the array is completely sorted.


Double Dimensional Arrays
Accepting DDA elements
1.//Java Program to illustrate the use of multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
}
[Link]();
}
}}
Sum of two arrays
class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices


int c[][]=new int[2][3];

//adding and printing addition of 2 matrices


for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
[Link](c[i][j]+" ");
}
[Link]();//new line
}

}}

You might also like