0% found this document useful (0 votes)
14 views16 pages

Lecture2-Data Structures & Dynamic Arrays

The document provides an overview of data structures, focusing on linear and dynamic arrays. It explains the characteristics, time complexities, and applications of both static and dynamic arrays, highlighting their efficient data management capabilities. Dynamic arrays, which resize automatically, are particularly useful for scenarios requiring frequent appending and fast random access.

Uploaded by

gondalumar001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Lecture2-Data Structures & Dynamic Arrays

The document provides an overview of data structures, focusing on linear and dynamic arrays. It explains the characteristics, time complexities, and applications of both static and dynamic arrays, highlighting their efficient data management capabilities. Dynamic arrays, which resize automatically, are particularly useful for scenarios requiring frequent appending and fast random access.

Uploaded by

gondalumar001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Structures &

Dynamic Arrays
Course instructor: Maheen Zulfiqar
What Are Data Structures?
• Definition:
A data structure is a way of organizing and storing data so it can be
accessed and modified efficiently.
• Why they matter:
• Enable efficient data management
• Crucial for performance in real-world software
• Examples:
• Arrays, Linked Lists
• Stacks, Queues
• Trees, Graphs, Hash Tables
•Linear Data Structures
AGENDA •Arrays
•Linked Lists
•Stacks
•Classification •Queues
•Linear: Data elements are arranged in •Non-Linear Data Structures
sequence (e.g., Array, Stack) •Trees
•Non-Linear: Hierarchical or networked •Graphs
structure (e.g., Tree, Graph) •Hashing and Hash Tables
•Static: Fixed size (e.g., Array)
•Dynamic: Resizable during runtime
(e.g., Linked List)
Linear Data Structures (Arrays)
1. Arrays
An array is a basic data structure used to store a fixed-
size collection of elements of the same type. These
elements are arranged in contiguous memory locations,
allowing each element to be indexed or accessed directly
using an integer index.
Cont.
• Characteristics of Arrays in Data Structures
• Fixed Size: Once an array is declared, its size cannot be changed. You need to know the number of
elements you will store in the array beforehand.

• Homogeneous Elements: All elements in an array must be of the same data type, such as all
integers, all floats, or all characters.

• Indexed by Integers: Each element in an array is assigned a unique integer called an index, which
identifies its position within the array. Indexing usually starts at 0.

• Efficient Access: Because of the way arrays are stored in memory (contiguously), accessing any
element by its index is very efficient. This direct access using the index is often referred to as
"random access."
Cont.
Time Complexity:
• Insertion:
• O(n) in the worst case (inserting at the beginning) and O(1) in the best case
(inserting at the end). This is because shifting elements to make space for
the new element can take linear time.
• Deletion:
• O(n) in the worst case (deleting from the beginning) and O(1) in the best
case (deleting from the end). Shifting elements to fill the gap created by the
deletion can take linear time.
Cont.
• Searching:
• O(n) in the worst case (searching for an element that is not present or at the end). This is
because every element in the array might need to be checked.
• Accessing:
• O(1) (constant time). You can access any element directly using its index.
• Reading:
• O(1) (constant time). Reading an element by its index is a quick operation.
Space Complexity:
• Overall: O(n). The space required to store an array grows proportionally to the number of
elements.
Introducing Dynamic Arrays
Definition:
A dynamic array is an array that resizes automatically when its capacity is exceeded.
Examples:
•Python list
•C++ vector
•Java ArrayList
Key Feature:
Automatic resizing (usually doubles in size)
• How Dynamic Arrays Work
• Steps when inserting:
• Check if capacity is full
• Allocate new array (usually double the size)
• Copy elements to new array
• Add the new element
Key Features of Dynamic Array

• Add Element (append): Add element at the end if the array size is not
enough then extend the size of the array and add an element at the
end of the original array as well as given index. Doing all that copying
takes O(n) time, where n is the number of elements in our array.
That's an expensive cost for an append. In a fixed-length array,
appends only take O(1) time. But appends take O(n) time only when
we insert into a full array. And that is pretty rare, especially if we
double the size of the array every time we run out of space. So in
most cases appending is still O(1) time, and sometimes it's O(n) time.
In dynamic array you can create fixed-size array when required added
some more element in array then use this approach:
Cont.
Cont.
• Delete Element: Delete an element from array, default remove()
method delete an element from end, simply store zero at last index
and you also delete element at specific index by calling removeAt(i)
method where i is index. removeAt(i) method shift all right element in
the left side from the given index.
Cont.
• Resize of Array Size: When the array has null/zero data (aside from an
element added by you) at the right side of the array, meaning it has
unused memory, the method shrinkSize() can free up the extra
memory. When all space is consumed, and an additional element is to
be added, then the underlying fixed-size array needs to increase in
size. Typically resizing is expensive because you have to allocate a
bigger array and copy over all of the elements from the array you
have outgrown before we can finally append our item. Use growsize()
function for this purpose.
Cont.
• Shrinksize() and growsize()
Time Complexities of Dynamic
Arrays

Operation Time Complexity

Access O(1)

Append O(1) amortized

Insert/Delete at end O(1) amortized

Insert/Delete at middle/start O(n)

Note: Amortized O(1) means resizing cost is spread over


multiple operations
When to Use Dynamic Arrays?
• Use cases:
• Frequent appending to the end
• Unpredictable size at runtime
• Fast random access required
• Avoid if:
• Many insertions/deletions at the start/middle
• Strict memory constraints
Real-world Applications

•Dynamic arrays power many standard library containers:


•Python: list
•C++: std::vector
•Java: ArrayList
•Used in:
•Game development (entity lists)
•Text editors (buffers)
•Spreadsheet apps

You might also like