SlideShare a Scribd company logo
1 of 64
Download to read offline
Data Structures & Algorithm
CSC-102
Lecture 1
Introduction to Data Structures
and Algorithms
Lecturer: Syeda Nazia Ashraf 1
Instructor: Syeda Nazia Ashraf
• Lecturer, Computer Science in CS Department,
SMIU, Karachi
• Enrolled in PhD(CS) from Bahria University
Karachi
• M.Engg. in Computer Systems from NED
University, Karachi
• BS Computer Engg. from SSUET, Karachi
• More than 2 years of Experience in FUUAST,
Karachi
• 4 years of Experience in SMIU, Karachi
2
Course Outline
• Introduction to Data Structures and Algorithms
• Arrays. Multidimensional Arrays
• Linked Lists
• Doubly Linked List, Circular Link List
• Stack using Linked List
• Infix to Post Fix, Post Fix to Infix
• Queues, Queues using Array
• Priority Queue, Trees
• Application of Binary Tree, Traversing a Binary Tree
• Recursion (Pre order, In order), Level order Traversal
• Degenerate Binary Search Tree
3
Course Outline (Cont.)
• Balanced Binary Tree, Expression Tree, Complete Binary
Tree
• Heap
• Hashing
• Sorting Algorithms (Selection, Insertion, Bubble)
• NlogN Algorithms (Divide and Conquer, Merge Sort,
Quick Sort)
• AVL Trees
• AVL Rotations
• AVL Rotations Insert Operation
• Graph
• Algorithm Complexity
4
Grading
• Quizzes = 30%
• Assignments = 20%
• Class Participation = (Proj/Pres, Attendance,
Punctuality, Active participation in Class/Lab)=
10%
• Final Exam/Term Paper/Viva = 40%
• Course Web Page:
http:// lms.smiu.edu.pk
• Email : snazia@smiu.edu.pk
5
Text Book:
Programme Design with Pseudo-code, Bailey and
Lundgaard, Brooks/Cole Publishing, 1988
Reference Books:
• Simple Programme Design: A step-by-step
approach, 4/e, Lesley Anne Robertson, ISBN: 0-
619-16046-2 © 2004.
• Theory and Problems of Data Structures,
Schaum’s Outline Series
• Data Structures and their Implementations, Robert
J. Baron, Linda G. Shapiro
6
Goals of this Course
• Learn the commonly used data structures.
• How to design good algorithms.
• Cover well-known data structures such as
arrays, linked lists, stacks, queues, trees and
graphs.
• Implement data structures and algorithms in
C language code.
7
•The primary objective of programming is to efficiently
process the input to generate the desired output
•We can achieve this objective in an efficient and neat
style if the input data is organized in a way to help us meet
our goal
• Data structures is nothing but ways and means of
organizing data so that it can be processed easily and
efficiently
•Data structure dictate the manner in which the data can
be processed. In other words the choice of an algorithm
depends upon the underlying data organization
Data structures
8
Organizing Data
▪ Any organization for a collection of records
that can be searched, processed in any
order, or modified.
▪ The choice of data structure and algorithm
can make the difference between a
program running in a few seconds or many
days.
9
Efficiency
▪ A solution is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time
▪ The cost of a solution is the amount of
resources that the solution consumes.
10
Data Structure Philosophy
▪ Each data structure has costs and
benefits.
▪ Rarely is one data structure better than
another in all situations.
▪ A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.
11
12
• An algorithm is a set of rules for carrying out calculation either by
hand or on a machine
• An algorithm is a finite step-by-step procedure to achieve a
required result
• An algorithm is a sequence of computational steps that transform
the input into the output.
• An algorithm is a sequence of operations performed on data that
have to be organized in data structures.
• An algorithm is a systematic logical approach used to problems in a
computer
• Step wise algorithm gives us the complete overview of the steps in
detail and it can also be defined as the English language
representation of the steps performed.
Algorithm
13
Origin of the Term “Algorithm”
• The name derives from the title of a Latin
book: Algoritmi de numero Indorum
• That book was written by the famous 9th
century Muslim mathematician, Muhammad
ibn Musa al-Khwarizmi
• The study of algorithms began with
mathematicians and was a significant area of
work in the early years
14
Why Algorithms are Useful?
• Once we find an algorithm for solving a
problem, we do not need to re-discover it the
next time we are faced with that problem
• Once an algorithm is known, the task of
solving the problem reduces to following
(almost blindly and without thinking) the
instructions precisely
• All the knowledge required for solving the
problem is present in the algorithm
15
What is Algorithm?
16
Why Study Algorithm
• A good understanding of Algorithm Design is a central
element to a good understanding of computer science
and computer programming.
• They apply these to various applications:
- Compilers,
- Operating Systems,
- Data bases,
- Artificial Intelligence,
- Computer Graphics and Vision,
- Networks, etc.
17
Properties of Algorithm
18
 There are the properties of algorithm that
