0% found this document useful (0 votes)
6 views

Data Structure Unit-1

The document provides an overview of arrays in data structures, explaining their definition, types (one-dimensional and multi-dimensional), declaration, initialization, and operations such as traversal, insertion, deletion, searching, and sorting. It discusses the advantages and disadvantages of arrays, as well as the calculation of addresses for elements in one-dimensional and two-dimensional arrays using row-major and column-major orders. The document includes code examples for various operations on arrays, demonstrating their practical application.

Uploaded by

aniruddh2573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Structure Unit-1

The document provides an overview of arrays in data structures, explaining their definition, types (one-dimensional and multi-dimensional), declaration, initialization, and operations such as traversal, insertion, deletion, searching, and sorting. It discusses the advantages and disadvantages of arrays, as well as the calculation of addresses for elements in one-dimensional and two-dimensional arrays using row-major and column-major orders. The document includes code examples for various operations on arrays, demonstrating their practical application.

Uploaded by

aniruddh2573
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-1

ARRAYS

Arrays in Data Structure


What Are Arrays in Data Structures?

An array is a linear data structure that collects elements of the same data type
and stores them in contiguous and adjacent memory locations. Arrays work on
an index system starting from 0 to (n-1), where n is the size of the array.
It is an array, but there is a reason that arrays came into the picture.

Why Do You Need an Array in Data Structures?

Let's suppose a class consists of ten students, and the class has to publish their
results. If you had declared all ten variables individually, it would be
challenging to manipulate and maintain the data.

If more students were to join, it would become more difficult to declare all the
variables and keep track of it. To overcome this problem, arrays came into the
picture.

What Are the Types of Arrays?


There are majorly two types of arrays, they are:
One-Dimensional Arrays:

You can imagine a 1d array as a row, where elements are stored one after
another.
Multi-Dimensional Arrays:
These multi-dimensional arrays are again of two types. They are:
Two-Dimensional Arrays:

You can imagine it like a table where each cell contains elements.

Three-Dimensional Arrays:

You can imagine it like a cuboid made up of smaller cuboids where each cuboid
can contain an element.

How Do You Declare an Array?

Arrays are typically defined with square brackets with the size of the arrays as
its argument.

Here is the syntax for arrays:


1D Arrays: int arr[n];
2D Arrays: int arr[m][n];
3D Arrays: int arr[m][n][o];
How Do You Initialize an Array?
You can initialize an array in four different ways:
• Method 1:
int a[6] = {2, 3, 5, 7, 11, 13};
• Method 2:
int arr[]= {2, 3, 5, 7, 11};
• Method 3:
int n;
scanf(“%d”,&n);
int arr[n];
for(int i=0;i<5;i++)
{
scanf(“%d”,&arr[i]);
}
• Method 4:
int arr[5];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;

How Can You Access Elements of Arrays in Data Structures?

You can access elements with the help of the index at which you stored them.
Let's discuss it with a code:
#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
printf(“%d\n”,a[0]); // we are accessing
printf(“%d\n”,a[1]);
printf(“%d\n”,a[2]);
printf(“%d\n”,a[3]);
printf(“%d”,a[4]);
return 0;
}
What Operations Can You Perform on an Array?
• Traversal
• Insertion
• Deletion
• Searching
• Sorting

Traversing the Array:

Traversal in an array is a process of visiting each element once.


Code:

#include<stdio.h>
int main()
{
int a[5] = {2, 3, 5, 7, 11};
for(int i=0;i<5;i++)
{
//traversing ith element in the array
printf(“%d\n”,a[i]);
}
return 0;
}
Insertion:

Insertion in an array is the process of including one or more elements in an


array.

Insertion of an element can be done:


• At the beginning
• At the end and
• At any given index of an array.

At the Beginning:
Code:
#include<stdio.h>
int main()
{
int array[10], n,i, item;
printf("Enter the size of array: ");
scanf("%d", &n);
printf("\nEnter Elements in array: ");
for(i=0;i<n;i++)
{
scanf("%d", &array[i]);
}
printf("\n enter the element at the beginning");
scanf("%d", &item);
n++;
for(i=n; i>1; i--)
{
array[i-1]=array[i-2];
}
array[0]=item;
printf("resultant array element");
for(i=0;i<n;i++)
{
printf("\n%d", array[i]);
}
getch();
return 0;
}
At the End:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int array[10], i, values;
printf("Enter 5 Array Elements: ");
for(i=0; i<5; i++)
scanf("%d", &array[i]);
printf("\nEnter Element to Insert: ");
scanf("%d", &values);
array[i] = values;
printf("\nThe New Array is:\n");
for(i=0; i<6; i++)
printf("%d ", array[i]);
getch();
return 0;
}
At a Specific Position:
Code:
#include <stdio.h>
int main()
{
int array[100], pos, size, val;
printf("Enter size of the array:");
scanf("%d", &size);
printf("\nEnter %d elements\n", size);
for (int i = 0; i < size; i++)
scanf("%d", &array[i]);
printf("Enter the insertion location\n");
scanf("%d", &pos);
printf("Enter the value to insert\n");
scanf("%d", &val);
for (int i = size - 1; i >= pos - 1; i--)
array[i+1] = array[i];
array[pos-1] = val;
printf("Resultant array is\n");
for (int i = 0; i <= size; i++)
printf("%d\n", array[i]);
return 0;
}
Deletion:

