Professional Documents
Culture Documents
Introduction To Data Structures and Algorithms - Lecture 4
Introduction To Data Structures and Algorithms - Lecture 4
Arrays
Linear data structures
3.1 What are linear data structures
A Linear data structure have data elements arranged in sequential manner and each member
element is connected to its previous and next element. This connection helps to traverse a linear
data structure in a single level and in single run. Following are linear data structures.
I. Arrays
II. Stack
III. Queue
IV. Linked list
3.2 Arrays
Array is a collection of variables that can hold value of same type and reference by common
name. It is a derived data Structure. They always contain continuous memory. Their indexes start
from zero.
1
3.3 Initialization of Arrays
3.3.1 Method 1
Int Array1 [5] = {1,2,3,4,5};
This means that Array1 is an integer type array of 5 digits which are enclosed in curly brackets.
3.3.2 Method 2
Int Array1 [ ] = {1,2,3,4,5,54,65,23};
This means that Array1 is an integer type of array of unspecific size. In this type complier
automatically allocate size to array according to the number of elements enclosed in curly
brackets.
3.3.3 Method 3
Int Array1[3];
Array1[0] =123;
Array1[1] =432;
Array1[2] =974;
In this way we are assigning values to each index by ourself. The indexes of arrays are always
start from 0 and continue to reach higher number.
3.3.4 Method 4
Int array1[ 5 ];
for (int i =0; i<5; i++)
cin>>array1[ I ];
In this way we use loop to take input at every index of array. The value of i changes at every
iteration so it takes value next index to the previous.
2
3.4 Dimensions of Arrays
3.4.1 1-D array
A one-dimensional array is one in which one subscript /indices specification is needed to specify
a particular element of array.
Declaration: Datatype array name [size of array];
Example: Int num [5];
3
3.4.3 Multi-Dimensional array
An array with dimensions more than two. The maximum limit of array is compiler dependent.
Declaration: Datatype name [a][b][c][d][e][f] ……. [n];
Array of 3 or more dimensional are not often use because of huge memory requirement and
complexity involved
4
Figure 3.5 linear search
5
Figure 3.6 Binary search
6
Figure 3.7 insertion sort
7
figure 3.8 (b) Selection sort
8
3.7 Example of 2-D array