must have to consider in solving a certain
problem in programming:
▪ Finiteness
▪ Definiteness / Absence of Ambiguity
▪ Sequence Definition
▪ Input and Output Definition
▪ Effectiveness
▪ Scope of Definition
Properties of Algorithm
19
• Finiteness
– The algorithm must always terminate after a finite
number of steps.
– The execution of a programmed algorithm must
be complete after a finite number of operations
have been performed. Otherwise, we cannot
claim that the execution produces a solution.
Properties of Algorithm
20
• Definiteness / Absence of Ambiguity:
• Each step must be precisely defined; the actions to
be carried out must be rigorously and
unambiguously specified for each case.
• The representation of every step of an algorithm
should have a unique interpretation which also
understand by the human.
– It is convenient to deal with algorithms presented
in notational with scattered detail:
• Example:
–Pseudo code
–Flowcharts
Properties of Algorithm
21
• Sequence of Definition
– The sequence in which the steps of the algorithm
are to carried out should be clearly specified.
– In algorithmic specifications, the instructions are
performed from top to button, unless the
instruction themselves otherwise specified.
Properties of Algorithm
22
• Input and Output Definition
– Inputs – are the data items that is presented in
the algorithm.
– An algorithm has zero or more inputs, taken from
a specified set of objects
– Outputs – are the data items presented to the
outside world as the result of the execution of a
program based on the algorithm.
– An algorithm has one or more outputs, which
have a specified relation to the inputs.
Properties of Algorithm
23
• Effectiveness
– All operations to be performed must be
sufficiently basic that they can be done exactly
and in finite length.
– It consists of basic instructions that are realizable.
This means that the instructions can be performed
by using the given inputs in a finite amount of
time.
– The instructions of an algorithm may order the
computer only to perform tasks that is capable of
carrying out.
Properties of Algorithm
24
• Scope Definition
– An algorithm applies to the following:
• Specific problem or class of problem
• The range of inputs has to be predefined
• The range determines the generality of the algorithm.
How to express/specify an Algorithm?
25
• Algorithms can be expressed in many kinds of
notation or methods, including:
– Natural language: usually verbose and ambiguous
– Pseudo-Code: also avoids most issues of
ambiguity; vaguely resembles common elements
of programming languages; no particular
agreement on syntax
– Flowcharts: avoid most (if not all) issues of
ambiguity; difficult to modify w/o specialized
tools; largely standardized
– Programming Language: tend to require
expressing low-level details that are not necessary
for a high-level understanding
Pseudo code
• High-level description of an algorithm
• More structured than English prose
• Less detailed than a program
• Preferred notation for describing algorithms
• Hides program design issues
• In this type of algorithm, special keywords
(pseudo words) are used to represent the
steps of a given task.
26
What is Pseudocode…?
• “Pseudo” means imitation or false and “code”
refers to the instructions written in a
programming language.
• Pseudocode is another programming analysis tool
that is used for planning a program.
• Pseudocode is also called Program Design
Language (PDL).
• Pseudocode is the statement in plain English
which may be translated later into a
programming language (program).
• Pseudo code is an intermediary between an
algorithm and implemented program.
27
What is Pseudocode…?
• By wikipedia definition:
–Pseudocode is a compact and informal
high-level description of a computer
programming algorithm that uses the
structural conventions of some
programming language, but is intended for
human reading rather than machine
reading.
28
Pseudocode Details
• Control flow
– if … then … [else …]
– while … do …
– repeat … until …
– for … do …
– Indentation replaces braces
• Method declaration
Algorithm method (arg [, arg…])
Input …
Output …
• Method call
var.method (arg [, arg…])
• Return value
return expression
• Expressions
Assignment
(like = in Java)
= Equality testing
(like == in Java)
n2 Superscripts and other
mathematical formatting
allowed
29
30
Pseudo code Example
31
Pseudo code Example
32
Program to calculate Area of Circle
Pseudo code Example
33
Pseudo code Example
Example 1
Write an algorithm in pseudo code that finds
the average of two numbers
Solution
Average of Two
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Algorithm : Average of two
34
Example 2
Write an algorithm to change a numeric grade to a
pass/no pass grade.
Pass/No Pass Grade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “no pass”
End if
2. Return the grade
End
Algorithm : Pass/no pass Grade
35
Example 3 Write an algorithm to change a numeric grade
to a letter grade.
Letter Grade
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Algorithm : Letter grade
Continues on the next slide 36
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if
Algorithm : Letter grade (continued)
37
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
Example 4 Write an algorithm to find the largest of a set
of numbers. You do not know the number of
numbers.
Find Largest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the integer
End if
End while
3. Return Largest
End
Algorithm : Find largest
38
Example 5 Write an algorithm to find the
largest of 1000 numbers.
Find Largest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
Algorithm : Find largest of 1000 numbers
39
Psuedocode-Another Example
CheckInBook(library, book, fineSlip)
Function: Checks in a book
Input: A Record of library,
A book as String,
Output: fineSlip as String,
updated record of library
precondition: book was checked out of this
library, book is presented at the
check-in-desk
postcondition: fineSlip is issued if the book is overdue,
contents of the library are the original
contents of book
40
• Algorithm:
1. Examine due date to see whether the book is late
2. if the book is late
a. calculate fine
b. Issue fineSlip
3. Update library records to show that the book has
been returned
4. check reserve list to see if someone is waiting for
the book
5. If book is on reserve list
a. put the book on the reserve shelf
6. Else
a. Replace the book on the proper shelf,
according to the library shelf arrangement
scheme
41
Rules for Pseudocode
– Write only one statement per line.
– Capitalized initial keyword.
– Indent to show hierarchy.
– End multi-line structures.
– Keep statement language independent.
1. Write only one statement per line.
– Each statement in your pseudocode should
express just one action for the computer.
– If the task list is properly drawn, then in most
cases each task will correspond to one line of
pseudocode. 42
Rules for Pseudocode
• Examples
43
Rules for Pseudocode
2. Capitalized initial keyword.
– In the example above, READ and WRITE are in
caps.
– There are just a few keywords we will use:
• READ, WRITE, IF, ELSE, ENDIF, WHILE,
ENDWHILE, REPEAT, UNTIL
Rules for Variable Names
• Begin with lowercase letter
• Contain no spaces
• Additional words begin with capital
• Unique names within code
• Consistent use of names
44
Rules for Pseudocode
3. Indent to show hierarchy.
– We will use a particular indentation pattern in
each of the design structures:
• SEQUENCE: keep statements that are “stacked” in
sequence all starting in the same column.
• SELECTION: indent the statements that fall inside the
selection structure, but not the keywords that form the
selection
• LOOPING: indent the statements that fall inside the
loop, but not the keywords that form the loop
45
Rules for Pseudocode
• Examples:
46
Rules for Pseudocode
4. End multi-line structures.
▫ All the initial keyword must always in line with the last or
end of the structure.
Example:
READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net
5. Keep statement language independent.
▫ Resist the need to write in whatever language you are most
comfortable with. There may be special features available in
the language you plan to eventually write the program in; if
you are SURE it will be written in that language, then you can
use the features. If not, then avoid using the special features.
47
Standard for good pseudocode…
• These are follows:
▫ Number each instruction.
 This is to enforce the notion, “well-ordered
