You are on page 1of 20

Arrays in C++

Presentation by
Pawan Tak (PGT Comp Sci )
KV Sabarmati A’md
ARRAY

 An array is a data structure which allows a


collective name to be given to a group of
elements which all have the same type. An
individual element of an array is identified by
its own unique index

 An array is a collection of homogeneous data


elements described by a single name.
Declaration of Array
An array declaration is very similar to a variable
declaration. First a type is given for the elements of the
array, then an identifier for the array and, within square
brackets, the number of elements in the array. The number
of elements must be an integer.

For example data on the average temperature over the


year in Ahmedabad for each of the last 15 days could be
stored in an array declared as follows:

int temp[15];
0 1 2 3 4
Temp 30 30 35 34 26

1st
2nd
Element
Element
1000 1002 1004 1006 1008
One Dimensional Array
A one dimensional array is a list of variables that are
all of the same type and are referred through a
common name. An individual variable in the array is
called an array element.

One dimensional array are implemented by


allocating a sequence of addressed location so as to
accommodate all its elements the starting address
of the very first element of the array is called base
address of the array. The elements are given
contiguous memory locations
Basic Operations on One Dimensional Array
Linear Search (Sequential Search)

In the linear search each element of the array is compared with


Given item to be searched one by one.

int Lsearch(int A[ ], int item)


{
for (int i = 0; i<50; i++)
{
if (A[i] == item)
return i;
}
return -1; // if the element in the array is not found
}
Binary Search
For this kind of search the array must be the
sorted order. To search for the item in the sorted
array the item is compared with middle element
segment . If the item is more than the middle
element then, latter part of the segment becomes
new segment to be scanned else Former part is
scanned
int Bsearch(int A[ ],int size, int item)
{ int beg, last,mid;
beg = 0;
last = size-1;
while (beg <=last)
{
mid = (beg + last)/2;
if (item = = A[mid])
retuen mid;
else if (item > A[mid]);
beg = mid +1;
else
last = mid – 1;
}
}
Insertion in Array
Insertion of new element in the array can be
done in two ways
(i) If the array is unordered, the new element is
inserted at the end of the array.
(ii) If the array is sorted then new element is
added at appropriate place without altering
the order and to achieve this, rest of the
elements are shifted.
Insertion in Array
10 10 10
15 15 15
16 16 16
18 18 18
20 19
25 20 20
26 25 25
29 26 26
35 29 29
35 35
Original Element Array after
Shifted Insertion
Array
Deletion in Array
In the deletion method the element to be
Deleted is first searched for in the array using
One of the search technique i.e either linear or
Binary search. If the search is successful, the
Element is removed and the rest of the element
Shifted so as to keep the order of array
Undisturbed.
Deletion in Array
10 10 10
15 15 15
16 16 16
18 18 18
20 20 20
25 26
26 26 29
29 29 35
35 35 40
40 40
Selection Sort
The basic idea of a selection sort is to
Repeatedly select the smallest key element in
The remaining unsorted array Smallest

45 15 16 2 6
2 45 15 16 6
2 6 45 15 16 Smallest
2 6 15 45 16 Smallest
2 6 15 16 45

6 2 16 15 45
2 6 16 15 45 Smallest
2 6 16 15 45
2 6 15 16 45
2 6 15 16 45
Selection Sort
Void selsort(int A[ ], int size)
{
int small, pos, tmp;
for(int i = 0; i<size; i++)
{
small = A[i];
pos = i;
for (int j = i+1; j<size; j++)
{ if(A[j]<small)
{
small = A[j]
pos = j;
}
}
tmp = A[i];
A[i] = A[pos];
A[pos] = tmp;
}
}
Bubble Sort

The basic concept of bubble sort is to compare


Two adjoining values and exchange them if
They are not in proper order.
10 10 10 10 10 10
12 12 11 11 11 11
11 11 12 12 12 12
48 48 48 48 18 18
18 18 18 18 48 20
20 20 20 20 20 20

Similarly so on iterations ………….


Bubble Sort
void bsort(int A[ ], int size)
{int tmp;
for(int i = 0; i<size; i++)
{for (int j = 0; j<(size-1)-i; j++)
{ if(A[j] > A[j+1])
{ tmp = A[j];
A[j] = A[j+1];
A[j+1] = tmp;
}
}
}

}
Two Dimensional Array
 A two dimensional array is an array in which
Each element itself is an array. For example

 A[M][N] is an M by N table with M rows and N


Columns containing M x N elements
Row Major Implementaion

0 1 2

0 1000 1002 1004

1006 1008 1010


1

1012 1014 1016


2
Column Major Implementation
0 1 2

0 1000 1006 1012

1 1002 1008 1014

2 1004 1010 1016

You might also like