Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Array Theory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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

Declaration of an array: Array can be declared using given syntax.


Datatype array_name[]=new Datatype[size];
int arr[]=new int[10];
It will create an array names arr of size 10.
Array is always start with 0. First element is arr[0] and last element is arr[9].
String str[]=new String[5];
It will create an String array names str of size 5.
First element is str[0] and last element is str[4].

Array initialization : Array can be initialize using given syntax.


Datatype array_name[]={value1, value2,value3…….…..};
For example
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
char c[]={‘a’, ‘e’, ‘i’, ‘o’, ’u’};
note:- array can be initialized at the time of declaration otherwise we need to initialize each
element separately .

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;

A simple Program to demonstrate the functioning of Arrays: ….. (i)


class Arrays
{
public static void main()
{
int m[] = {12,58,69,87,45};
System.out.println(“The elements in the Array are : “);
for(int i=0;i<m.length;i++)
{
System.out.print(m[i]+” “);
}
}
}

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.

arr[0][0] arr[0][1] arr[0][2] ` arr[0][3]


arr[1][0] arr[1][1] arr[1][2] arr[1][3]
arr[2][0] arr[2][1] arr[2][2] arr[2][3]

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

A simple Program to demonstrate the functioning of 2D arrays :


class Arrays2D
{
public static void main()
{
int m[][]={{1,5,6,9},{2,5,9,7},{15,5,78,9}};
for(int i=0;i<m.length;i++) // number of rows
{
for(int j=0;j<m[0].length;j++) // number of columns
{
System.out.print(m[i][j]+” “);
}
System.out.println();
}
}
}
Searching : Searching is a process in which the entered value is checked with the already present
values to find out whether the entered value is present in the array.
Two types of Searching Methods ,

(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.

Example Program for binary search in Java,


import java.util.*;
class BinarySearch
{
public static void main()
{
int c, first, last, mid, n, num,p=0;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of elements”);
n = sc.nextInt();
int arr[] = new int[n];
System.out.println(“Enter ” + n + ” integers”);
for (c = 0; c < n; c++)
arr[c] = sc.nextInt();
System.out.println(“Enter value to find”);
num = in.nextInt();
first = 0;
last = n – 1;
while( first <= last )
{
mid = (first + last)/2;
else if ( arr[mid] == num )
{
System.out.println(num + ” found at index ” + mid + “.”);
p++;
break;
}
else if ( num>arr[mid] )
first = mid + 1;
else
last = mid – 1;
}
if ( p==0)
System.out.println(num + ” is not present in the list.”);
}
}

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]);
}
}

You might also like