collection of ... operations.”
▫ Each instruction should be unambiguous.
 It means the computing agent, in this case the
reader, should be capable of carrying out the
instructions. And also, each instruction should be
effectively computable (do-able).
▫ Completeness.
 Nothing should be left out.
48
Common Action Keywords
Several keywords are often used to indicate common
input, output, and processing operations.
Input: READ, OBTAIN, GET
e.g. Read student name (from the student file)
Get system date (from the computer system).
Read student ID number
Get order.
Output: WRITE, PUT, PRINT, DISPLAY, SHOW
e.g. Print "End of the Output"
Write student record to master file
Put out name, address and post code
Output grade; Display "an input error occurred, please re-enter“
Add one: INCREMENT
49
Perform Arithmatic: COMPUTE, CALCULATE,
DETERMINE
e.g. Divide total_score by student_count
class_average = total_score / student_count
Compute C = (F – 32) * 5 / 9
Calculate the number of registrations
The + symbol means to add: 2 + 3 = 5.
The – symbol means to subtract: 2 – 3 = -1.
The * symbol means to multiply: 2 * 3 = 6.
The / symbol means to divide: 2 / 3 = .66667
The ^ symbol means to the power of. 2 ^ 3 = 8.
50
51
Assign variable or memory location: SET, INITIALIZE
(To assign a value as a result of some processing, the symbol
"=" is used)
To keep a piece of information for later use, the verbs SAVE
or STORE are used.
e.g. Initialize total_score to 0: total_score = 0
Set student_count to 0: student_count = 0
total_score = total_score + score 1
student_count = student_count + 1
class_average = total_score / student_count
store class_average in class_average_quiz1
Compare two variables and select one of the two alternative
choices: IF, THEN, ELSE and ENDIF are the words used to do this.
E.g.
If student is part_time Then
add 1 to part_time_count
Else
add 1 to full_time_count
EndIf
Repeat a set of actions: That is the control structure interation,
more commonly known as a loop. DO WHILE ----END DO , (while---
do) ,for ---- do , repeat----until are the words used in pseudocode.
E.g.
Do While student_total <30
Read student record
Print student name, address
Add 1 to student_total
End Do 52
More on Formatting and Conventions in Pseudocoding
•INDENTATION in pseudocode should be identical to its
implementation in a programming language. Try to indent at least
four spaces.
•As noted above, the pseudocode entries are to be cryptic, AND
SHOULD NOT BE PROSE. NO SENTENCES.
•Do not include data declarations in your pseudocode.
•But do cite variables that are initialized as part of their declarations.
E.g. "initialize count to zero" is a good entry.
Function Calls, Function Documentation, and Pseudocode
Calls to Functions should appear as: Call FunctionName (arguments:
field1, field2, etc.)
Returns in functions should appear as: Return (field1)
Function headers should appear as: FunctionName (parameters:
field1, field2, etc. )
53
Logical Structure of Pseudocode
• Pseudocode is made up of the following logic
structures that have been proved to be
sufficient for writing any computer program:
–Sequence Logic
–Selection Logic
–Iteration Logic
54
Sequence Logic
55
Selection Logic
56
Selection Logic
57
Iteration Logic
58
Iteration Logic
59
Iteration Logic
60
Working with Fields
Calculation Symbols
• To symbolize the arithmetic operators we use these
symbols:
– Note: There is a precedence or hierarchy implied in this
symbols.
61
Working with Fields
Selection Symbols
• When we have to make a choice between
actions, we almost always base that choice on a
test.
• There is a universally accepted set of symbols
used to represent these phrases:
62
Logical Operators
63
What is the difference between Algorithm and
Pseudocode?
• An algorithm is a well defined sequence of steps that provides
a solution for a given problem, while a pseudocode is one of
the methods that can be used to represent an algorithm.
• While algorithms can be written in natural language,
pseudocode is written in a format that is closely related to
high level programming language structures.
• But pseudocode does not use specific programming language
syntax and therefore could be understood by programmers
who are familiar with different programming languages.
• Additionally, transforming an algorithm presented in
pseudocode to programming code could be much easier than
converting an algorithm written in natural language.
64

