3. Arrays
An array is a data structure consisting of
a collection of elements (values or
variables), each identified by at least one
array index or key. Depending on the
language, array types may overlap (or be
identified with) other data types that
describe aggregates of values, such as
lists and strings.
4. Is the Arrays always of a fixed size?
Arrays at core are of fixed size only,
but most of the languages provide
dynamic sized arrays using the
underlying fixed sized arrays. For
example, vector in C++, ArrayList in
Java and list in Python. In C language,
the array has a fixed size meaning
once the size is given to it, it cannot
be changed i.e. you can’t shrink it nor
can you expand it.
5. Applications of Array Data Structures
Below are some applications of arrays.
● Searching: If data in array is sorted, we can search an item in O(log n) time. We can also find
floor(), ceiling(), kth smallest, kth largest, etc efficiently.
● Matrices: Two-dimensional arrays are used for matrices in computations like graph algorithms
and image processing.
● Implementing other data structures: Arrays are used as the underlying data structure for
implementing stacks and queues.
● Dynamic programming: Dynamic programming algorithms often use arrays to store
intermediate results of subproblems in order to solve a larger problem.
● Data Buffers: Arrays serve as data buffers and queues, temporarily storing incoming data like
network packets, file streams, and database results before processing.
6. Advantages of Array Data Structure
● Efficient and Fast Access: Arrays allow direct and efficient access to any element in the collection
with constant access time, as the data is stored in contiguous memory locations.
● Memory Efficiency: Arrays store elements in contiguous memory, allowing efficient allocation in a
single block and reducing memory fragmentation.
● Versatility: Arrays can be used to store a wide range of data types, including integers,
floating-point numbers, characters, and even complex data structures such as objects and
pointers.
● Compatibility with hardware: The array data structure is compatible with most hardware
architectures, making it a versatile tool for programming in a wide range of environments.
7. Disadvantages of Array Data Structure
● Fixed Size: Arrays have a fixed size set at creation. Expanding an array requires creating a
new one and copying elements, which is time-consuming and memory-intensive.
● Memory Allocation Issues: Allocating large arrays can cause memory exhaustion, leading to
crashes, especially on systems with limited resources.
● Insertion and Deletion Challenges: Adding or removing elements requires shifting subsequent
elements, making these operations inefficient.
● Limited Data Type Support: Arrays support only elements of the same type, limiting their use
with complex data types.
● Lack of Flexibility: Fixed size and limited type support make arrays less adaptable than
structures like linked lists or trees.
8. Linear Search
Searching Array
Binary Search
Binary Search is a searching
algorithm used in a sorted
array. In this algorithm, the
element is found by repeatedly
dividing the search interval in
half and deciding the next
interval to find the element.
Linear Search is defined as a
sequential search algorithm that
starts at one end and goes through
each element of a list until the
desired element or group of
elements is found.
Ternary Search
Ternary search is a divide and
conquer algorithm that can be
used to find an element in an
array.
9. Activity Time!
Input: arr[] = {1, 1, 2, 1, 3, 4, 5, 2, 8};
query[] = [0, 4], [1, 3] [2, 4]
The visualization
What is the output?
11. Stacks
A stack is a linear data structure that follows
the Last In, First Out (LIFO) principle. This
means that the last element added to the
stack is the first one to be removed. It can be
visualized as a pile of plates where you can
only add or remove the top plate.
12. Operations on Stack
Push
Adds an element to the top of
the stack.
Pop
Removes and returns the top
element of the stack.
Peek (or Top)
Returns the top element of
the stack without removing it.
isEmpty
Checks if the stack is empty.
Size
Returns the number of
elements in the stack.
13. Use Case of Stacks
The call stack in
programming languages
keeps track of function
calls and returns.
Function Call Management Expression Evaluation Backtracking
Used in parsing
expressions and evaluating
postfix or prefix notations.
Helps in algorithms that
require exploring all
possibilities, such as maze
solving and depth-first
search.
14. Queues
A queue is a linear data structure that follows the First In,
First Out (FIFO) principle. This means that the first element
added to the queue is the first one to be removed. It can be
visualized as a line of people waiting for a service, where
the first person in line is the first to be served.
15. Operations on Queue
Enqueue
Adds an element to the end
(rear) of the queue.
Dequeue
Removes and returns the front
element of the queue.
Front (or Peek)
Returns the front element of the
queue without removing it.
isEmpty
Checks if the queue is empty.
Size
Returns the number of
elements in the queue.
16. Use Case of Queues
Operating systems use
queues to manage tasks and
processes.
Task Scheduling Breadth-First Search (BFS) Buffering
In graph traversal algorithms,
queues help in exploring
nodes level by level.
Used in situations where data
is transferred asynchronously,
such as IO buffers and print
spooling.