Array Theory
Array Theory
Array Theory
An array is a collection of variable of the same type that are referenced by a common name. It
allocates the memory for the same data type in sequence. they are of two type single
dimension and multi dimension.
An individual variable in the array is called an array element. The locations in which they are
stored is called array index or array subscript hence arrays are also sometimes referred as
Subscripted variables.
the data type of the array is called base type of the array
length of an array : Length of an array refers to the number of elements in the array. To
determine the length of an array the following general syntax is used:
<array-name>.length;
ArrayIndexOutOf Bound : It is an Exception which occurs when we try to access any element
which is beyond the valid index of the array that is 0 to size-1.
Advantage of array :
i. Since elements of the arrays are stored in contiguous memory locations the sequential access
of elements is very fast.
ii. Random access is of elements is possible. It is not necessary to access the members
sequentially.
Disadvantage of array:-
i. one should know the size of the array at the time of declaration.
ii.Once array is declared we cannot change the size of array which some times lead unnecessary
reservation of memory.
Double Dimensional array : A double dimensional array is collection of one dimension array
which are divided in rows and columns. 2D array is also called Matrix. The general form of a
two-dimensional array is
data type name[ ] [ ] = new data type [row size] [column size];
int arr[][]=new int[3][4];
this statement create an array of 3 rows and 4 columns.
A specific element in a two-dimensional array is accessed by its row index and column index. For
example, if a is a two-dimensional array of integers, then a[i][j] is the integer present in ith row
and jth column in the array.
2D Array initialization : Two dim. Array can be initialize using given syntax.
Datatype name[][]={{ values of first rows},( values of second rows },…….…..};
int arr[][] = {{1,5,7,6},{3,8,9,6}};
In the above statement we initialize an array of 2 rows and 4 column.
length of 2D array : To determine th number of rows and column of 2D array the following
general syntax is used:
<array-name>.length; // return number of rows in 2D array
<array-name[0]>.length; // return number of column in 2D array
int arr[][]=new int[3][4];
arr.length // return 3
arr[0].length //return 4
(i) Linear Searching : Linear Search is a search process technique that involves checking each
element of the array sequentially(one by one) with the given element to be searched for.
Example Program on Linear Searching ,
import java.util.*;
class LinearSearch
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int arr[]=new int[10];
System.out.println(“enter 10 Elements in the Array “);
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextInt();
System.out.println(“Enter the number to be searched “);
int num = sc.nextInt();
int pos = -1;
for(int j=0;j<arr.length;j++)
{
if(num==arr[j])
{
pos=j;
break;
}
}
if(pos>=0)
System.out.print(“Search successful, “+num+” is present at position : “+pos);
else
System.out.println(“Search unsuccessful ! “);
}
}
(ii) Binary Searching : Binary search can only be applied to array whose elements are in either
ascending or descending order. In this technique the element to be searched is checked with the
central element of the array and depending upon its value, it is checked in the sub-array before
the central position or in the sub-array after the central position. The same search process
continues even with the sub-array.
Difference between linear search and binary search: Technique of binary search can only
be implemented on an array that is arranged in ascending or descending order. However, to
implement linear search it is not essential that the array should be sorted.
Binary search is more efficient and fast searching algorithm than linear search because each
comparison eliminates from consideration half of the elements in the array. Linear search
algorithm is slow as it searches each element in an array sequentially until it finds the correct
element.
2 .Sorting Sorting is a process in which the elements of the array are either arranged in
ascending or descending order.
Two methods of sorting is -
(i)Selection Sort : if the array is to be arranged in ascending order, the smallest element
needs to be selected and exchanged with the first position element in the array. Again, the
smallest element from the remaining elements is selected and exchanged with second element .
The process continues till all elements are arranged. If the array needs to be arranged in
descending order. Select the largest element.
class Selectionsort
{
public static void main()
{
int arr[]={1,5,3,8,2,9,10};
int n=a.length; //
for(int i=0;i<n-1;i++)
{
int min=arr[i];
int pos = i;
for(int j=i+1;j<n;j++)
{
if[min<arr[j]) // for descending order min>arr[j]
{
min=arr[j];
pos = j;
}
}
int temp=arr[i];
arr[i]=arr[pos];
a[pos]=temp;
}
for(int x=0;x<n;x++)
{
System.out.println(arr[x]);
}
}
}
BUBBLE SORTING : The bubble sorting process checks adjacent elements of the array, i.e.
first element with the second element, the second element with the third element and the
process continues till last element. In case the array element is greater than the adjacent
element, the elements are interchanged. The process continues until all elements are arranged.
class BubbleSort
{
public static void main()
{
int arr[] = {11,10,9,7,5,4};
int n = arr.length;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
for(int i=0;i<n;i++){
System.out.print(arr[i]);
}
}