Array
Array
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
Array
Table of Content
What is an Array?
Need of Array Data Structures
Types of Array Data Structures
Array Operations
Application of Array
Array:
Array is the name of the consecutive memory locations that all have same name and same type.
Or
An array is a collection of items of the same variable type that are stored at contiguous memory locations
It’s one of the most popular and simple data structures and is often used to implement other data
structures. Each item in an array is indexed starting with 0 . Each element in an array is accessed through
its index.
Or
Array is a finite number of homogeneous data having a common name, a unique index, and
stored in consecutive memory location in the computer memory.
By finite number mean that size of the array should be known. By homogenous mean that the data
should be of the same type. By consecutive mean that the data item of the array stored one after the other
in computer memory.
1. One-Dimensional Array(ODA)
2. Two-Dimensional Array
Where UB represents the upper bound of the one-dim ensional array and LB is
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
lower bound of the one-dimensional array.
Representation of Array
The representation of an array can be defined by its declaration. A declaration means allocating
memory for an array of a given size.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
MA (i) = SA + (i-1) * w
Where: SA = 1010 & w = 2
MA (4) = 1010 + (4-1) * 2
MA (4) = 1010 + 3 * 2
MA (4) = 1010 + 6
MA (4) = 1016
The value at address 1016 is 40.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
cin>>LA [K];
// Start TRAVERSING ALGORITHM
cout<<endl<<"Traversing Elements of the Array Starting from LB to
UB: ";K = LB;
while (K<=UB)
{
cout<<LA
[K]<<" ";K =
K+1;
}
// End of TRAVERSING ALGORITHM
getch ();
}
Insertion Operation:
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to
care about the position at which the element is to be placed.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
cin>>A[I];
cout<<endl<<"Elements in ODA: ";
for (I=0; I<N; I++)
cout<<A[I]<<" ";
cout<<endl<<endl<<"Enter the element to insert into ODA: ";
cin>>ITEM;
cout<<endl<<"Enter position of the element to be inserted: ";
cin>>K;
// Start INSERTION ALGORITHM
I = N;
while(I>K)
{
A [I] = A
[I-1];I =
I-1;
}
A[K] = ITEM;
N = N+1;
cout<<endl<<"Total number of elements in array after insertion of an element: "<<N<<endl;
// End of Insertion Algorithm
cout<<endl<<"Array after insertion of an element: ";
Delete Operation:
In the delete operation, the element to be deleted is searched using the linear search, and then the delete
operation is performed followed by shifting the elements.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
I isthe counter variable.
clrscr ();
int A [200], I, K, N;
cout<<"Enter Total number of filled memory locations in array: N = ";
cin>>N;
cout<<endl<<"Enter N number of elements into
Array:"<<endl<<endl;for (I=0; I<N; I++)
cin>>A[I];
cout<<endl<<"Elements in
Array: ";for (I=0; I<N; I++)
cout<<A[I]<<" ";
cout<<endl<<endl<<"Enter position of an element to be deleted: k = ";
cin>>K;
// Start DELETION ALGORITHM
I = K;
cout<<endl<<"Element to be deleted at position "<<I<<": "<<A[I]<<endl<<endl;
while (I<N-1)
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
{
A [I] = A [I+1];
I = I+1;
}
N = N-1;
A [N] = -1;
// End of DELETION ALGORITHM
cout<<"Total number of elements in array after deletion an element: "<<N<<endl<<endl;
cout<<"Array after deletion of an element:"<<endl;
for (I=0; I<=N; I++)
cout<<A[I]<<" ";
getch ();
}
TWO-DIMENSIONAL ARRAY
Introduction to Two-Dimensional Array:
Two dimensinal array has two dimensions which consist on both rows and columns.
A two dimensional array is declared by giving two indexed values in square brackets.
The first indexed value represents the total number of rows and the second represents
the total number of columns. E.g. Int arr [5][5];
A two dimensional array is called matrix in mathematics and table in the database.
Two Dimensional Array Program-01:
#include<iostream.h>
#include <conio.h>
void main()
{
clrscr ();
int i, j;
int A [3][3]={{10,20,30},{40,50,60},{70,80,90}};
cout<<"Two Dimensional Array Elements: ";
for (i=0; i<3; i++)
for (j=0; j<3; j++)
cout<<" "<<A[i][j];
getch();
}
Size or Length of Two Dimensional Array:
If m is the number of rows and n is the number of columns then the formula
forthe Size of Two Dimensional Array is as under:
Length or Size = m × n
Representation of Two-Dimensional Array in computer memory:
When two-dimensional array is stored in computer memory, it is first mapped
into a single vector because two-dimensional array is logically represented in rows and
columns but physically mapped into computer memory in vector form. It is either
mapped ina Row-by-Row Vector or a Column-by-Column Vector.
Row-by Row Vector Method
Column-by-Column Vector Method
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
Row-by Row Vector Method or Row-by-Row Major Order:
In row-by-row major order, first row is stored/mapped, then second row and so on.
For example:
1 3 5
A= 2 4 6
7 8 9
1 3 5
A= 2 4 6
7 8 9
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
1 3 5
A= 2 4 6
7 8 9
i = 3, j = 2, SA = 1010, n = 3, w =
2 MA (i, j) = SA + {n (i - 1) + (j –
1)} * w
MA (3, 2) = 1010 + {3 (3 - 1) + (2 – 1)} * 2
MA (3, 2) = 1010 + {3 (2) + (1)} * 2
MA (3, 2) = 1010 + {6 + 1} * 2
MA (3, 2) = 1010 + {7} * 2
MA (3, 2) = 1010 + 14
MA (3, 2) = 1024
Thus, the value at the address 1024 is 8.
MA (i, j) = SA + {m (j - 1) + (i – 1)} * w
For example:
1 3 5
A= 2 4 6
7 8 9
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
(1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3)
1 2 7 3 4 8 5 6 9
1010 1012 1014 1016 1018 1020 1022 1024 1026
i = 2, j = 3, SA = 1010, m = 3, w =
2 MA (i, j) = SA + {m (j - 1) + (i –
1)} * w
MA (2, 3) = 1010 + {3 (3 - 1) + (2 – 1)} * 2
MA (2, 3) = 1010 + {3 (2) + (1)} * 2
MA (2, 3) = 1010 + {6 + 1} * 2
MA (2, 3) = 1010 + {7} * 2
MA (2, 3) = 1010 + 14
MA (2, 3) = 1024
Computer systems are often used to store large amounts of data from which individual
records are retrieved according to some search criteria.
The process of finding a specific data item or record from a list is called searching.
OR
It is the process by which can find the specific location of a given item in a list.
OR
Searching is a technique of finding an element from the given list or a set of elements like
arrays, list or trees.
If the item exists in the given list, then search is said to be successful otherwise if the element
is not found in the given list then search is said to be unsuccessful.
Internal search: The search in which the whole list resides in the main memory is called
internal search.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
External search: The search in which most of the list resides in the secondary
memory is called external search.
Best Case: The best case is that in which the element is found during the first
comparison.
Worst Case: The worst case is that in which the element is found only at the end
or thelast comparison or not found.
Average Case: The average case is that in which the element is found in
comparisons more than best case but less than worst case.
Types of Searching:
1. Linear Search
2. Binary Search
1. Linear Search:
Linear Search is also called sequential search. It is the simplest way for finding an element
in alist. In Linear Search, the elements find sequentially in a list, no matter whether the list
is sorted or unsorted.
In case of sorted list, the search is started from 1st element and continuous until the element
is found or the element whose value is greater than the value being searched.
In case of unsorted list, the search is started from 1st location and continuous until the
elementis found or the end of the list is reached.
Linear Search is a very slow process. It is used for small amounts of data. This method is
not recommended for large amount of data.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
Example: Sorted Array
Data
0 1 2 3 4 5 6 7 8 9 10 11 12
A [13] = 10 20 30 40 50 60 70 80 90 100 110 120 130
Algorithm for Linear Search: This algorithm is used for linear searching. ITEM
is to be searched in a linear array A having N numbers of elements. LOC is variable
which store the location in case of successful search and ‘I’ is a counter variable.
Algorithm for Sorted Array: LINEAR SEARCH (A, LOC, I, N, ITEM)
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
Step 1: [Initialization]
I = 0 and LOC = -1
Step 2: [Starting Loop to Search]
Repeat step 3 & 4 ... While (I < N)
Step 3: [Comparing given element]
If (ITEM == A [I]) then
LOC: = I
Break
(These statements will not be included for unsorted array)Else if (ITEM < A [I]) then
Break
[End of IF Structure]
Step 4: [Increment counter variable]
I = I+1
[End of Loop]
Step 5: [Display Result]
If (LOC == -1) then
Write (“Element is not Found”)
Else:
Write (“Search is Successful”)
Write (“ITEM is found at Location”, LOC)
[End of IF Structure]
Step 6: [Finish]
Exit
1. Binary Search:
Binary search is the most efficient searching technique for finding an element in a sorted
list/array. In binary search, first, find the middle index of the list/array, then compare
the element (which you want to search) with the middle element of the list/array. If they
are equal, the search is successful otherwise if the element is greater than the middle
element of the list/array, then right side of the list/array will be searched and if the
element is less than the middle element, then left side of the list/array will be searched in
similar way.
Example
01 2 3 4 5 6 7 8 9 10 11 12
10 20 30 40 50 60 70 80 90 100 110 120 130
Find the ITEM = 50?
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar
BEG=0 & END=12, MID=INT ((0+12)/2) = 6 so A[MID]=70 and 50 < 70.
2. Since 50 < 70 therefore BEG = 0 & END = MID - 1 = 6 – 1 = 5
MID = INT ((0 + 5) / 2) = 2.5 = 2 so A [MID] = 30 and 50 > 30.
3. Since 50 > 30 therefore BEG = MID + 1 = 2 + 1 = 3 & END = 5
MID = INT ((3 + 5) / 2) = 4 so A [MID] = 50 and 50 = 50.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar