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

Lecture 04

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.

Figure 3.1 indexes of array

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

Figure 3.2 indexes of 1-D array

3.4.2 2-D array


A 2-d array is an array in which each element is itself an array.
Declaration: Datatype array name [rows] [columns];
Example: Int a [3] [4];

Figure 3.3 indexes of 2-D array

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

Figure 3.4 indexes of multi-dimensional array

3.5 Searching in Arrays


Searching means to find the index of a particular value that exist in the array and display a
massage to user if value does not exist. Following are some ways of searching.
I. Linear search
II. Binary search
3.5.1 Linear search / sequential or serial search
It is the simple way of searching in an array. It has following steps.
I. Visit the 1st element of array and compare it with the desired value.
II. If it matches search is stopped and index is found.
III. If it does not match the process begin for the next element.
Loops are commonly used for this purpose we start the counter of outer loop from 0 and move it
to the next index. For example, if array has 10 elements counter will move from 0 to 9 index.
Mostly we use this method.

4
Figure 3.5 linear search

3.5.2 Binary Search


Binary search is a quicker method of finding element in an array. But it can be only applied at
sorted array.
I. It locates the middle element of array and compare it with the searched element.
II. If they are equal search is stopped and index is found.
III. If they are not equal it reduces the array to half.
IV. If the searched number is less than middle number. It searches the first half of array
otherwise it will search the second half of array. The process continues till the required number
is found.

5
Figure 3.6 Binary search

3.6 Sorting an Array


Sorting is a technique to place the elements of the array in a specific order. It can be lowest to
highest (Ascending) or highest to lowest (descending). Following are different ways of sorting.
I. Selection sort
II. Bubble sort
III. Insertion sort
3.6.1 Insertion sort
In insertion sort the array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted part. Insertion sort is a
simple sorting algorithm more efficient than the Bubble sort and Selection sort.

6
Figure 3.7 insertion sort

3.6.2 Selection sort

figure 3.8 (a) Selection sort

7
figure 3.8 (b) Selection sort

8
3.7 Example of 2-D array

Figure 3.9 Example of 2-D array

You might also like