CHAPTER 3 - ARRAYSds
CHAPTER 3 - ARRAYSds
CHAPTER 3 - ARRAYSds
CHAPTER - 3
ARRAYS
Data structures are classified into two categories- linear and non-linear. A data structure is said to
be linear if its elements form a sequence, or a linear list. Elements in a non-linear data structure
do not form a sequence. There are two ways of representing linear structures in memory. One
way is to have the linear relationship between the elements by means of sequential memory
locations. Such linear structures are called Arrays.
Arrays are useful when the number of elements to be stored is fixed. They are easy to traverse,
search and sort.
An Array is a finite collection of homogeneous values stored in adjacent memory locations. Here
‘finite’ mean that there are specific number of elements in an array and by ‘similar’ mean that
the all the elements in an array are of the same type. The data type could be of type char, in
which case we have a string. The data type could just as easily be of type int, float or even
another array.
The operations that can performed on any linear structure, whether it may be an array or linked
list, include the following:
OPERATIONS ON ARRAYS
An array containing n number of elements is referenced using an index that varies from 0 to n-1.
For example, the elements of an array a[n] containing n elements are denoted by a[0],a[1],a[2]…
a[n-1], where 0 is the lower bound and n-1 is the upper bound of the array.
An array is a set of pairs of an index and a value. For each index there is a value associated with
it. For each index, there is a value associated with it. For example,
The number of elements is called the length or size of the array. In the above situation n is the
length of the array.
TRAVERSAL
Let ‘a’ be a collection of data elements stored in the memory of the computer. In order to print
the contents of each element of ‘a’ or suppose to count the number of elements of a with a given
property. This is accomplished by traversing ‘a’, i.e., by accessing and processing each element
of ‘a’ exactly once.
1) Start
2) // Initialize counter
Set k: =LB.
{
3) // APPLY PROCESS TO LA[K]. PROCESS is scaling the array elements by 2.
LA[K] = LA[K] * 2;
Set k: =k + 1.
5) Stop
Algorithm
INSERT (LA, N, K, ITEM)
// Here LA is a linear array with N elements and k is a positive integer such that K <= N. This
1) // Initialize counter
Set J := N.
4) // Decrease counter
Set J : = J – 1
5) // Insert element
6) // Reset N
Set N:=N+1.
7) Exit.
DELETION
Deleting an element at the end of an array presents no difficulties. But deleting an element from
the middle of the array would require that each subsequent element be moved one location
upward in order to fill up the array.
Algorithm
DELETE (LA, N, K, ITEM)
// Here LA is a linear array with N elements and k is a positive integer such that K <= N. This
2) Repeat for J = K to N – 1:
[End of loop]
Set N := N – 1.
4) Exit
Algorithm
BUBBLESORT( int A[ ], N)
// This algorithm sorts the elements in the array in ascending order
1) Start
2) Repeat steps 2 and 3 for K = 1 to N-1
{
PTR = 1;
3) Repeat steps while ptr <= N - K
{
4) IF A[PTR] > A[PTR + 1]
{
temp = A[PTR];
A[PTR] = A[PTR+1];
A[PTR+1] = temp;
}
5) Stop
Complexity
Time Complexity:
Worst case: O(n*n).
Average case: O(n*n).
Best Case: O(n).
Space Complexity: O(1)
MERGING
Merging of arrays involves two steps- sorting the arrays that are to be merged, and adding the
sorted elements of both the arrays to a new array in a sorted order.
Method
1) Declare two arrays and input the array elements in both the arrays
Array A elements:1 2 3 4 5
Array B elements: 6 7 8 9 10
Output- The Merged Sorted Array C: 1 2 3 4 5 6 7 8 9 10
Algorithm MERGE (int A[ ], int B[ ], int c, int m, int n)
int i,j,k;
1) Initialize i=0, j = 0, k = 0;
2) Repeat steps while(i<m && j<n)
{
3) if(A[i] <= B[j])
{
C[k] = A[i];
i++;
}
else
{
C[k] = B[j];
j++;
}
k++;
}
4) for(int p=i; p<m; p++)
{
C[k] = A[P];
k++;
}
5) for(int p=j; p<n; p++)
{
REVERSING
Figure: Reversing
Algorithm
REVERSE ARRAY (int arr[], int start, int end){
int temp;
temp = arr[start];
arr[start] = arr[end];
end = end – 1
print(arr[i]); }}
Two-Dimensional Arrays
A 2-dimensional array is a collection of elements placed in m rows and n columns. The syntax
used to declare a 2-D array includes two subscripts, of which one specifies the number of rows
and the other specifies the number of columns of an array. These two subscripts are used to
reference an element in an array.
Example: a[4][3] is a 2-D array containing 4 rows and 3 columns and a[0][2] is an element
placed at 0th row and 2nd column in the array. The two-dimensional array is also called a matrix.
The pictorial representation of a matrix is shown in figure below:
Figure: Matrix
The horizontal lines in a matrix are called rows and the vertical lines are called columns. A
matrix with m rows and n columns is called an m-by-n matrix (or m×n matrix) and m and n are
called its dimensions. Information that is stored in a rectangular array may not require every
position in the rectangle for its representation.
The entry of a matrix A that lies in the i -th row and the j-th column is called the i,j entry or (i,j)-
th entry of A. This is written as Ai,j or A[i,j].
Matrix addition is the sum or difference of the individual elements and this is the same as the
simple array arithmetic operations. If two matrices have the same dimensions, they may be added
together. The result is a new matrix with the same dimensions in which each element is the sum
of the corresponding elements of the previous matrices.
Matrix subtraction is like addition. Each element of one matrix is subtracted from the
corresponding element of the other. If a scalar is subtracted from a matrix, the former is
subtracted from every element of the latter.
For a linear array LA, the computer does not keep track of the address
To find the address of a[n] in time independent of n. (Here w is the number of words per memory
cell for the array LA, and 1 is the lower bound of the index set of a.)
A similar situation also holds for any two-dimensional m x n array a. That is, the computer keeps
track of Base(a) – the address of the first element a[1, 1] of a – and computes the address
LOC(a[j,k]) of a[j,k] using the formula
int a[3][4] = {
{ 22, 10,-3,4 } ,
{ 15, 9, 81, 123 },
{ 7, 87, 59, 34 }
}
Shown below are the possible arrangements of 2-D array.
Since the array elements are stored in adjacent memory locations we can access any element of
the array once we know the base address (starting address) of the array and number of rows and
columns are present in the array.
Example, if the base address of the array shown in Figure is 502 and we wish to refer the element
123, then the calculation involved would be as follows.
Example:
int *ptr;
int arr[5];
ptr = arr
Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of
the first element of the array in variable ptr.
int *ptr;
int arr[5];
ptr = arr;
Let us assume that system has allocated memory location 80F for a variable a.
int a = 10;
We can access the value 10 either by using the variable name a or by using its address 80F.
The variables which are used to hold memory addresses are called Pointer variables.
A pointer variable is therefore nothing but a variable which holds an address of some other
variable. And the value of a pointer variable gets stored in another memory location.