Deletion of an element is the process of removing the desired element and re-
organizing it.
You can also do deletion in different ways:
• At the beginning
• At the end

At the Beginning:
#include<stdio.h>
int main()
{
int n,array[10];
printf("enter the size of an array");
scanf("%d" ,&n);
printf("enter elements in an array");
for(int i=0;i<n;i++)
scanf("%d", &array[i]);
n--;
for(int i=0;i<n;i++)
array[i]=array[i+1];
printf("\nafter deletion ");
for(int i=0;i<n;i++)
printf("\n%d" , array[i]);
}
At the End:
#include<stdio.h>
int main()
{
int n,array[10];
printf("enter the size of an array");
scanf("%d" ,&n);
printf("enter elements in an array");
for(int i=0;i<n;i++)
scanf("%d", &array[i]);
printf("\nafter deletion array elements are");
for(int i=0;i<n-1;i++)
printf("\n%d" , array[i]);
}

Searching for an Element

The method of searching for a specific value in an array is known as searching.

There are two ways we can search in an array, they are:


• Linear search
• Binary search
Linear Search:
Code:
#include <stdio.h>
int linear(int a[], int n, int x)
{
for (int i = 0; i < n; i++)
if (a[i] == x)
return i;
return -1;
}
int main(void)
{
int a[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(a) / sizeof(a[0]);
// Function call
int result = linear(a, n, x);
if(result == -1)
{
printf("Element is not present in array");
}
else
{
printf("Element found at index: %d", result);
}
return 0;
}

Binary Search:
#include <stdio.h>
int binary(int a[], int lt, int rt, int k)
{
if (rt >= lt) {
int mid = lt + (rt - l) / 2;
// check If element is at the middle
if (a[mid] == k)
return mid;
//check if element is at the left side of mid
if (a[mid] > x)
return binary(a, lt, mid - 1, k);
// Else element is at the right side of mid
return binary(a, mid + 1, rt, k);
}
// if element not found
return -1;
}
int main(void)
{
int a[] = { 2, 3, 5, 7, 11 };
int n = sizeof(a) / sizeof(a[0]);
int k = 11;
int res = binary(arr, 0, n - 1, k);
if(res == -1)
{
printf("Element is not found")
}
else
{
printf("Element found at index %d",res);
}
return 0;
}

Sorting:

Sorting in an array is the process in which it sorts elements in a user-defined


order.
Code:
#include <stdio.h>
void main()
{
int temp, size, array[100];
printf("Enter the size \n");
scanf("%d", &size);
printf("Enter the numbers \n");
for (int i = 0; i < size; ++i)
scanf("%d", &array[i]);
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size; ++j)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("sorted array:\n");
for (int i = 0; i < size; ++i)
printf("%d\n", array[i]);
}

What Are the Advantages of Arrays in Data Structures?


• Arrays store multiple elements of the same type with the same name.
• You can randomly access elements in the array using an index number.
• Array memory is predefined, so there is no extra memory loss.
• Arrays avoid memory overflow.
• 2D arrays can efficiently represent the tabular data.

What Are the Disadvantages of Arrays in Data Structures?


• The number of elements in an array should be predefined
• An array is static. It cannot alter its size after declaration.
• Insertion and deletion operation in an array is quite tricky as the array
stores elements in continuous form.
• Allocating excess memory than required may lead to memory wastage.
Calculation of address of element of 1-D, 2-D, and 3-D using row-major
and column-major order

Calculating the address of any element In the 1-D array:


A 1-dimensional array (or single-dimension array) is a type of linear array.
Accessing its elements involves a single subscript that can either represent a
row or column index.

Example:

2-D array
To find the address of an element in an array the following formula is used-
Address of A[I] = B + W * (I – LB)
I = Subset of element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LB = Lower Limit/Lower Bound of subscript(If not specified assume zero).

Example: Given the base address of an array A[1300 …………


1900] as 1020 and the size of each element is 2 bytes in the memory, find the
address of A[1700].

Solution:
Given:
Base address B = 1020
Lower Limit/Lower Bound of subscript LB = 1300
Storage size of one element store in any array W = 2 Byte
Subset of element whose address to be found I = 1700
Formula used:
Address of A[I] = B + W * (I – LB)
Solution:
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820

Calculate the address of any element in the 2-D array:


The 2-dimensional array can be defined as an array of arrays. The 2-
Dimensional arrays are organized as matrices which can be represented as the
collection of rows and columns as array[M][N] where M is the number of rows
and N is the number of columns.
Example:

