Arrays_Theory+Programs
Arrays_Theory+Programs
Arrays_Theory+Programs
Roopesh
int a[ ]={10,20,30,40,50};
Introduction int l=a.length;
• Array is a collection of values of similar datatype stored in
continuous memory location.
• Array is a like any other normal variable. The only difference is that it
can store one or more than one values.
• Arrayname is always followed by [ ] square brackets.
• To work with an array
Array
Declaration Initialization
Declaration of an array
• Syntax:
datatype arrayname[ ] = new datatype[ number of elements/values ];
e.g.:
int a[] = new int[5];
index no. 0 1 2 3 4
a
position no. 1 2 3 4 5
Each block is known as element of any array. Each element can store only 1 value.
Index number = Position number – 1 OR Position number = Index number + 1
Access an array element
• Syntax:
arrayname[index number];
e.g.: a[3];
• Once array element is accessed, any form of operation or work can be done
in that element like storing a value, arithmetic operations on the value of an
element, printing the value of an element etc…
• Index number of an array is known as subscript of an array
Initialise an array element
• Syntax:
arrayname[index number] = value;
• formula:
Total Memory(byte) = Memory byte of 1 value for given datatype x number
of elements
a a[]
It can store only 1 value It can store 1 or more than 1
value of similar data type
It is a normal variable It is an array variable
Primitive datatype Non Primitive datatype
Difference between length() and length
length() length
It is a function of string library It is an property of an array
class/It is a String function
It returns number of character It returns number of elements
present in the string of an array.
import java.util.*; WAP to input 10 integer values in an array and
class array1
{
print them in tab form
public static void main()
{
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
int length = a.length; int I;
System.out.println("Enter any 10 integers values");
for( i = 0; i<a.length; i++)
{
a[i] = sc.nextInt();
}
System.out.println("\n\n\n****** Output *****");
for( i = 0; j<len; j++)
{
System.out.print(a[j] + "\t");
}
}
}
import java.util.*;
class array2
{
public static void main()
{
WAP to input 10 integer values in an array. Calculate and
Scanner sc = new Scanner(System.in);
int a[] = new int[10];
print the sum and average of the values
int len = a.length, s=0;double avg;
System.out.println("Enter any 10 numbers");
for(int i = 0; i<len; i++)
{
a[i] = sc.nextInt();
}
for(int i = 0; i<len; i++)
{
s=s+ a[i];
}
avg = s/10.0;
System.out.println("\n\n\n****** Output *****");
System.out.println("Sum of the numbers: "+s);
System.out.println("Average of the numbers: "+avg);
}
}
WAP to input 10 character values in an array. Count and
print number of vowels present in an array
import java.util.*; System.out.println("\n\n\n****** Output *****");
class array3
{ for(int j = 0; j<len; j++)
public static void main() {
{ System.out.print(a[j] + "\t");
Scanner sc = new Scanner(System.in); } // end of for block
char a[] = new char[10];
int len = a.length, c=0; System.out.println("\nCount of Vowels: "+c);
System.out.println("Enter any 10 Alphabets"); } // end of main()
for(int i = 0; i<len; i++) } // end of class
{
a[i] = sc.nextLine().charAt(0);
a[i] = Character.toUpperCase(a[i]);
if( a[i] =='A' || a[i] =='E' || a[i] =='I' || a[i] =='O' || a[i]=='U')
{
c = c + 1;
} // end of if block
} // end of for block
import java.util.*;
class array4
WAP to input 10 names in an array. Print the names
{ having odd length
public static void main()
{
Scanner sc = new Scanner(System.in);
String a[] = new String[10];
int len = a.length;
System.out.println("Enter any 10 names");
for(int i = 0; i<len; i++)
{
a[i] = sc.nextLine().trim();
}
System.out.println("\n\n\n****** Output *****");
for(int j = 0; j<len; j++)
{
if(a[j].length()%2==1)
System.out.println(a[j]);
}
}
}
Operations on Arrays
Linear Search
Processing Search
Binary Search
Bubble Sort
Sort
Selection Sort
Search process in an array
• Searching means looking or find a value in an array elements.
• If the value is present then print that the value is present otherwise print
that the value is not present.
• Types of Search process
❖ Linear Search
❖ Binary Search
• In search process, an extra variable is required which will store the
position of the value to be searched. The final output the program will
depend on the value of the such variable. This variable is known as flag
variable.
• Initially flag variable will be zero i.e assume the value to be searched is
not present in an array
Linear Search
WAP to input 10 integer values in an array and a number to find in the
array. Check and print the Last occurrence of the number if it is
present in an array else print not present.
import java.util.*; for(int j = 0; j<len; j++)
class array6 {
{ //searching for number
public static void main() if(a[j]==n)
{ {
Scanner sc = new Scanner(System.in); p = j + 1;// storing the position number
int a[] = new int[10]; }end of if block
int len = a.length; } // end of for block
System.out.println("Enter any 10 numbers");
for(int i = 0; i<len; i++) if(p==0)
{ {
a[i] = sc.nextInt(); System.out.println("Value is not present");
}// end of for block } // end of if block
System.out.println(“Enter the number to be searched"); else
int n = sc.nextInt(); // number to be searched {
int p=0; // flag variable System.out.println("Value is present at position: "+p);
} // end of else block
} // end of main()
} // end of class
Binary Search
Binary Search
a 11 17 23 29 35 43 51 71
0 1 2 3 4 5 6 7
lb mb ub
lb = 0 (initially)
ub = a.length – 1 = 8 – 1 = 7 (initially)
mb = (ub + lb ) / 2 = (7 + 0) / 2 =3
• During Binary search, the value (n) to be searched is compared with an array of Middle
bound. There are three possibilities in such case
• Possibility 1: if n == a[mb] then p = mb + 1 // p is the position number
• Possibility 2: if n < a[mb] then ub = mb – 1
• Possibility 3: if n > a[mb] then lb = mb + 1
Wap to input a number to be searched in an array. Using Binary
search, check and print whether the number is present in an array or
not.
{11, 17, 23 , 65 , 105 ,115, 150, 171} if( n == a[mb])
{
import java.util.*; p = mb + 1; break;
class BinarySearch }
{ else if( n < a[mb] )
{
public static void main() ub = mb – 1;
{ }
Scanner sc = new Scanner(System.in); else
int a[] = {11,17, 23, 65, 105, 115, 150, 171}; {
lb = mb + 1;
int len = a.length; }
System.out.println(“Enter the number to be searched"); } // end of while block
int n = sc.nextInt(); // number to be searched
int p = 0; // flag variable if(p==0)
{
int lb = 0, ub = len – 1, mb; System.out.println("Value is not present");
} // end of if block
while(lb <= ub) else
{ {
System.out.println("Value is present at position: "+p);
mb = (ub + lb) / 2; } // end of else block
} // end of main()
} // end of class
Sort process in an array
• Sorting means arranging values in ascending or descending order in
an array.
• Types of Search process
❖ Bubble Sort
❖ Selection Sort
• In search process, an extra variable is required which will be used to
swap the values of two array elements during the process.
• Nested Loop is required to do sorting process.
WAP to input 10 integer values in array. Using Selection sort
technique, sort and print the values in ascending order
import java.util.*;
for(i=0 ; i< 4;i++)
public class selectionsort
{
{
min=i;
static void main()
for(j=i+1 ;j< 5; j++)
{
{
if(m[min]>(m[j]))
Scanner ob=new Scanner(System.in);
min=j;
}
int m[]=new int[5];
t=m[min];
m[min]=m[i];
int i,j,min;
m[i]=t;
int t;
}
System.out.println("Enter 5 nos");
System.out.println("sorted nos");
for(i=0 ; i< 5 ;i++)
for(i=0 ; i< 5 ;i++)
{
{
m[i]=ob.nextInt();
System.out.println(m[i]);
}
}
}
}
WAP to input 10 integer values in array. Using bubble sort technique,
sort and print the values in ascending order
for(int i = 0 ; i < len-1 ; i++)
import java.util.*; {
class BubbleSort for(int j = 0 ; j < len – 1 – i ; j++)
{ {
public static void main() if(m[j]>m[j+1])
{ {
int m[] = new int[10]; t = m[j];
int len = a.length; m[j] = m[j+1];
Scanner sc = new Scanner(System.in); m[j+1] = t;
System.out.println("Enter "+len+" values"); }
}
for(int i = 0 ; i < len ; i++) }
{ System.out.println("\n\n***** Final Sorted Value *****");
m[i] = sc.nextInt(); for(int i = 0 ; i < len ; i++)
} {
int t; // Temporary variable for swapping System.out.print(a[i]+"\t");
}
}
}
Difference between Linear Search and Binary Search
Sort Search
It arranges the value in It looks / find the value in the
ascending / descending order array element
Two type: Bubble sort and Two type: Linear Search and
Selection sort Binary Search