Algorithms and Data Structure: Please Turnoff Your Cell Phone
Algorithms and Data Structure: Please Turnoff Your Cell Phone
Lecture 1
I take it because I
am interested
Concepts to be covered
The course is divided roughly into 4 parts
1st Review of basic programming
And a Quiz on the topic
2nd Searching and Sorting
Different techniques of searching and sorting
3rd Memory manipulation Stacks, Queues and Linked List
Pointers and memory address space
Dynamic memory allocation
Abstract data types
4th Trees, Graph and Heap
Uses of trees in Computer Science, Heap as priority queue and
shortest path finding algorithms using graph.
Course Outline
Detailed Course Outline is on the CSC-112
homepage
http://groups.yahoo.com/group/BTE-5A
Check your email often for announcements
related to the course
Resources
Recommended Book:
C++ Plus Data Structures by Nell Dale 5th Edition
Resources
Reference Books:
Data Structure & Problem Design in C++, Robert Kruse /
C.L.Tondo, Bruce Leung
Data structures through C in depth by S.K. Srivastava and
Deepali Srivastava
Data Structure through “C” Language by Sameeran
Chattopadhyay, Matangini Chottopadhyay, BPB Publications
Data Structure Using C and C++ by Yedidyah Langsam,
Aaron.Tanenbaum Moshe J.Augenstein
Mastering Algorithms with C by Kyle Loudon
Data Structure and Algorithm Analysis in C by Mark Allen
Weises
Data Structures Demystified by Jim Keogh and Ken
Davidson
Five Tips to Success
Work hard
Try more exercises and more
practice
Do the labs and assignments
yourself
Be patient with the machine
If you really need that, do it quietly
...
No make-ups
Quizzes
Unannounced
No make-ups
Tutorials/Lab Sessions
yes
No
Boss assigns task
Great thinkers
will always be needed.
Study:
Many experienced programmers were
asked to code up binary search.
Problem Algorithm:
Algorithm A sequence
of instructions describing
how to do a task
Computer Program
Algorithm Representations
There are two ways to
represent an Algorithm.
They are:
1-Pseudo Code
2-Flow Chart
Algorithm
2X2=5
Example
procedure Do_Thursday procedure Do_Week
{ {
Wake_up ;
Take_A_Shower ; Do_Monday ;
Eat_Breakfast ; Do_Tuesday ;
Drive_To_university ; Do_Thursday ;
Attend_ALGO_Lecture ; ...etc...etc...etc...
...etc...etc...etc... }
Drive_From_university ;
...etc...etc...etc...
}
PSEUDOCODE Rules
There is currently no standard pseudocode.
There are however certain conventions:
Statements are written in simple English;
Each instruction is written on a separate
line;
Logic-showing Keywords are written in
UPPER CASE
(e.g. IF , THEN, FOR, WHILE )
Each set of instructions is written from top to
bottom with only one entry and one exit;
Groups of statements may be formed into
modules, and that group given a name.
PSEUDOCODE
Pseudo code basically consists of keywords and English-
like phrases that are intended to indicate flow of
control. The following conventions are usually used.
1. The symbol ► is used to indicate that the reminder
of a line should be treated as a comment. If more
than one statement appears on a single line, a
semicolon will be used to separate them.
2. ASSIGNMENT STATEMENTS have the form x ← e,
which assigns the value of expression e to variable
x. Multiple assignments can be performed in one
statement; for example, x ← y ← e assigns the
value of e to variables x and y.
PSEUDOCODE
Another way do this as follows:
x := e
where x is a variable and e is an expression.
When an assignment statement is executed, the expression e
is evaluated (using the current values of all variables in the
expression), and then its value is placed in the memory
location corresponding to x (replacing any previous
contents of this location) .
Transposing Two Values – Example
Example: Transposing Two Values.
Find an algorithm that takes two values x and y as inputs.
The input values are, then, interchanged to obtain the
output.
Method: If we try
x := y
y :=x
whereas x=x+y
is simply false in this case. By algebra, of course, it is only true
when y = 0.
The Oval.
Start END
How to represent using Flow Chart? (Contd.)
A PARALLELOGRAM
READ PRINT
How to represent using Flow Chart?
A RECTANGLE.
S=a+b
yes
condition
No
Module B
Module C
Selection Logic (Conditional Flow)
Selection logic employs a number of conditions which lead to a
selection of one out of several alternative modules. The
structures which implement this logic are called selection
structures.
These selection structures fall into three types.
Single selection ( If structure )
Double selection ( If/else structure)
Multiple selection ( Switch structure )
Selection Logic (Conditional Flow)
Single selection:
The logic of this structure is, If the condition holds, then
Module A , which may consist of one or more statements is
executed ; otherwise Module A is skipped and control
transfers to the next step of the algorithm.
Selection Logic (Conditional Flow)
Single Selection:
No
Condition ?
Yes
Module A
Selection Logic (Conditional Flow)
Double selection:
No
Condition ?
Yes
Module A Module B
PSEUDOCODE – Execution of if-
then-else
Multiple selection :
T
Break
F
T
Break
F
..
.
T
Break
F
Iterative Logic (Repetitive Flow)
The third kind of logic refers to either of two types of structures
involving loops. Each type begins with a repeat statement
and is followed by a module ,called the body of the loop.
1- While structure
2- For Structure
Iterative Logic (Repetitive Flow)
While Structure :
No
Condition ?
Yes
Module
(body of loop)
Iterative Logic (Repetitive Flow)
For Structure :
KR
NO
Is K > S ?
YES
Module
(body of loop)
KK+T
Example – Add two numbers, calculate &
print the sum.
A Flow Chart
Start
READ a, b
S := a + b
Print S
End
Example-Find location and Max value
of Data (Using If structure )
K1,Loc 1
Max Data[1]
K K+1
Yes
Is K > N Write: LOC, MAX
No
Stop
No
Is Max < Data[K]
Yes
LOC K
MAX DATA [K]
Write an algorithm to find out the largest number
from a given set of three numbers
1 Start
2 Input Three Numbers A,B & c
3 If A > B then
If A > C then [ nested if structure]
Print “ A is greater “
Else
Print “C is greater “
End If
Else
If B > C
Print “ B is greater “
Else
Print “ C is greater “
End if
End if
4. Exit
of the digits of an integer entered by
the user.
Step 1.[Initialize] Sum:=0
Step 2.Input the number N
Step 3.While ( N!= 0)
Sum:=Sum + N mod 10
N:= N div 10
End While
Step 4. Write Sum
Flow Chart
Start
Read N
sum := 0
No
N != 0
Yes
N = N div 10
End
Comparing of two numbers (using If
Structure)
Step 1. Input two Numbers num1 , num2
Step 2. If ( num1 == num2 )
Write “ num1 is equal to num2”
Step 3. If ( num1 != num2 )
Write “ num1 is not equal to num2”
Step 4. If ( num1 < num2 )
Write “ num1 is less than num2”
Step 5. If ( num1 > num2 )
Write “ num1 is greater than num2”
Step 6. If ( num1 <= num2 )
Write “ num1 is greater than or equal to num2”
Step 7. If ( num1 >= num2 )
Write “ num1 is greater than or equal to num2”
Flow Diagram
Start
Read num1,num2
Sum:=0
number:=2
No
number<=100
Yes
Sum:=Sum+number
number:=number+2
Write" Sum”
End
Tracing an algorithm
Step X y
No.
1 5 -
2 5 7
3 5 8
4.1 5 8
4.2 3 8
4.3 3 8
Program
– an implementation of an algorithm in some
programming language
Data structure
An Organization of data needed to solve the problem
Overall Picture
Correctness
Efficiency
Read Ahead
You are expected to read the lecture notes
before the lecture.
This will facilitate more productive
discussion during class.
Explaining