2-D array
To find the address of any element in a 2-Dimensional array there are the
following two ways-
1. Row Major Order
2. Column Major Order

1. Row Major Order:


Row major ordering assigns successive elements, moving across the rows and
then down the next row, to successive memory locations. In simple language,
the elements of an array are stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following
formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of the matrix(If not given
assume it as zero),
N = Number of column given in the matrix.

Example: Given an array, arr[1………10][1………15] with base


value 100 and the size of each element is 1 Byte in memory. Find the address
of arr[8][6] with the help of row-major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Address of A[I][J] = 210

2. Column Major Order:


If elements of an array are stored in a column-major fashion means moving
across the column and then to the next column then it’s in column-major
order. To find the address of the element using column-major order use the
following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as
zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it
as zero),
M = Number of rows given in the matrix.

Example: Given an array arr[1………10][1………15] with a base value


of 100 and the size of each element is 1 Byte in memory find the address of
arr[8][6] with the help of column-major order.

Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
= 10 – 1 + 1
= 10
Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157

From the above examples, it can be observed that for the same position two
different address locations are obtained that’s because in row-major order
movement is done across the rows and then down to the next row, and in
column-major order, first move down to the first column and then next column.
So both the answers are right.
So it’s all based on the position of the element whose address is to be found for
some cases the same answers is also obtained with row-major order and
column-major order and for some cases, different answers are obtained.

Calculate the address of any element in the 3-D Array:


A 3-Dimensional array is a collection of 2-Dimensional arrays. It is specified
by using three subscripts:
1. Block size
2. Row size
3. Column size
More dimensions in an array mean more data can be stored in that array.
Example:

3-D array
To find the address of any element in 3-Dimensional arrays there are the
following two ways-
• Row Major Order
• Column Major Order

1. Row Major Order:


To find the address of the element using row-major order, use the following
formula:
Address of A[i][j][k] = B + W *(M * N(i-x) + N *(j-y) + (k-z))
Here:
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
M = Row (total number of rows)
N = Column (total number of columns)
P = Width (total number of cells depth-wise)
x = Lower Bound of Row
y = Lower Bound of Column
z = Lower Bound of Width

Example: Given an array, arr[1:9, -4:1, 5:10] with a base value of 400 and the
size of each element is 2 Bytes in memory find the address of element arr[5][-
1][8] with the help of row-major order?

Solution:
Given:
Row Subset of an element whose address to be found I = 5
Column Subset of an element whose address to be found J = -1
Block Subset of an element whose address to be found K = 8
Base address B = 400
Storage size of one element store in any array(in Byte) W = 2
Lower Limit of row/start row index of matrix x = 1
Lower Limit of column/start column index of matrix y = -4
Lower Limit of blocks in matrix z = 5
M = Upper Bound – Lower Bound + 1 = 1 – (-4) + 1 = 6
N = Upper Bound – Lower Bound + 1 = 10 – 5 + 1 = 6
Formula used:
Address of[I][J][K] =B + W (M * N(i-x) + N *(j-y) + (k-z))
Solution:
Address of arr[5][-1][8] = 400 + 2 * {[6 * 6 * (5 – 1)] + 6 * [(-1 + 4)]} + [8 –
5]
= 400 + 2 * ((4 * 6 + 3) * 6 + 3)
= 400 + 2 * (165)
= 730
2. Column Major Order:
To find the address of the element using column-major order, use the following
formula:1
Address of A[i][j][k]= B + W(M * N(i – x) + M *(k – z) + (j – y))
Here:
B = Base Address (start address)
W = Weight (storage size of one element stored in the array)
M = Row (total number of rows)
N = Column (total number of columns)
P = Width (total number of cells depth-wise)
x = Lower Bound of Row
y = Lower Bound of Column
z = Lower Bound of Width

Example: Given an array arr[1:8, -5:5, -10:5] with a base value of 400 and the
size of each element is 4 Bytes in memory find the address of
element arr[3][3][3] with the help of column-major order?

Solution:
Given:
Row Subset of an element whose address to be found I = 3
Column Subset of an element whose address to be found J = 3
Block Subset of an element whose address to be found K = 3
Base address B = 400
Storage size of one element store in any array(in Byte) W = 4
Lower Limit of row/start row index of matrix x = 1
Lower Limit of column/start column index of matrix y = -5
Lower Limit of blocks in matrix z = -10
M = Upper Bound – Lower Bound + 1 = 5 + 5 + 1 = 11
N = Upper Bound – Lower Bound + 1 = 5 + 10 + 1 = 16
Formula used:
Address of[i][j][k] = B + W(M * N(i – x) + M * (k – z) + (j – y))
Solution:
Address of arr[3][3][3] = 400 + 4 * {[(3 – 1)] * 16 + [3 + 10] ]} * 11 + [3 +
5]
= 400 + 4 * ((32 + 13) * 11 + 8)
= 400 + 4 * (503)
= 400 + 2012
= 2412

You might also like