Algorithms and Data
structures
INTRODUCTION
Data structure and algorithms 2
What is data?
Data is a collection of facts from which conclusion
may be drawn.
Types of data:
Textual, numeric, audio, picture, video, etc.
What is data structure? 3
A particular way of storing and organizing data in a computer
so that it can be used efficiently and effectively.
The need for data structures 4
Goal: to organize data.
Criteria: to facilitate efficient processing
storage of data.
retrieval of data.
manipulation of data.
Learning and understanding data structures will help us
select the most suitable data structure for a particular task.
Data structure operations 5
Traversing: accessing each data element exactly once so that certain
items in the data may be processed.
Searching: finding the location of the data element/key in the structure.
Insertion: adding a new data element to the structure .
Deletion: removing a data element from the structure.
Sorting: arrange the data elements in a logical ascending/descending
order.
Merging: combining data elements from two or more data structures into
one
What is an algorithm? 6
A finite set of instructions that accomplish a particular task.
A good algorithm must be:
Correct.
Finite (in terms of time and size).
Unambiguous (Which step is next?)
Space and time efficient.
A computer program is an instance of an algorithm, written in some
specific programming language.
Algorithm development 7
Clearly identify:
what output is required?
what is the input?
What steps are required to transform input into output
A problem can be solved in many different ways
Which solution, amongst the different possible solutions
is the optimal?
Example 8
• Problem: Find maximum of a, b, c.
• Algorithm
• Input: a, b, c
• Output: max
• Process:
1. Let max = a
2. If b > max then max = b
3. If c > max then max = c
4. Display max
How to express an algorithm? 9
A sequence of steps to solve a problem.
We need a way to express this sequence of steps.
Natural language (NL) is an obvious choice, but not a good choice. Why?
NLs are notoriously ambiguous (unclear).
Programming language (PL) is another choice, but again not a good choice. Why?
Algorithm should be PL independent.
We need some balance between PL independence and clarity.
Pseudo-code provides the right balance
What is pseudo-code? 10
Pseudo-code is a shorthand way of describing a computer
program.
Rather than using the specific syntax of a computer language,
more general wording is used.
It is a mixture of NL and PL expressions, in a systematic way.
Using pseudo-code, it is easier for a non-programmer to
understand the general workings of the program.
Pseudo-code: general 11
guidelines
Use PL constructs that are consistent with modern high-level
languages, e.g., C++, Java, ...
Use appropriate comments for clarity.
Be simple and precise.
Example: Determining 12
even/odd number
Input: range
for num=0; num<=range; num=num+1
if num % 2 = 0 then
print num is even
A number divisible by 2 is
considered an even number, while else
a number that is not divisible by 2 print num is odd
is considered an odd number. end if
end for