Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Arrays 1D and 2D

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

Data Structure and Algorithms

Array data structure

Puneet Kumar Jain

CSE Department
National Institute of Technology Rourkela
Reference
 Most of the content of this presentation belongs to the following
book:

Reema Thareja, “Data structures using C”, 2nd edition, Oxford


University Press, 2014

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


In this topic
 We will learn how arrays are defined, declared, initialized, and
accessed.

 We will also discuss the different operations that can be


performed on array elements and their complexity

 Different types of arrays such as two-dimensional arrays, multi-


dimensional arrays, and sparse matrices

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Array
 An array is a user-defined data type that stores related
information together in a linear manner

 An array is a collection of similar data elements. These data


elements have the same data type.

 The elements of the array are stored in consecutive memory


locations and are referenced by an index (also known as the
subscript)

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


OPERATIONS ON ARRAYS
 Traversing/accessing an array element
 Inserting an element in an array
 Deleting an element from an array
 Searching an element in an array

 Merging two arrays


 Sorting an array in ascending or descending order

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Operations
Operations at the kth entry of the list include:

Access to the object Erasing an object

Insertion of a new object Replacement of the object

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Traversing
 Since, array is a linear data structure (because all its elements
form a sequence), traversing its elements is very simple and
straightforward.

 Algorithm for array traversal


Step 1: [INITIALIZATION] SET I = lower_bound
Step 2: Repeat Steps 3 to 4 while I <= upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I = I + 1
[END OF LOOP]
Step 5: EXIT

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Traversing the array: simple example
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20];
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]); Output
Enter the number of elements in the array : 5
}
arr[0] = 1
printf("\n The array elements are "); arr[1] = 2
for(i=0;i<n;i++) arr[2] = 3
printf("\t %d", arr[i]); arr[3] = 4
return 0; arr[4] = 5
The array elements are 1 2 3 4 5
}
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Accessing the elements of array

 Calculating the Address of Array Elements


Address of data element, A[k] = BA(A) + w(k – lower_bound)
BA: Base address (the name of the array point to it)
W: size of one element
K: index of the desired element
Lower bound: generally it would be 0

 Considering that storing an integer value requires 2 bytes, therefore, its


size is 2 bytes.
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008

Time and space Complexity: Theta(1)


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Inserting an Element in an Array
 Inserting element at the end
upper_bound: indicates number
Step 1: Set upper_bound = upper_bound + 1 of elements in the array
Step 2: Set A[upper_bound] = VAL
Step 3: EXIT
 Time Complexity :Theta(1), Space complexity: Theta(1)

 Inserting element in the middle


Step 1: [INITIALIZATION] SET I = N
Step 2: Repeat Steps 3 and 4 while I >= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SET I = I – 1
[END OF LOOP]
Step 5: SET N = N + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT New element
 Time Complexity: Theta(n) , Space complexity: Theta(1)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Deleting an Element from an Array
 Deletion of the last element
Step 1: SET upper_bound = upper_bound - 1
Step 2: EXIT
 Time Complexity :Theta(1), Space complexity: Theta(1)

 Deletion of an element in the middle


Step 1: [INITIALIZATION] SET I = POS
Step 2: Repeat Steps 3 and 4 while I <= N – 1
Step 3: SET A[I] = A[I + 1]
Step 4: SET I = I + 1
[END OF LOOP]
Step 5: SET N = N – 1
Step 6: EXIT

Time Complexity: Theta(n) ,


Space complexity: Theta(1)

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Searching in the array

Worst-case O(n)
Average-case O(n)
Best-case O(1)
Worst-case space O(1)

Worst case: O(log n)


Average cae O(log n)
Best case: O(1)
Space complexity: O(1)

Image ref: https://blog.penjee.com/binary-vs-linear-search-animated-gifs/


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Merging Two Arrays
 Merging two arrays in a third array means first copying the
contents of the first array into the third array and then copying
the contents of the second array into the third array.

 If the arrays are unsorted, then merging the arrays is very simple,
as one just needs to copy the contents of one array into another.

 Time complexity: O(m+n), space complexity: O(m+n)


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Merging Two Arrays
 But merging is not a trivial task when the two arrays are sorted
and the merged array also needs to be sorted.

 Time complexity: O(m+n), space complexity: O(m+n)

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Sorting

