1 Introduction
1 Introduction
Introduction
1
Data Structure and Algorithms
● What is data?
● Data is A collection of facts from which conclusion may
be drawn. E.g. Data: Temperature 47°C; Conclusion: It is
hot.
● Types of data:
● Textual: For example, your name (Norah).
● Numeric: For example, your ID (090254).
● Audio: For example, your voice.
● Video: For example, your voice and picture.
2
What is Data Structure?
● A particular way of storing and organizing data in a
computer so that it can be used efficiently and effectively.
● Data structure is the logical or mathematical model of a
particular organization of data.
● A group of data elements grouped together under one
name.
● For example, an array of integers.
3
What is Data Structure?
There are , but we named a few.
4
The Need for Data Structures
● Goal: to organize data.
● Criteria: to facilitate efficient processing
● storage of data.
● retrieval of data.
● manipulation of data.
● Design Issue:
● select and design appropriate data types
(This is the main motivation to learn and understand data 5
Data Structure Operations
● 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 order (ascending/descending))
● Merging (Combining data elements from two or more data structures into one)
6
What is algorithm?
● A finite set of instructions which accomplish a particular
task.
● A method or process to solve a problem.
● Transforms input of a problem to output.
● Algorithm = Input + Process + Output.
7
What is a good algorithm?
● It must be correct.
● It must be finite (in terms of time and size).
● It must terminate.
● It must be unambiguous (Which step is next?)
● It must be space and time efficient
9
Algorithm development:
• Problem: Find maximum of a, b, c
Basics
• Algorithm
– Input = a, b, c
– Output = max
– Process
o Let max = a
o If b > max then
– max = b
o If c > max then
– max = c
o Display max.
10
In-Class Exercise
● Problem: Find the equivalent grade letter of a given percent grade.
– E.g. 90-100 A
11
How to express an algorithm?
● 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
12
What is pseudo-code?
● Pseudo-code is a short hand 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.
13
Pseudo-code: general guidelines
● Use PLs construct that are consistent with modern high level
languages, e.g. C++, Java, ...
● Use appropriate comments for clarity.
● Be simple and precise.
14
Algorithm Design: Practice
● Example 1: Determining even/odd number
– A number divisible by 2 is considered an even number, while a
number which is not divisible by 2 is considered an odd number.
Write pseudo-code to display first N odd/even numbers.
15
Even/ Odd Numbers
Input range
for num←0; num<=range; num←num+1
do
if num % 2 = 0 then
print num is even
else
print num is odd
endif
endfor
16