More Related Content

What's hot

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Artificial Intelligence - A modern approach 3ed
Artificial Intelligence - A modern approach 3edArtificial Intelligence - A modern approach 3ed
Artificial Intelligence - A modern approach 3edRohanMistry15
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplicationKiran K
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureVrushali Dhanokar
 
State Space Search in ai
State Space Search in aiState Space Search in ai
State Space Search in aivikas dhakane
 
Theory of Computation
Theory of ComputationTheory of Computation
Theory of ComputationShiraz316
 
Uninformed search /Blind search in AI
Uninformed search /Blind search in AIUninformed search /Blind search in AI
Uninformed search /Blind search in AIKirti Verma
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and designMegha V
 

What's hot (20)

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
P vs NP
P vs NP P vs NP
P vs NP
 
Artificial Intelligence - A modern approach 3ed
Artificial Intelligence - A modern approach 3edArtificial Intelligence - A modern approach 3ed
Artificial Intelligence - A modern approach 3ed
 
Hill climbing
Hill climbingHill climbing
Hill climbing
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Lisp
LispLisp
Lisp
 
Ia5 rbc
Ia5  rbcIa5  rbc
Ia5 rbc
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Algorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structureAlgorithm analysis in fundamentals of data structure
Algorithm analysis in fundamentals of data structure
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
State Space Search in ai
State Space Search in aiState Space Search in ai
State Space Search in ai
 
Theory of Computation
Theory of ComputationTheory of Computation
Theory of Computation
 
Uninformed search /Blind search in AI
Uninformed search /Blind search in AIUninformed search /Blind search in AI
Uninformed search /Blind search in AI
 
Problem Solving
Problem Solving Problem Solving
Problem Solving
 
Daa
DaaDaa
Daa
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 

Similar to DS & Algo Intro

Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithmsiqbalphy1
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfDukeCalvin
 
Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structuressonykhan3
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programmingSangheethaa Sukumaran
 
Csci101 lect00 introduction
Csci101 lect00 introductionCsci101 lect00 introduction
Csci101 lect00 introductionElsayed Hemayed
 
Introduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonIntroduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonPriyankaC44
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit onessuserb7c8b8
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
 
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptxAlgorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptxRobertCarreonBula
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsAkhil Kaushik
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)Yogesh Deshpande
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 

Similar to DS & Algo Intro (20)

Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdf
 
Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structures
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programming
 
Csci101 lect00 introduction
Csci101 lect00 introductionCsci101 lect00 introduction
Csci101 lect00 introduction
 
Introduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- PythonIntroduction to Problem Solving Techniques- Python
Introduction to Problem Solving Techniques- Python
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
Unit no_1.pptx
Unit no_1.pptxUnit no_1.pptx
Unit no_1.pptx
 
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptxAlgorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 

More from MuhammadUmerIhtisham

Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfMuhammadUmerIhtisham
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfMuhammadUmerIhtisham
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfMuhammadUmerIhtisham
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfMuhammadUmerIhtisham
 

More from MuhammadUmerIhtisham (14)

LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Lect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdfLect 13, 14 (final)AVL Tree and Rotations.pdf
Lect 13, 14 (final)AVL Tree and Rotations.pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
LEC3-DS ALGO(updated).pdf
LEC3-DS  ALGO(updated).pdfLEC3-DS  ALGO(updated).pdf
LEC3-DS ALGO(updated).pdf
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
LEC 8-DS ALGO(heaps).pdf
LEC 8-DS  ALGO(heaps).pdfLEC 8-DS  ALGO(heaps).pdf
LEC 8-DS ALGO(heaps).pdf
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
 
SMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdfSMIU Discrete Structure Lecture 3 Section 3E.pdf
SMIU Discrete Structure Lecture 3 Section 3E.pdf
 
Discrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdfDiscrete Structure Lecture #5 & 6.pdf
Discrete Structure Lecture #5 & 6.pdf
 
Discrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdfDiscrete Structure Lecture #7 & 8.pdf
Discrete Structure Lecture #7 & 8.pdf
 
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdfSMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
SMIU Lecture #1 & 2 Introduction to Discrete Structure and Truth Table.pdf
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