Image Ref: https://medium.com/@george.seif94/a-tour-of-the-top-5-sorting-algorithms-with-python-code-43ea9aa02889


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Two- Dimensional (2D) array
 Declaring 2D array:
data_type array_name[row_size][column_size];
 For example:
int marks[3][5];

 Initializing Two-dimensional Arrays


int marks[2][3]={90, 87, 78, 68, 62, 71};
int marks[2][3]={{90,87,78},{68, 62, 71}};

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


2D array in memory

Image ref: https://dyclassroom.com/c/c-pointers-and-two-dimensional-array


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
2D array in memory
 Although we have shown a rectangular picture of a two-
dimensional array, in the memory, these elements actually will
be stored sequentially
 There are two ways of storing a two-dimensional array in the
memory.
 The first way is the row major order and
1 2 3 4 5 6 7 8 9 10 11 12

 the second is the column major order.

1 5 9 2 6 10 3 7 11 4 8 12

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


2D array in memory
 Using the base address, address of the other elements is
calculated using the following formula.
 In row major order,
Address(A[I][J]) = Base_Address + w{N ( I – Lr) + (J – Lc)}
 In column major order,
Address(A[I][J]) = Base_Address + w{M ( J – Lc) + (I – Lr)}

 W: number of bytes required to store one element,


 N is the number of columns,
 M is the number of rows, and
 I and J are the subscripts of the array element.
 Lr and Lc: lower bound of row and column (generally 0)

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


2D array in memory
 Example:
 Consider a 20 x 5 two-dimensional array marks
 base address = 1000
 size of an element = 2.
 compute the address of the element, marks[18][ 4] Assuming that
the elements are stored in row major order.

 Solution
 Address(A[I][J]) = Base_Address + w{N (I – 0) + (J – 0)}
 Address(marks[18][4]) = 1000 + 2 {5(18 – 0) + (4 – 0)}
 = 1000 + 2 {5(18) + 4}
 = 1000 + 2 (94)
 = 1000 + 188 = 1188

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


SPARSE MATRICES
 Sparse matrix is a matrix that has large number of elements with a zero
value.
 lower triangular matrix: all elements above the main diagonal have a
zero value.
 Using 1D array which stores only non-zero elements. The mapping can be
done in any one of the following ways:
(a) Row-wise mapping—Here the contents of array A[] will be
{1, 5,3, 2, 7, –1, 3, 1, 4, 2, –9, 2, –8, 1, 7}
(b) Column-wise mapping—Here the contents of array A[] will be
{1, 5, 2, 3, –9, 3, 7, 1, 2, –1, 4, –8, 2, 1, 7}

 upper-triangular matrix tri-diagonal matrix

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


• End of chapter

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Pointer arithmetic

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Operator precedence

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Pointers And two-dimensional Arays
int arr[5][5];
int **ptr

 Individual elements of the array mat can be accessed using either:


arr[i][j]
(*(arr+i))[j]
*((*arr+i))+j)
*(arr[i]+j)

 Therefore,
arr[0][0] = *(arr)[0] = *((*arr)+0) = *(arr[0]+0)
arr[1][2] = (*(arr+1))[2] = *((*(arr+1))+2) = *(arr[1]+2)

 arr[0] + 1 points to arr[0][1]


 arr + 1 points to arr[1][0]
 &arr[0] + 1 points to arr[1][0]
 &arr[0][0] + 1 points to arr[0][1]

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Declaration of arrays
 An array must be declared before being used
 Declaring an array means specifying the following:
 Data type—the kind of values it can store, for example, int, char,
float, double.
 Name—to identify the array.
 Size—the maximum number of values that the array can hold.
 Arrays are declared using the following syntax:
type name[size];
 For example:
int marks[10];
Memory representation of an array of 10 elements

NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”


Declaration of arrays
 Declaring arrays of different data types and sizes

Array of structure:
struct student{
int rollno;
char name[10];
};
struct student st[5];
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Initialization of array

 Initializing the elements during declaration


int Array[3]={1,2,3};
int Array[]={1,2,3};
char name[10] = {'L','e','s','s','o','n','s','\0'}; // can be accessed as string
char name[7] = {'L','e','s','s','o','n','s'}; // can’t access as string
char a[3]={'a','b'}; // null character will be appended automatically
char name[13] = "StudyTonight";
// Set each element of the array to –1
int i, marks[10 ];
for(i= ;i<1 ;i++)
marks[i] = –1;
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”

You might also like