arrays
arrays
Same variables
with different
m0 m1 subscripts
m2 m3
• Two types of arrays:
1. Single dimensional/one dimensional array
2. Double /two dimensional array
Single dimensional array
It is an array in which the elements are specified
by a single subscript.
Eg:int m[]=new int[10];
m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7] m[8] m[9]
33 12 32 41 33 55 46 71 80 11
Data items
int m[];
int m[10];
Using array elements
• Three different ways:
1. By using assignment statement
2. By using function argument(BlueJ system)
3. By using input stream
By using assignment statement
for normal variable int m=10;
• For integer type: • For character type:
int m[]={15,7,22,12,34,45,23,31,27,10} char m[]={‘c’, ’o’, ’m’, ’p’, ’u’, ’t’, ’e’, ’r’}
Program: Program:
class sample class sample
{ {
public void main() public void main()
{ int i , sum; sum=0; { int i;
int m[]={2,3,4,5,6}; char m[]={‘c’, ’o’, ’m’, ’p’, ’u’, ’t’, ’e’, ’r’};
for(i=0;i<5;i++) System.out.println(“the characters are:”)
sum=sum + m[i]; for(i=0;i<8;i++)
System.out.println(“the sum =”+sum); System.out.println(m[i]);
} }
} }
An array with empty []
represents an unfixed • For string type:
number of cells in the String m[]={“Amit”, “Sumit”, “Rohit”, “vijay”,
“mohit”}
memory
By using BlueJ system
• For integer type: • For character type:
Program: class sample
class sample {
{ public void main(char m[])
public void main( int m[]) { int i;
{ System.out.println(“the
int i,sum;sum=0; characters are:”)
for( i=0;i<5;i++) for(i=0;i<8;i++)
sum=sum + m[i]; System.out.println(m[i]);
System.out.println(“the sum }
=”+sum); }
} • For string type:
} public void main(String m[])
By using Input Stream
• For integer type: • For character type:
Syntax: Syntax :
int m[]=new int[10]; char m[]=new char [10];
Program: Program:
class sample class sample
{ {
public void main() public void main()
{ { int i,j;
Scanner sc =new Scanner (System.in)); char m[]=new char[10];
int i,sum;sum=0; Scanner sc=new Scanner (System.in);
int m[]=new int[10]; System.out.println(“the characters are:”)
for(i=0;i<10;i++) for(i=0;i<8;i++)
{ m[i]=sc.next().charAt(0);
System.out.println(“Enter your marks in the cell:”); }
m[i]=sc.nextInt(); for(j=0;j<8;j++)
} System.out.println(m[j]);
for(i=0;i<10;i++) }
sum=sum+m[i]; • For string type:
System.out.println(“the sum =”+sum); Syntax:
} String m[]=new String[10];
} Program:
m[i]=sc.next();
1. WAP to accept 20 different numbers in SDA.Display the
sum of all the numbers which are divisible by either 3 or 5.
import java.util.*;
class sum for(i=0;i<20;i++)
{ {
public void main() if(m[i]%3==0 || m[i]%5 == 0)
{ sum= sum +m[i];
Scanner sc= new scanner (System.in); }
int i,sum=0; System.out.println(“the sum
int m[]=new int[20]; =”+sum);
for(i=0;i<20;i++) }
{ }
System.out.println(“Enter the
numbers in the cell:”);
m[i]=sc.nextInt();
}
2.WAP to accept 10 different numbers in SDA. Display the
greatest and the smallest numbers of the array elements.
import java.util.*; max= m[0];min=m[0];
class max_min for(i=0;i<10;i++)
{ {
public void main()
if(m[i]>max)
max=m[i];
{
if(m[i]<min)
Scanner sc= new scanner (System.in);
min=m[i];
int i,min,max;
int m[]=new int[10]; }
for(i=0;i<10;i++) System.out.println(“The greatest of
{ the array elements =” +max);
System.out.println(“Enter the numbers System.out.println(“The smallest of
in the cell:”); the array elements =” +min);
m[i]=sc.nextInt();
}
}
}
Basic operations on Arrays
1. Searching
2. Sorting
3. Insertion
4. Deletion
5. Merging
Search
• It is a process to determine whether a given
item( ie;a number/a character/a word/ a
name etc)is present in the array or not.
• This can be done in two ways:
1. Linear search
2. Binary search
Linear search
• In this technique, the searching starts from
the 0th position of the array and continues one
after another ,where each element is
checked/verified till the end of the array
location.This process is known as Sequential
Search.After searching,a message will be
displayed whether it is completed successfully
or unsuccessfully.
3.WAP to accept n different numbers in SDA.Now,enter a
number and search whether the number is present or not in the
list of array elements by using the Linear search technique.
import java.util.*;
System.out.println(“Enter the number
class linear to be searched:”);
{ ns=sc.nextInt();
public void main()
{ for(i=0;i<n ;i++)
Scanner sc= new Scanner (System.in); {
int i, k=0,ns,n;
if(m[i]==ns
k=1;
System.out.println(“Enter the size of the
break;
array”);
}
n=sc.nextInt(); if(k==1)
int m[]=new int[n]; System.out.println(“The number is
for(i=0;i< n; i++) present”);
{ else
System.out.println(“The number is not
System.out.println(“Enter the numbers in the present”);
cell:”);
m[i]=sc.nextInt(); }
} }
3.WAP to accept n different names in SDA.Now,enter a name and
search whether the name is present or not in the list of array elements
by using the Linear search technique .
import java.util.*; System.out.println(“Enter the name
class linear to be searched:”);
{ ns=sc. next();
public void main()
{
for(i=0;i<n ;i++)
{
Scanner sc= new Scanner (System.in);
if(m[i].equalsIgnoreCase(ns))
int i, k=0,n;
k=1;
String ns; break;
System.out.println(“Enter the size of the }
array”);
if(k==1)
n=sc.nextInt(); System.out.println(“The name is
int m[]=new int[n]; present”);
for(i=0;i< n; i++) else
{ System.out.println(“The name is not
System.out.println(“Enter the names in the
present”);
cell:”);
m[i]=sc. next();
}
}
Binary search
• M[]={17,31,49,55,64,78,87}
• To find the element 31:
Step I: First=0,Last=6,Mid=(First+Last)/2=(0+6)/2=3
Step II:num[Mid]=num[3]=55
55>31,so take first half of array.
Step III: Last= Mid-1 = 3-1 = 2
Mid=(First+Last)/2 = (0+2)/2 = 1
num[Mid]=num[1]= 31
Step IV: 31=31.Hence,Search is Successful
Diff. b/w Linear Search and Binary Search
• Step 2:
15 97 49 56 89 27 44 77
0th 1st 2nd 3rd 4th 5th 6th 7th
• Step 3:
15 27 49 56 89 97 44 77
0th 1st 2nd 3rd 4th 5th 6th 7th
• Step 4:
15 27 44 56 89 97 49 77
0th 1st 2nd 3rd 4th 5th 6th 7th
• ……………………contd
Bubble sort
• In this technique ,the array is sequentially
sacnned several times and during each
iteration the pairs of consecutive elements are
compared and interchanged.If we want to sort
8 numbers in the cell,then the total number of
iterations=7*6*5*4*……..*1. At first pass,the
highest element is pushed in the last
position.(N-1 iterations)
9 6 24 3 11 45 2 15
0th 1st 2nd 3rd 4th 5th 6th 7th
6 9 24 3 11 45 2 15
0th 1st 2nd 3rd 4th 5th 6th 7th
6 9 24 3 11 45 2 15
0th 1st 2nd 3rd 4th 5th 6th 7th
6 9 3 24 11 45 2 15
0th 1st 2nd 3rd 4th 5th 6th 7th
6 9 3 11 24 2 15 45
0th 1st 2nd 3rd 4th 5th 6th 7th