Intro to Data structures
and algorithms
RP: Imran Latif
[email protected]
Data structures
What is Data Structure?
“A systematic way of storing and organizing data”
A data structure is a specialized format for organizing,
processing, retrieving and storing data. or
“The organized collection of data is called data structure”
Array is a simple example
Data structures
A 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.
There are different basic and advanced types of data structures
that are used in almost every program or software system that has
been developed. E.g., stack, queue, linked list, binary tree, etc.
Why Data structures?
Data Structures are necessary for designing efficient
algorithms. It provides reusability and abstraction.
Using appropriate data structures can help programmers
save a good amount of time while performing operations
such as storage, retrieval, or processing of data.
Manipulation of large amounts of data is easier.
Operations on Data Structure
Important operations usually performed on different data structures
• Inserting: Adding a new record to the structure.
• Deleting: Removing a record from the structure.
• Traversing: Accessing each record exactly once so that certain items in the
record may be processed.
• Searching: Finding the location of the record with a given key value.
• Sorting: Arranging the records in some logical order.
• Merging: Combing the records in two different sorted files into a single
sorted file.
Different data structures may perform each of these operations with different
efficiency
Classification of Data Structure
Types of Data Structure
• Linear Data Structure: The data elements are linked to
each other in a linear manner. The data elements exists in
a certain sequence one after another. Each of them has a
successor and/or predecessor.
• Example, Arrays, Stack, Queues etc.
• Non-Linear Data Structure: The data structure in which the
data elements are not arranged in a sequence. Every data
element may be attached to many other elements.
• Example Tree, Graph
Linear Data Structure
Static data structure: Static data structure has a fixed
memory size. It is easier to access the elements in a static data
structure.
An example of this data structure is an array.
Dynamic data structure: In dynamic data structure, the size
is not fixed. It can be randomly updated during the runtime
which may be considered efficient concerning the memory
(space) complexity of the code.
Examples of this data structure are queue, stack, etc.
Physical vs Logical Data Structure
Physical Data Structure Logical Data Structure
Any data structure for which memory Those data structure for which there is
is allocated e.g., array, linked list no memory allocated directly (like
stack, queues etc.) but by using
It decides or defines how the memory
physical data structure(array, linked-
is organized or allocated...
list) we can allocate the memory...
We call array and linked list as the Those data structures which is
physical data structure because these
implemented by using physical data
two data structures decide or define
structures are called Logical data
how the memory is organized or how
structures...
the memory is allocated.
Importance of Data Structures
Data structures bring together the data elements in a logical way
and facilitate the effective use, persistence and sharing of data.
They provide a formal model that describes the way the data
elements are organized.
Data structures are the building blocks for more sophisticated
applications. They are designed by composing data elements into
a logical unit representing an abstract data type that has relevance
to the algorithm or application.
Abstract data types
When an application requires a special kind of data which
is not available as a built-in data type
In this type we specify how to store a value for the data,
what are the operations that can meaningfully manipulate
variables of that kind of data, amount of memory required
to store a value
Programmer’s responsibility e.g., dd/mm/yy
Also called user-defined data type
Build using struct/class in C/C++/Java
Abstract data types
Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of values and a set of operations.
The definition of ADT only mentions what operations are to be
performed but not how these operations will be implemented. It
does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations.
It is called “abstract” because it gives an implementation-
independent view.
The process of providing only the essentials and hiding the
details is known as abstraction.
Abstract data types
Algorithms
• Algorithm: Sequence of steps designed to perform a particular
task.
• Computing problems
– All can be solved by executing a series of actions
(instructions) in a specific order
• Algorithm: procedure in terms of
1. Actions to be executed
2. The order in which these actions are to be executed
Pseudocode
Artificial,
informal language that helps us develop
algorithms
Similar to everyday English
Not actually executed on computers
Helps us “think out” a program before writing it
Easy to convert into a corresponding C program
No need to worry about language syntax rules
Consists only of executable statements
Example Algorithm
It is a well-defined set of instructions used to solve a particular problem.
Example:
Write an algorithm for finding the location of the largest element of an array Data.
Pseudo code:
Assume that first element is maximum. Compare the maximum number with each
element of the list. If an element greater than maximum number is found, now
assume that element as maximum. Do this for each element of the list, until all
elements are checked. Current maximum will be actual maximum number in the
list.
Algorithm:
Largest-Item (Data, N, Loc)
1. set k:=1, Loc:=1 and Max:=Data[1]
2. while k<=N repeat steps 3, 4
3. If Max < Data[k] then Set Loc:=k and Max:=Data[k]
4. Set k:=k+1
5. write: Max and Loc
6. exit
Flowcharts
Graphical representation of an algorithm
Drawn using certain special-purpose symbols connected
by arrows called flow lines
Oval symbol:
Indicates the beginning or end of a program or a
section of code
Rectangle symbol (action symbol):
Indicates any type of action
Diamond symbol:
indicates a decision
Example Flowchart
K 1
Loc 1
MAX DATA[1]
Write
Is K > N? Loc, MAX
Is MAX
<DATA[K] STOP
MAX = DATA[K]
Loc = K
Binary search: To find a key from an array A in ascending
order
found = false; low = 0; high = N – 1;
while (( ! found) && ( low <= high))
{
mid = (low + high)/2;
if (A[mid] = = key)
found = true;
else if (A[mid] > key)
high = mid – 1;
else
low = mid + 1;
}