DS & Algo Intro

  • 1. Data Structures & Algorithm CSC-102 Lecture 1 Introduction to Data Structures and Algorithms Lecturer: Syeda Nazia Ashraf 1
  • 2. Instructor: Syeda Nazia Ashraf • Lecturer, Computer Science in CS Department, SMIU, Karachi • Enrolled in PhD(CS) from Bahria University Karachi • M.Engg. in Computer Systems from NED University, Karachi • BS Computer Engg. from SSUET, Karachi • More than 2 years of Experience in FUUAST, Karachi • 4 years of Experience in SMIU, Karachi 2
  • 3. Course Outline • Introduction to Data Structures and Algorithms • Arrays. Multidimensional Arrays • Linked Lists • Doubly Linked List, Circular Link List • Stack using Linked List • Infix to Post Fix, Post Fix to Infix • Queues, Queues using Array • Priority Queue, Trees • Application of Binary Tree, Traversing a Binary Tree • Recursion (Pre order, In order), Level order Traversal • Degenerate Binary Search Tree 3
  • 4. Course Outline (Cont.) • Balanced Binary Tree, Expression Tree, Complete Binary Tree • Heap • Hashing • Sorting Algorithms (Selection, Insertion, Bubble) • NlogN Algorithms (Divide and Conquer, Merge Sort, Quick Sort) • AVL Trees • AVL Rotations • AVL Rotations Insert Operation • Graph • Algorithm Complexity 4
  • 5. Grading • Quizzes = 30% • Assignments = 20% • Class Participation = (Proj/Pres, Attendance, Punctuality, Active participation in Class/Lab)= 10% • Final Exam/Term Paper/Viva = 40% • Course Web Page: http:// lms.smiu.edu.pk • Email : snazia@smiu.edu.pk 5
  • 6. Text Book: Programme Design with Pseudo-code, Bailey and Lundgaard, Brooks/Cole Publishing, 1988 Reference Books: • Simple Programme Design: A step-by-step approach, 4/e, Lesley Anne Robertson, ISBN: 0- 619-16046-2 © 2004. • Theory and Problems of Data Structures, Schaum’s Outline Series • Data Structures and their Implementations, Robert J. Baron, Linda G. Shapiro 6
  • 7. Goals of this Course • Learn the commonly used data structures. • How to design good algorithms. • Cover well-known data structures such as arrays, linked lists, stacks, queues, trees and graphs. • Implement data structures and algorithms in C language code. 7
  • 8. •The primary objective of programming is to efficiently process the input to generate the desired output •We can achieve this objective in an efficient and neat style if the input data is organized in a way to help us meet our goal • Data structures is nothing but ways and means of organizing data so that it can be processed easily and efficiently •Data structure dictate the manner in which the data can be processed. In other words the choice of an algorithm depends upon the underlying data organization Data structures 8
  • 9. Organizing Data ▪ Any organization for a collection of records that can be searched, processed in any order, or modified. ▪ The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. 9
  • 10. Efficiency ▪ A solution is said to be efficient if it solves the problem within its resource constraints. – Space – Time ▪ The cost of a solution is the amount of resources that the solution consumes. 10
  • 11. Data Structure Philosophy ▪ Each data structure has costs and benefits. ▪ Rarely is one data structure better than another in all situations. ▪ A data structure requires: – space for each data item it stores, – time to perform each basic operation, – programming effort. 11
  • 12. 12
  • 13. • An algorithm is a set of rules for carrying out calculation either by hand or on a machine • An algorithm is a finite step-by-step procedure to achieve a required result • An algorithm is a sequence of computational steps that transform the input into the output. • An algorithm is a sequence of operations performed on data that have to be organized in data structures. • An algorithm is a systematic logical approach used to problems in a computer • Step wise algorithm gives us the complete overview of the steps in detail and it can also be defined as the English language representation of the steps performed. Algorithm 13
  • 14. Origin of the Term “Algorithm” • The name derives from the title of a Latin book: Algoritmi de numero Indorum • That book was written by the famous 9th century Muslim mathematician, Muhammad ibn Musa al-Khwarizmi • The study of algorithms began with mathematicians and was a significant area of work in the early years 14
  • 15. Why Algorithms are Useful? • Once we find an algorithm for solving a problem, we do not need to re-discover it the next time we are faced with that problem • Once an algorithm is known, the task of solving the problem reduces to following (almost blindly and without thinking) the instructions precisely • All the knowledge required for solving the problem is present in the algorithm 15
  • 17. Why Study Algorithm • A good understanding of Algorithm Design is a central element to a good understanding of computer science and computer programming. • They apply these to various applications: - Compilers, - Operating Systems, - Data bases, - Artificial Intelligence, - Computer Graphics and Vision, - Networks, etc. 17
  • 18. Properties of Algorithm 18  There are the properties of algorithm that must have to consider in solving a certain problem in programming: ▪ Finiteness ▪ Definiteness / Absence of Ambiguity ▪ Sequence Definition ▪ Input and Output Definition ▪ Effectiveness ▪ Scope of Definition
  • 19. Properties of Algorithm 19 • Finiteness – The algorithm must always terminate after a finite number of steps. – The execution of a programmed algorithm must be complete after a finite number of operations have been performed. Otherwise, we cannot claim that the execution produces a solution.
  • 20. Properties of Algorithm 20 • Definiteness / Absence of Ambiguity: • Each step must be precisely defined; the actions to be carried out must be rigorously and unambiguously specified for each case. • The representation of every step of an algorithm should have a unique interpretation which also understand by the human. – It is convenient to deal with algorithms presented in notational with scattered detail: • Example: –Pseudo code –Flowcharts
  • 21. Properties of Algorithm 21 • Sequence of Definition – The sequence in which the steps of the algorithm are to carried out should be clearly specified. – In algorithmic specifications, the instructions are performed from top to button, unless the instruction themselves otherwise specified.
  • 22. Properties of Algorithm 22 • Input and Output Definition – Inputs – are the data items that is presented in the algorithm. – An algorithm has zero or more inputs, taken from a specified set of objects – Outputs – are the data items presented to the outside world as the result of the execution of a program based on the algorithm. – An algorithm has one or more outputs, which have a specified relation to the inputs.
  • 23. Properties of Algorithm 23 • Effectiveness – All operations to be performed must be sufficiently basic that they can be done exactly and in finite length. – It consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time. – The instructions of an algorithm may order the computer only to perform tasks that is capable of carrying out.
  • 24. Properties of Algorithm 24 • Scope Definition – An algorithm applies to the following: • Specific problem or class of problem • The range of inputs has to be predefined • The range determines the generality of the algorithm.
  • 25. How to express/specify an Algorithm? 25 • Algorithms can be expressed in many kinds of notation or methods, including: – Natural language: usually verbose and ambiguous – Pseudo-Code: also avoids most issues of ambiguity; vaguely resembles common elements of programming languages; no particular agreement on syntax – Flowcharts: avoid most (if not all) issues of ambiguity; difficult to modify w/o specialized tools; largely standardized – Programming Language: tend to require expressing low-level details that are not necessary for a high-level understanding
  • 26. Pseudo code • High-level description of an algorithm • More structured than English prose • Less detailed than a program • Preferred notation for describing algorithms • Hides program design issues • In this type of algorithm, special keywords (pseudo words) are used to represent the steps of a given task. 26
  • 27. What is Pseudocode…? • “Pseudo” means imitation or false and “code” refers to the instructions written in a programming language. • Pseudocode is another programming analysis tool that is used for planning a program. • Pseudocode is also called Program Design Language (PDL). • Pseudocode is the statement in plain English which may be translated later into a programming language (program). • Pseudo code is an intermediary between an algorithm and implemented program. 27
  • 28. What is Pseudocode…? • By wikipedia definition: –Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading. 28
  • 29. Pseudocode Details • Control flow – if … then … [else …] – while … do … – repeat … until … – for … do … – Indentation replaces braces • Method declaration Algorithm method (arg [, arg…]) Input … Output … • Method call var.method (arg [, arg…]) • Return value return expression • Expressions Assignment (like = in Java) = Equality testing (like == in Java) n2 Superscripts and other mathematical formatting allowed 29
  • 32. 32 Program to calculate Area of Circle Pseudo code Example
  • 34. Example 1 Write an algorithm in pseudo code that finds the average of two numbers Solution Average of Two Input: Two numbers 1. Add the two numbers 2. Divide the result by 2 3. Return the result by step 2 End Algorithm : Average of two 34
  • 35. Example 2 Write an algorithm to change a numeric grade to a pass/no pass grade. Pass/No Pass Grade Input: One number 1. if (the number is greater than or equal to 70) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “no pass” End if 2. Return the grade End Algorithm : Pass/no pass Grade 35
  • 36. Example 3 Write an algorithm to change a numeric grade to a letter grade. Letter Grade Input: One number 1. if (the number is between 90 and 100, inclusive) then 1.1 Set the grade to “A” End if 2. if (the number is between 80 and 89, inclusive) then 2.1 Set the grade to “B” End if Algorithm : Letter grade Continues on the next slide 36
  • 37. 3. if (the number is between 70 and 79, inclusive) then 3.1 Set the grade to “C” End if 4. if (the number is between 60 and 69, inclusive) then 4.1 Set the grade to “D” End if Algorithm : Letter grade (continued) 37 5. If (the number is less than 60) then 5.1 Set the grade to “F” End if 6. Return the grade End
  • 38. Example 4 Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers. Find Largest Input: A list of positive integers 1. Set Largest to 0 2. while (more integers) 2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the integer End if End while 3. Return Largest End Algorithm : Find largest 38
  • 39. Example 5 Write an algorithm to find the largest of 1000 numbers. Find Largest Input: 1000 positive integers 1. Set Largest to 0 2. Set Counter to 0 3. while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while 4. Return Largest End Algorithm : Find largest of 1000 numbers 39
  • 40. Psuedocode-Another Example CheckInBook(library, book, fineSlip) Function: Checks in a book Input: A Record of library, A book as String, Output: fineSlip as String, updated record of library precondition: book was checked out of this library, book is presented at the check-in-desk postcondition: fineSlip is issued if the book is overdue, contents of the library are the original contents of book 40
  • 41. • Algorithm: 1. Examine due date to see whether the book is late 2. if the book is late a. calculate fine b. Issue fineSlip 3. Update library records to show that the book has been returned 4. check reserve list to see if someone is waiting for the book 5. If book is on reserve list a. put the book on the reserve shelf 6. Else a. Replace the book on the proper shelf, according to the library shelf arrangement scheme 41
  • 42. Rules for Pseudocode – Write only one statement per line. – Capitalized initial keyword. – Indent to show hierarchy. – End multi-line structures. – Keep statement language independent. 1. Write only one statement per line. – Each statement in your pseudocode should express just one action for the computer. – If the task list is properly drawn, then in most cases each task will correspond to one line of pseudocode. 42
  • 44. Rules for Pseudocode 2. Capitalized initial keyword. – In the example above, READ and WRITE are in caps. – There are just a few keywords we will use: • READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, UNTIL Rules for Variable Names • Begin with lowercase letter • Contain no spaces • Additional words begin with capital • Unique names within code • Consistent use of names 44
  • 45. Rules for Pseudocode 3. Indent to show hierarchy. – We will use a particular indentation pattern in each of the design structures: • SEQUENCE: keep statements that are “stacked” in sequence all starting in the same column. • SELECTION: indent the statements that fall inside the selection structure, but not the keywords that form the selection • LOOPING: indent the statements that fall inside the loop, but not the keywords that form the loop 45
  • 46. Rules for Pseudocode • Examples: 46
  • 47. Rules for Pseudocode 4. End multi-line structures. ▫ All the initial keyword must always in line with the last or end of the structure. Example: READ name, grossPay, taxes IF taxes > 0 net = grossPay – taxes ELSE net = grossPay ENDIF WRITE name, net 5. Keep statement language independent. ▫ Resist the need to write in whatever language you are most comfortable with. There may be special features available in the language you plan to eventually write the program in; if you are SURE it will be written in that language, then you can use the features. If not, then avoid using the special features. 47
  • 48. Standard for good pseudocode… • These are follows: ▫ Number each instruction.  This is to enforce the notion, “well-ordered collection of ... operations.” ▫ Each instruction should be unambiguous.  It means the computing agent, in this case the reader, should be capable of carrying out the instructions. And also, each instruction should be effectively computable (do-able). ▫ Completeness.  Nothing should be left out. 48
  • 49. Common Action Keywords Several keywords are often used to indicate common input, output, and processing operations. Input: READ, OBTAIN, GET e.g. Read student name (from the student file) Get system date (from the computer system). Read student ID number Get order. Output: WRITE, PUT, PRINT, DISPLAY, SHOW e.g. Print "End of the Output" Write student record to master file Put out name, address and post code Output grade; Display "an input error occurred, please re-enter“ Add one: INCREMENT 49
  • 50. Perform Arithmatic: COMPUTE, CALCULATE, DETERMINE e.g. Divide total_score by student_count class_average = total_score / student_count Compute C = (F – 32) * 5 / 9 Calculate the number of registrations The + symbol means to add: 2 + 3 = 5. The – symbol means to subtract: 2 – 3 = -1. The * symbol means to multiply: 2 * 3 = 6. The / symbol means to divide: 2 / 3 = .66667 The ^ symbol means to the power of. 2 ^ 3 = 8. 50
  • 51. 51 Assign variable or memory location: SET, INITIALIZE (To assign a value as a result of some processing, the symbol "=" is used) To keep a piece of information for later use, the verbs SAVE or STORE are used. e.g. Initialize total_score to 0: total_score = 0 Set student_count to 0: student_count = 0 total_score = total_score + score 1 student_count = student_count + 1 class_average = total_score / student_count store class_average in class_average_quiz1
  • 52. Compare two variables and select one of the two alternative choices: IF, THEN, ELSE and ENDIF are the words used to do this. E.g. If student is part_time Then add 1 to part_time_count Else add 1 to full_time_count EndIf Repeat a set of actions: That is the control structure interation, more commonly known as a loop. DO WHILE ----END DO , (while--- do) ,for ---- do , repeat----until are the words used in pseudocode. E.g. Do While student_total <30 Read student record Print student name, address Add 1 to student_total End Do 52
  • 53. More on Formatting and Conventions in Pseudocoding •INDENTATION in pseudocode should be identical to its implementation in a programming language. Try to indent at least four spaces. •As noted above, the pseudocode entries are to be cryptic, AND SHOULD NOT BE PROSE. NO SENTENCES. •Do not include data declarations in your pseudocode. •But do cite variables that are initialized as part of their declarations. E.g. "initialize count to zero" is a good entry. Function Calls, Function Documentation, and Pseudocode Calls to Functions should appear as: Call FunctionName (arguments: field1, field2, etc.) Returns in functions should appear as: Return (field1) Function headers should appear as: FunctionName (parameters: field1, field2, etc. ) 53
  • 54. Logical Structure of Pseudocode • Pseudocode is made up of the following logic structures that have been proved to be sufficient for writing any computer program: –Sequence Logic –Selection Logic –Iteration Logic 54
  • 61. Working with Fields Calculation Symbols • To symbolize the arithmetic operators we use these symbols: – Note: There is a precedence or hierarchy implied in this symbols. 61
  • 62. Working with Fields Selection Symbols • When we have to make a choice between actions, we almost always base that choice on a test. • There is a universally accepted set of symbols used to represent these phrases: 62
  • 64. What is the difference between Algorithm and Pseudocode? • An algorithm is a well defined sequence of steps that provides a solution for a given problem, while a pseudocode is one of the methods that can be used to represent an algorithm. • While algorithms can be written in natural language, pseudocode is written in a format that is closely related to high level programming language structures. • But pseudocode does not use specific programming language syntax and therefore could be understood by programmers who are familiar with different programming languages. • Additionally, transforming an algorithm presented in pseudocode to programming code could be much easier than converting an algorithm written in natural language. 64