SlideShare a Scribd company logo
ADVANCED DATA STRUCTURES
AND ALGORITHMS
Theory
What is an Array?
• An array is a collection of items of the same variable type that
are stored at contiguous memory locations. It’s one of the most
popular and simple data structures and is often used to
implement other data structures. Each item in an array is
indexed starting with 0.
This makes it easier to calculate the position of each element by simply adding an offset to a base
value, i.e., the memory location of the first element of the array (generally denoted by the name of
the array). The base value is index 0 and the difference between the two indexes is the offset
We can directly access an array
element by using its index value.
• Remember: “Location of next index depends on the data type
we use”.
Basic terminologies of array
•Array Index: In an array, elements are identified by their indexes. Array index starts from 0.
•Array element: Elements are items stored in an array and can be accessed by their index.
•Array Length: The length of an array is determined by the number of elements it can contain
Representation of Array
• The representation of an array can be defined by its declaration.
A declaration means allocating memory for an array of a given
size.
• Arrays can be declared in various ways in different languages.
For better illustration, below are some language-specific array
declarations.
• int arr[5]; // This array will store integer type element
• char arr[10]; // This array will store char type element
• float arr[20]; // This array will store float type element
• However, the above declaration is static or compile-time memory allocation, which
means that the array element’s memory is allocated when a program is compiled. Here
only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be
allocated for storage, but don’t you think it will not be the same situation as we know the
size of the array every time, there might be a case where we don’t know the size of the
array. If we declare a larger size and store a lesser number of elements will result in a
wastage of memory or either be a case where we declare a lesser size then we won’t get
enough memory to store the rest of the elements. In such cases, static memory allocation
is not preferred.
• Is it possible to create dynamic array?
• The answer is Yes. It is possible to allocate memory dynamically. So, dynamic memory
allocation is the process of assigning the memory space during the execution time or the
run time.
• Below are the languages that support dynamic memory allocation
• int *array = new int[5];
Why Array Data Structures is needed?
• Assume there is a class of five students and if we have to keep
records of their marks in examination then, we can do this by
declaring five variables individual and keeping track of records
but what if the number of students becomes very large, it would
be challenging to manipulate and maintain the data.
• What it means is that, we can use normal variables (v1, v2, v3, ..)
when we have a small number of objects. But if we want to store
a large number of instances, it becomes difficult to manage
them with normal variables. The idea of an array is to
represent many instances in one variable..
Need of array
Types of arrays:
• One-dimensional array (1-D arrays): You can imagine a 1d
array as a row, where elements are stored one after another.
Two-dimensional array: 2-D Multidimensional arrays can
be considered as an array of arrays or as a matrix
consisting of rows and columns.
• Three-dimensional array: A 3-D Multidimensional
array contains three dimensions, so it can be considered an
array of two-dimensional arrays.
Types of Array operations:
• Traversal: Traverse through the elements of an array.
• Insertion: Inserting a new element in an array.
• Deletion: Deleting element from the array.
• Searching: Search for an element in the array.
• Sorting: Maintaining the order of elements in the array.
Traverse operation
• For traversing in array of n number of elements, we’ll have to go through each element one by one so number of steps
required to accomplish this will be n.
• If there would have been just 1 element i.e. value of n equals one, we’ll require one step so the time complexity will
be O(1).
• Average or Worst Time Complexity : O(n)
• Best Time Complexity : O(1)
• #include <stdio.h>
• int main() {
• int noOfDrinks[] = {5,2,3,5,7};
•
• printf("Tables Number of orders for drinks :n");
• for(int i = 0; i<5; i++) {
• printf(" %d t %d n", i+1, noOfDrinks[i]);
• }
• }
Advantages of using Arrays:
• Arrays allow random access to elements. This makes accessing
elements by position faster.
• Arrays have better cache locality which makes a pretty big
difference in performance.
• Arrays represent multiple data items of the same type using a
single name.
• Arrays store multiple data of similar types with the same name.
• Array data structures are used to implement the other data
structures like linked lists, stacks, queues, trees, graphs, etc.
Disadvantages of Array:
• As arrays have a fixed size, once the memory is allocated to them, it
cannot be increased or decreased, making it impossible to store extra
data if required. An array of fixed size is referred to as a static array.
• Allocating less memory than required to an array leads to loss of
data.
An array is homogeneous in nature so, a single array cannot store
values of different data types.
• Arrays store data in contiguous memory locations, which makes
deletion and insertion very difficult to implement. This problem is
overcome by implementing linked lists, which allow elements to be
accessed sequentially.
Application of Array:
• They are used in the implementation of other data structures
such as array lists, heaps, hash tables, vectors, and matrices.
• Database records are usually implemented as arrays.
• It is used in lookup tables by computer.
• It is used for different sorting algorithms such as bubble sort
insertion sort, merge sort, and quick sort.
Representation of Arrays
• 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.
• 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:
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
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:
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
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.
• 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(row) = Upper Bound – Lower Bound + 1 = 9 – 1 + 1 = 9
N(Column)= Upper Bound – Lower Bound + 1 = 1 – (-4) + 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 * {[9 * 6 * (5 – 1)] + 6 * [(-1 + 4)]} + [8 – 5]
= 400 + 2 * (9*6*4)+(6*3)+3
= 400 + 2 * (237)
= 874
2. Column Major Order:
To find the address of the element using column-major order, use the following
formula:
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 (row)= Upper Bound – Lower Bound + 1 = 8-1+1 = 8
N (column)= Upper Bound – Lower Bound + 1 = 5 +5 + 1 = 11
Formula used:
Address of[i][j][k] = B + W(M * N(i – x) + M * (j-y) + (k – z))
Solution:
Address of arr[3][3][3] = 400 + 4 * ((8*11*(3-1)+8*(3-(-5)+(3-(-10)))
= 400 + 4 * ((88*2 + 8*8+13)
= 400 + 4 * (253)
= 400 + 1012
= 1412
sparse matrix
A sparse matrix is defined as the matrix of order MxN which has the
number of zero values strictly greater than the number of non-zero
values, distinct from those matrices which contain more non-zero
values than zero values, they are called dense matrices.
These forms are used in order to reduce the space and processing time.
• Assume a matrix of dimensions 100 x 100 containing integer values only and it has 10 non-zero values rest are all zeroes, then it will
take 100 x 100 x 2 = 20000 bytes of memory to store these values.
• Also, Zeros inside the matrices are of no use in the matrices, so a huge memory and processing are wasted on the zero values.
Processing time:
In the 2-d array representation of sparse matrix, In order to access the non-zero values,
one needs to scan through all MxN values, hence it results in irrelevant scanning of
zero elements because the positions of the non-zero elements are not specified.
Assuming the same matrix as in the previous example, traversing all 100 x 100 =
10000 elements requires scanning all of the 10000 elements making it
to O(MxN) complexity.
In order to avoid the above problems and unnecessary computation, one needs to
avoid the unnecessary scanning of the zero elements which appear in the 2-d array
representation.
Therefore, one needs to design those type of data structures that only allows the
storage of non-zero values.
Why do we need to use Sparse Matrix?
Sparse Matrix Representations
QUEUE
• A Queue is an abstract linear data structure serving as a collection of
elements that are inserted (enqueue operation) and removed
(dequeue operation) according to the First in First Out
(FIFO) approach.
• Insertion happens at the rear end of the queue whereas deletion
happens at the front end of the queue. The front of the queue is
returned using the peek operation.
• Applications of Queue data structure :
• Job Scheduling
• Multiprogramming
• Operation on data structures
• Buffer Space
• A Queue is a sequential data type, unlike an array, in an array,
we can access any of its elements using indexing, but we can
only access the element at the front of the queue at a time.
A Queue in data structure can be implemented
using arrays, linked lists, or vectors. For the sake of
simplicity, we will be implementing queue using a one-
dimensional array.
• Working of Queue
• We can use queue to perform its main two operations: Enqueue and Dequeue,
other operations being Peek, isEmpty and isFull.
• Queue operations
• Enqueue
• The Enqueue operation is used to add an element to the front of the queue.
• Steps of the algorithm:
1.Check if the Queue is full.
2.Set the front as 0 for the first element.
3.Increase rear by 1.
4.Add the new element at the rear index.
• Dequeue
• The Dequeue operation is used to remove an element from the rear of the
queue.
• Steps of the algorithm:
1.Check if the Queue is empty.
2.Return the value at the front index.
3.Increase front by 1.
4.Set front and rear as -1 for the last element.
• Peek
• The Peek operation is used to return the front most element of the queue.
• Steps of the algorithm:
1.Check if the Queue is empty.
2.Return the value at the front index.
• isFull
• The isFull operation is used to check if the queue is full or not.
• Steps of the algorithm:
1.Check if the number of elements in the queue (size) is equal to
the capacity, if yes, return True.
2.Return False.
• isEmpty
• The isEmpty operation is used to check if the queue is empty or not.
• Steps of the algorithm:
1.Check if the number of elements in the queue (size) is equal to 0, if yes,
return True.
2.Return False.
Limitations of Queue
• Following are the limitations/disadvantages of a Queue data
structure:
1.A queue is not readily searchable: You might have to maintain
another queue to store the dequeued elements in search of the
wanted element.
2.Traversal possible only once: The front most element needs to be
dequeued to access the element behind it, this happens throughout
the queue while traversing through it. In this process, the queue
becomes empty.
3.Memory Wastage: In a Static Queue, the array's size determines
the queue capacity, the space occupied by the array remains the
same, no matter how many elements are in the queue.
Applications of Queue
• A Queue data structure is used when things don't have to be accessed
immediately but in FIFO (First In First Out) order. This property of the queue data
structure makes queue applicable for the following situations:
1.Job Scheduling The computer schedules the job execution one by one. There
are many jobs like a key press, a mouse click, etc. in the system. The jobs are
brought in the main memory and are assigned to the processor one by one which
is organized using a queue.
2.For eg, Round Robin processor scheduling in queues.
3.Multiprogramming If there are multiple programs in the main memory, then that
state is called multiprogramming. The programs in the main memory are
organized in the form of queues, which are then called "Ready Queues". The
processors will execute the programs by accessing them from the "Cache
Memory" for simultaneous execution.
4.Operation on data structures Certain operations like BFS (Breadth First
Search), and tree traversal uses Queue. The sequence of traversal of inputs is
set using queues.
5.Buffer Space Queues are used in networking, during the transmission of data
from the source machine to the destination.
Conclusion
• A Queue is a sequential data structure that follows the FIFO (First In
First Out) approach.
• A Static stack is implemented using an array, whereas a dynamic
stack is implemented using lists.
• Enqueue and Dequeue are two main operations of a queue, the
others being Peek, isEmpty, isFull.
• Queues are used in BFS (Breadth first search) in tree and graph
traversals.
• There can be a lot of memory wastage in static queues, as no matter
what is the size of the queue, the space occupied by it remains the
same.
• Traversing a queue will delete the foremost element on each
iteration, eventually, emptying the queue.

More Related Content

PPTX
Data Structures - Array presentation .pptx
PPTX
Unit 2 linear data structures
PPTX
data structure unit -1_170434dd7400.pptx
PPTX
polynomial_linked list for electrical engineer
PPTX
ARRAYS.pptx
PPTX
unit 2.pptx
PPTX
Arrays in C.pptx
PDF
cluod.pdf
Data Structures - Array presentation .pptx
Unit 2 linear data structures
data structure unit -1_170434dd7400.pptx
polynomial_linked list for electrical engineer
ARRAYS.pptx
unit 2.pptx
Arrays in C.pptx
cluod.pdf

Similar to ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx (20)

PPTX
Basic of array and data structure, data structure basics, array, address calc...
PPTX
Arrays.pptx
PPTX
DSA Unit II array.pptx
PPTX
PPTX
Array ppt
PDF
Array.pdf
PPTX
Arrays In C++
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
PPTX
Arrays
PPTX
Arrays.pptx
PPT
Presentation of array
PPTX
ARRAY PPT.pptx for mca finals placement
PPTX
Arrays in C
PPTX
Arrays declartion and initialization
PDF
8074.pdf
PPTX
arrayppt.pptx
PPTX
Arrays accessing using for loops
PPTX
TMK_DSA_Unit 2 part1(array and stack).pptx
PPTX
arrays.pptx
PDF
DSA UNIT II ARRAY AND LIST - notes
Basic of array and data structure, data structure basics, array, address calc...
Arrays.pptx
DSA Unit II array.pptx
Array ppt
Array.pdf
Arrays In C++
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Arrays
Arrays.pptx
Presentation of array
ARRAY PPT.pptx for mca finals placement
Arrays in C
Arrays declartion and initialization
8074.pdf
arrayppt.pptx
Arrays accessing using for loops
TMK_DSA_Unit 2 part1(array and stack).pptx
arrays.pptx
DSA UNIT II ARRAY AND LIST - notes
Ad

Recently uploaded (20)

PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Geodesy 1.pptx...............................................
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
436813905-LNG-Process-Overview-Short.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Simulation of electric circuit laws using tinkercad.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Geodesy 1.pptx...............................................
Chapter 6 Design in software Engineeing.ppt
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Arduino robotics embedded978-1-4302-3184-4.pdf
Road Safety tips for School Kids by a k maurya.pptx
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
436813905-LNG-Process-Overview-Short.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Ad

ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx

  • 1. ADVANCED DATA STRUCTURES AND ALGORITHMS Theory
  • 2. What is an Array? • An array is a collection of items of the same variable type that are stored at contiguous memory locations. It’s one of the most popular and simple data structures and is often used to implement other data structures. Each item in an array is indexed starting with 0. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset We can directly access an array element by using its index value.
  • 3. • Remember: “Location of next index depends on the data type we use”. Basic terminologies of array •Array Index: In an array, elements are identified by their indexes. Array index starts from 0. •Array element: Elements are items stored in an array and can be accessed by their index. •Array Length: The length of an array is determined by the number of elements it can contain
  • 4. Representation of Array • The representation of an array can be defined by its declaration. A declaration means allocating memory for an array of a given size. • Arrays can be declared in various ways in different languages. For better illustration, below are some language-specific array declarations. • int arr[5]; // This array will store integer type element • char arr[10]; // This array will store char type element • float arr[20]; // This array will store float type element
  • 5. • However, the above declaration is static or compile-time memory allocation, which means that the array element’s memory is allocated when a program is compiled. Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage, but don’t you think it will not be the same situation as we know the size of the array every time, there might be a case where we don’t know the size of the array. If we declare a larger size and store a lesser number of elements will result in a wastage of memory or either be a case where we declare a lesser size then we won’t get enough memory to store the rest of the elements. In such cases, static memory allocation is not preferred. • Is it possible to create dynamic array? • The answer is Yes. It is possible to allocate memory dynamically. So, dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. • Below are the languages that support dynamic memory allocation • int *array = new int[5];
  • 6. Why Array Data Structures is needed? • Assume there is a class of five students and if we have to keep records of their marks in examination then, we can do this by declaring five variables individual and keeping track of records but what if the number of students becomes very large, it would be challenging to manipulate and maintain the data. • What it means is that, we can use normal variables (v1, v2, v3, ..) when we have a small number of objects. But if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable..
  • 8. Types of arrays: • One-dimensional array (1-D arrays): You can imagine a 1d array as a row, where elements are stored one after another. Two-dimensional array: 2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
  • 9. • Three-dimensional array: A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.
  • 10. Types of Array operations: • Traversal: Traverse through the elements of an array. • Insertion: Inserting a new element in an array. • Deletion: Deleting element from the array. • Searching: Search for an element in the array. • Sorting: Maintaining the order of elements in the array.
  • 11. Traverse operation • For traversing in array of n number of elements, we’ll have to go through each element one by one so number of steps required to accomplish this will be n. • If there would have been just 1 element i.e. value of n equals one, we’ll require one step so the time complexity will be O(1). • Average or Worst Time Complexity : O(n) • Best Time Complexity : O(1) • #include <stdio.h> • int main() { • int noOfDrinks[] = {5,2,3,5,7}; • • printf("Tables Number of orders for drinks :n"); • for(int i = 0; i<5; i++) { • printf(" %d t %d n", i+1, noOfDrinks[i]); • } • }
  • 12. Advantages of using Arrays: • Arrays allow random access to elements. This makes accessing elements by position faster. • Arrays have better cache locality which makes a pretty big difference in performance. • Arrays represent multiple data items of the same type using a single name. • Arrays store multiple data of similar types with the same name. • Array data structures are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc.
  • 13. Disadvantages of Array: • As arrays have a fixed size, once the memory is allocated to them, it cannot be increased or decreased, making it impossible to store extra data if required. An array of fixed size is referred to as a static array. • Allocating less memory than required to an array leads to loss of data. An array is homogeneous in nature so, a single array cannot store values of different data types. • Arrays store data in contiguous memory locations, which makes deletion and insertion very difficult to implement. This problem is overcome by implementing linked lists, which allow elements to be accessed sequentially.
  • 14. Application of Array: • They are used in the implementation of other data structures such as array lists, heaps, hash tables, vectors, and matrices. • Database records are usually implemented as arrays. • It is used in lookup tables by computer. • It is used for different sorting algorithms such as bubble sort insertion sort, merge sort, and quick sort.
  • 15. Representation of Arrays • 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. • 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).
  • 16. Example: 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
  • 17. Calculate the address of any element in the 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.
  • 18. Example: 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
  • 19. 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 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.
  • 20. 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. • To find the address of any element in 3-Dimensional arrays there are the following two ways- • Row Major Order • Column Major Order
  • 21. 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(row) = Upper Bound – Lower Bound + 1 = 9 – 1 + 1 = 9 N(Column)= Upper Bound – Lower Bound + 1 = 1 – (-4) + 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 * {[9 * 6 * (5 – 1)] + 6 * [(-1 + 4)]} + [8 – 5] = 400 + 2 * (9*6*4)+(6*3)+3 = 400 + 2 * (237) = 874
  • 22. 2. Column Major Order: To find the address of the element using column-major order, use the following formula: 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 (row)= Upper Bound – Lower Bound + 1 = 8-1+1 = 8 N (column)= Upper Bound – Lower Bound + 1 = 5 +5 + 1 = 11 Formula used: Address of[i][j][k] = B + W(M * N(i – x) + M * (j-y) + (k – z)) Solution: Address of arr[3][3][3] = 400 + 4 * ((8*11*(3-1)+8*(3-(-5)+(3-(-10))) = 400 + 4 * ((88*2 + 8*8+13) = 400 + 4 * (253) = 400 + 1012 = 1412
  • 23. sparse matrix A sparse matrix is defined as the matrix of order MxN which has the number of zero values strictly greater than the number of non-zero values, distinct from those matrices which contain more non-zero values than zero values, they are called dense matrices.
  • 24. These forms are used in order to reduce the space and processing time. • Assume a matrix of dimensions 100 x 100 containing integer values only and it has 10 non-zero values rest are all zeroes, then it will take 100 x 100 x 2 = 20000 bytes of memory to store these values. • Also, Zeros inside the matrices are of no use in the matrices, so a huge memory and processing are wasted on the zero values. Processing time: In the 2-d array representation of sparse matrix, In order to access the non-zero values, one needs to scan through all MxN values, hence it results in irrelevant scanning of zero elements because the positions of the non-zero elements are not specified. Assuming the same matrix as in the previous example, traversing all 100 x 100 = 10000 elements requires scanning all of the 10000 elements making it to O(MxN) complexity. In order to avoid the above problems and unnecessary computation, one needs to avoid the unnecessary scanning of the zero elements which appear in the 2-d array representation. Therefore, one needs to design those type of data structures that only allows the storage of non-zero values. Why do we need to use Sparse Matrix?
  • 26. QUEUE • A Queue is an abstract linear data structure serving as a collection of elements that are inserted (enqueue operation) and removed (dequeue operation) according to the First in First Out (FIFO) approach. • Insertion happens at the rear end of the queue whereas deletion happens at the front end of the queue. The front of the queue is returned using the peek operation. • Applications of Queue data structure : • Job Scheduling • Multiprogramming • Operation on data structures • Buffer Space
  • 27. • A Queue is a sequential data type, unlike an array, in an array, we can access any of its elements using indexing, but we can only access the element at the front of the queue at a time. A Queue in data structure can be implemented using arrays, linked lists, or vectors. For the sake of simplicity, we will be implementing queue using a one- dimensional array.
  • 28. • Working of Queue • We can use queue to perform its main two operations: Enqueue and Dequeue, other operations being Peek, isEmpty and isFull. • Queue operations • Enqueue • The Enqueue operation is used to add an element to the front of the queue. • Steps of the algorithm: 1.Check if the Queue is full. 2.Set the front as 0 for the first element. 3.Increase rear by 1. 4.Add the new element at the rear index.
  • 29. • Dequeue • The Dequeue operation is used to remove an element from the rear of the queue. • Steps of the algorithm: 1.Check if the Queue is empty. 2.Return the value at the front index. 3.Increase front by 1. 4.Set front and rear as -1 for the last element. • Peek • The Peek operation is used to return the front most element of the queue. • Steps of the algorithm: 1.Check if the Queue is empty. 2.Return the value at the front index.
  • 30. • isFull • The isFull operation is used to check if the queue is full or not. • Steps of the algorithm: 1.Check if the number of elements in the queue (size) is equal to the capacity, if yes, return True. 2.Return False. • isEmpty • The isEmpty operation is used to check if the queue is empty or not. • Steps of the algorithm: 1.Check if the number of elements in the queue (size) is equal to 0, if yes, return True. 2.Return False.
  • 31. Limitations of Queue • Following are the limitations/disadvantages of a Queue data structure: 1.A queue is not readily searchable: You might have to maintain another queue to store the dequeued elements in search of the wanted element. 2.Traversal possible only once: The front most element needs to be dequeued to access the element behind it, this happens throughout the queue while traversing through it. In this process, the queue becomes empty. 3.Memory Wastage: In a Static Queue, the array's size determines the queue capacity, the space occupied by the array remains the same, no matter how many elements are in the queue.
  • 32. Applications of Queue • A Queue data structure is used when things don't have to be accessed immediately but in FIFO (First In First Out) order. This property of the queue data structure makes queue applicable for the following situations: 1.Job Scheduling The computer schedules the job execution one by one. There are many jobs like a key press, a mouse click, etc. in the system. The jobs are brought in the main memory and are assigned to the processor one by one which is organized using a queue. 2.For eg, Round Robin processor scheduling in queues. 3.Multiprogramming If there are multiple programs in the main memory, then that state is called multiprogramming. The programs in the main memory are organized in the form of queues, which are then called "Ready Queues". The processors will execute the programs by accessing them from the "Cache Memory" for simultaneous execution. 4.Operation on data structures Certain operations like BFS (Breadth First Search), and tree traversal uses Queue. The sequence of traversal of inputs is set using queues. 5.Buffer Space Queues are used in networking, during the transmission of data from the source machine to the destination.
  • 33. Conclusion • A Queue is a sequential data structure that follows the FIFO (First In First Out) approach. • A Static stack is implemented using an array, whereas a dynamic stack is implemented using lists. • Enqueue and Dequeue are two main operations of a queue, the others being Peek, isEmpty, isFull. • Queues are used in BFS (Breadth first search) in tree and graph traversals. • There can be a lot of memory wastage in static queues, as no matter what is the size of the queue, the space occupied by it remains the same. • Traversing a queue will delete the foremost element on each iteration, eventually, emptying the queue.