Data Structures and Algorithm
Data Structures and Algorithm
## Objectives
1. **Basic Data Structures**: Provide knowledge of basic data structures and their
implementations.
## Algorithms
1. Narrative
2. Pseudo code
3. Flow charts
2. **Clarity and Unambiguity**: Each step in the algorithm should be clear and
unambiguous.
• Data structure is a storage that is used to store and organize data. It is a way
of arranging data on a computer so that it can be accessed and updated
efficiently.
• Depending on your requirement and project, it is important to choose the
right data structure for your project. For example, if you want to store data
sequentially in the memory, then you can go for the Array data structure.
In linear data structures, the elements are arranged in sequence one after the
other. Since elements are arranged in particular order, they are easy to implement.
However, when the complexity of the program increases, the linear data structures
might not be the best choice because of operational complexities
Examples:
1. Array
2. Stack
3. Queue
4. Linked List
• Unlike linear data structures, elements in non-linear data structures are not in
any sequence. Instead they are arranged in a hierarchical manner where one
element will be connected to one or more elements.
• Non-linear data structures are further divided into graph and tree based data
structures.
Examples:
1. Graph
2. Trees (e.g., Binary Tree, Binary Search Tree, AVL Tree, B-Tree, B+ Tree, Red-
Black Tree)
The data items are arranged in The data items are arranged in non-
sequential order, one after the sequential order (hierarchical
other. manner).
All the items are present on the The data items are present at
single layer. different layers.
Again, divide each subpart recursively into two halves until you get individual
elements.
Dynamic approach:
The memory comes from above the static part of the data segment.
Programs may request memory and may also return previously dynamically
allocated memory. Memory may be returned whenever it is no longer needed.
Memory can be returned in any order without any relation to the order in which it
was allocated. The heap may develop "holes" where previously allocated memory
has been returned between blocks of memory still in use.
A new dynamic request for memory might return a range of addresses out of
one of the holes. But it might not use up all the hole, so further dynamic requests
might be satisfied out of the original hole.
Array Data Structures in Python
An array is a fundamental data structure available in most programming languages,
and it has a wide range of uses across different algorithms.
Python’s array module provides space-efficient storage of basic C-style data types
like bytes, 32-bit integers, floating point numbers, and so on.
Arrays created with the array.array class are mutable and behave similarly to lists,
except for one important difference — they are “typed arrays” constrained to a
single data type.
Because of this constraint, array.array objects with many elements are more space-
efficient than lists and tuples. The elements stored in them are tightly packed, and
this can be useful if we need to store many elements of the same type.
Also, arrays support many of the same methods as regular lists, and we might be
able to use them as a “drop-in replacement” without requiring other changes to our
application code.
Lists
• Lists are used to store data of different data types in a sequential manner.
There are addresses assigned to every element of the list, which is called as
Index.
• The index value starts from 0 and goes on until the last element called
the positive index.
• There is also negative indexing which starts from -1 enabling you to access
elements from the last to first.
Dictionary
• Dictionaries are used to store key-value pairs. To understand better, think of
a phone directory where hundreds and thousands of names and their
corresponding numbers have been added.
• Now the constant values here are Name and the Phone Numbers which are
called as the keys. And the various names and phone numbers are the values
that have been fed to the keys.
• If you access the values of the keys, you will obtain all the names and phone
numbers. So that is what a key-value pair is. And in Python, this structure is
stored using Dictionaries.
Sets
• Sets are a collection of unordered elements that are unique. Meaning that
even if the data is repeated more than one time, it would be entered into the
set only once.
• It resembles the sets that you have learnt in arithmetic. The operations also
are the same as is with the arithmetic sets.