SlideShare a Scribd company logo
Some slides are borrowed from Mr. Mohammad Alqahtani
•Overview.
•Algorithm Analysis.
 Be familiar with problem solving.
 Be able to develop (and implement) algorithms.
 Be able to trace algorithms.
 Be able to select appropriate data structures and
algorithms for given problems.
 Prerequisites:
◦ Variables and expressions.
◦ Methods (functions or procedures ).
◦ Decision structures( like if-statements and
switch-statements.
◦ Iteration structures (for-loops and while-loops).
◦ Classes and objects
 Data
◦ A collection of facts from which conclusion may be drawn.
◦ e.g. Data: Temperature 35°C; Conclusion: It is hot.
 Types of data
◦ Textual: For example, your name (Alya)
◦ Numeric: For example, your ID (090254)
◦ Audio: For example, your voice
◦ Video: For example, your voice and picture
◦ (...)
5
 Applications/programs read data, store data
temporarily, process it and finally output
results.
 What is data? Numbers, Characters, etc.
Application/
Program
Data Data
6
 Data is classified into data types. e.g. char, float, int, etc.
 A data type is (i) a domain of allowed values and (ii) a set
of operations on these values.
 Compiler signals an error if wrong operation is performed
on data of a certain type. For example, char x,y,z; z =
x*y is not allowed.
7
 Examples
Data Type Domain Operations
boolean 0,1 and, or, =, etc.
char ASCII =, <>, <, etc.
integer -maxint to
+maxint
+, _, =, ==,
<>, <, etc.
8
 Simple Data types: also known as atomic data
types  have no component parts. E.g. int,
char, float, etc.
21 3.14 ‘a’
 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
10
 Structured Data types: can be broken into
component parts. E.g. an object, array, set,
file, etc. Example: a student object.
A H M A D
20
C S C
Name
Age
Branch
A Component part
There are many, but we named a few. We’ll learn these
data structures in great detail!
Array
Linked List
Tree
Queue Stack
Types of Data Structures
 Goal: to organize data
 Criteria: to facilitate efficient
◦ 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 structures)
(Demonstrate using class room
example!)
 Navigating
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
 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
Algorithm development is an art – it needs practice, practice
and only practice!
 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
A program is an instance of an algorithm, written in some
specific programming language
 Problem: Find maximum of a, b, c
 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
Order is very important!!!
 Clearly identify:
 what output is required?
 what is the input?
 What steps are required to transform input into
output
o The most crucial bit
o Needs problem solving skills
o A problem can be solved in many different ways
o Which solution, amongst the different possible
solutions is optimal?
 A sequence of steps to solve a problem
 We need a way to express this sequence of steps
1. Natural language (NL) is an obvious choice, but not
a good choice. Why?
o NLs are notoriously ambiguous (unclear)
2. Programming language (PL) is another choice, but
again not a good choice. Why?
o Algorithm should be PL independent
 We need some balance
o We need PL independence
o We need clarity
o Pseudo-code provides the right balance
 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
 Use PLs construct that are consistent with
modern high level languages, e.g. C++,
Java, ...
 Use appropriate comments for clarity
 Be simple and precise
 Expressions
 Standard mathematical symbols are used
o Left arrow sign (←) as the assignment operator in assignment statements
(equivalent to the = operator in Java)
o Equal sign (=) as the equality relation in Boolean expressions (equivalent to the "=
=" relation in Java)
o For example
Sum ← 0
Sum ← Sum + 5
What is the final value of sum?
 Decision structures (if-then-else logic)
 if condition then
true-actions
[else
false-actions]
 We use indentation to indicate what actions should be included in the true-
actions and false-actions
 For example
if marks > 50 then
print “Congratulation, you are passed!”
else
print “Sorry, you are failed!”
end if
What will be the output if marks are equal to 75?
 Loops (Repetition)
◦ Pre-condition loops
o While loops
 while condition do actions
 We use indentation to indicate what actions should be included in
the loop actions
 For example
while counter < 5 do
print “Welcome to CS204!”
counter ← counter + 1
end while
What will be the output if counter is initialised to 0, 7?
 Loops (Repetition)
◦ Pre-condition loops
o For loops
 for variable-increment-definition do actions
 For example
for counter ← 0; counter < 5; counter ← counter + 2 do
print “Welcome to CS204!”
end for
What will be the output?
 Loops (Repetition)
 Post-condition loops
o Do loops
 do actions while condition
 For example
do
print “Welcome to CS204!”
counter ← counter + 1
while counter < 5
What will be the output, if counter was initialised to 10?
The body of a post-condition loop must execute at least once
 Method declarations
◦ Return_type method_name (parameter_list)
method_body
◦ For example
 integer sum ( integer num1, integer num2)
 start
 result ← num1 + num2
 end
 Method calls
 object.method (args)
 For example
mycalculator.sum(num1, num2)
 Method returns
 return value
 For example
integer sum ( integer num1, integer num2)
start
result ← num1 + num2
return result
end
 Comments
◦ /* Multiple line comments go here. */
◦ // Single line comments go here
◦ Some people prefer braces {}, for comments
 Arrays
 A[i] represents the ith cell in the array A.
 The cells of an n-celled array A are indexed from A[0] to A[n −
1] (consistent with Java).
 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.
 Example 2: Computing Weekly Wages
 Gross pay depends on the pay rate and the number of hours
worked per week. However, if you work more than 40 hours, you
get paid time-and-a-half for all hours worked over 40. Write the
pseudo-code to compute gross pay given pay rate and hours
worked
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
 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.
 Example 2: Computing Weekly Wages
 Gross pay depends on the pay rate and the number of hours
worked per week. However, if you work more than 40 hours,
you get paid time-and-a-half for all hours worked over 40.
Write the pseudo-code to compute gross pay given pay rate and
hours worked
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
Input hours_worked, pay_rate
if hours_worked <= 40 then
gross_pay ← pay_rate x hours_worked
else
basic_pay ← pay_rate x 40
over_time ← hours_worked – 40
over_time_pay ← 1.5 x pay_rate x over_time
gross_pay ← basic_pay + over_time_pay
endfor
print gross_pay
1. Write an algorithm to find the largest
of a set of numbers. You do not know
the number of numbers.
2. Write an algorithm in pseudocode
that finds the average of (n) numbers.
For example numbers are [4,5,14,20,3,6]s

More Related Content

Similar to lec_4_data_structures_and_algorithm_analysis.ppt

3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
Rohit Shrivastava
 
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
ssuserb7c8b8
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
Sabi995708
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
RaoHamza24
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
NishaRohit6
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.ppt
MaiGaafar
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questionsDr P Deepak
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
GovindUpadhyay25
 
5 conceptos progamacion2-tema4
5 conceptos progamacion2-tema45 conceptos progamacion2-tema4
5 conceptos progamacion2-tema4
Elba Sepúlveda
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
DeepakYadav656387
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
racha49
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
SamridhiGulati4
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
BhargaviDalal4
 
Data structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 pptData structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 ppt
aviban
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
Md. Ashikur Rahman
 
c-programming
c-programmingc-programming
c-programming
Zulhazmi Harith
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
DeepikaV81
 
Ds
DsDs

Similar to lec_4_data_structures_and_algorithm_analysis.ppt (20)

3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
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
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
 
Unit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptxUnit 1 Introduction Part 3.pptx
Unit 1 Introduction Part 3.pptx
 
Lecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.pptLecture 01 - Introduction and Review.ppt
Lecture 01 - Introduction and Review.ppt
 
Base sas interview questions
Base sas interview questionsBase sas interview questions
Base sas interview questions
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
5 conceptos progamacion2-tema4
5 conceptos progamacion2-tema45 conceptos progamacion2-tema4
5 conceptos progamacion2-tema4
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
 
Data structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 pptData structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 ppt
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 
c-programming
c-programmingc-programming
c-programming
 
chapter 1
chapter 1chapter 1
chapter 1
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Ds
DsDs
Ds
 

Recently uploaded

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 

Recently uploaded (20)

Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 

lec_4_data_structures_and_algorithm_analysis.ppt

  • 1. Some slides are borrowed from Mr. Mohammad Alqahtani •Overview. •Algorithm Analysis.
  • 2.  Be familiar with problem solving.  Be able to develop (and implement) algorithms.  Be able to trace algorithms.  Be able to select appropriate data structures and algorithms for given problems.
  • 3.  Prerequisites: ◦ Variables and expressions. ◦ Methods (functions or procedures ). ◦ Decision structures( like if-statements and switch-statements. ◦ Iteration structures (for-loops and while-loops). ◦ Classes and objects
  • 4.  Data ◦ A collection of facts from which conclusion may be drawn. ◦ e.g. Data: Temperature 35°C; Conclusion: It is hot.  Types of data ◦ Textual: For example, your name (Alya) ◦ Numeric: For example, your ID (090254) ◦ Audio: For example, your voice ◦ Video: For example, your voice and picture ◦ (...)
  • 5. 5  Applications/programs read data, store data temporarily, process it and finally output results.  What is data? Numbers, Characters, etc. Application/ Program Data Data
  • 6. 6  Data is classified into data types. e.g. char, float, int, etc.  A data type is (i) a domain of allowed values and (ii) a set of operations on these values.  Compiler signals an error if wrong operation is performed on data of a certain type. For example, char x,y,z; z = x*y is not allowed.
  • 7. 7  Examples Data Type Domain Operations boolean 0,1 and, or, =, etc. char ASCII =, <>, <, etc. integer -maxint to +maxint +, _, =, ==, <>, <, etc.
  • 8. 8  Simple Data types: also known as atomic data types  have no component parts. E.g. int, char, float, etc. 21 3.14 ‘a’
  • 9.  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
  • 10. 10  Structured Data types: can be broken into component parts. E.g. an object, array, set, file, etc. Example: a student object. A H M A D 20 C S C Name Age Branch A Component part
  • 11. There are many, but we named a few. We’ll learn these data structures in great detail! Array Linked List Tree Queue Stack Types of Data Structures
  • 12.  Goal: to organize data  Criteria: to facilitate efficient ◦ 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 structures)
  • 13. (Demonstrate using class room example!)  Navigating 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
  • 14.  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
  • 15.  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 Algorithm development is an art – it needs practice, practice and only practice!
  • 16.  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 A program is an instance of an algorithm, written in some specific programming language
  • 17.  Problem: Find maximum of a, b, c  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 Order is very important!!!
  • 18.  Clearly identify:  what output is required?  what is the input?  What steps are required to transform input into output o The most crucial bit o Needs problem solving skills o A problem can be solved in many different ways o Which solution, amongst the different possible solutions is optimal?
  • 19.  A sequence of steps to solve a problem  We need a way to express this sequence of steps 1. Natural language (NL) is an obvious choice, but not a good choice. Why? o NLs are notoriously ambiguous (unclear) 2. Programming language (PL) is another choice, but again not a good choice. Why? o Algorithm should be PL independent  We need some balance o We need PL independence o We need clarity o Pseudo-code provides the right balance
  • 20.  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
  • 21.  Use PLs construct that are consistent with modern high level languages, e.g. C++, Java, ...  Use appropriate comments for clarity  Be simple and precise
  • 22.  Expressions  Standard mathematical symbols are used o Left arrow sign (←) as the assignment operator in assignment statements (equivalent to the = operator in Java) o Equal sign (=) as the equality relation in Boolean expressions (equivalent to the "= =" relation in Java) o For example Sum ← 0 Sum ← Sum + 5 What is the final value of sum?
  • 23.  Decision structures (if-then-else logic)  if condition then true-actions [else false-actions]  We use indentation to indicate what actions should be included in the true- actions and false-actions  For example if marks > 50 then print “Congratulation, you are passed!” else print “Sorry, you are failed!” end if What will be the output if marks are equal to 75?
  • 24.  Loops (Repetition) ◦ Pre-condition loops o While loops  while condition do actions  We use indentation to indicate what actions should be included in the loop actions  For example while counter < 5 do print “Welcome to CS204!” counter ← counter + 1 end while What will be the output if counter is initialised to 0, 7?
  • 25.  Loops (Repetition) ◦ Pre-condition loops o For loops  for variable-increment-definition do actions  For example for counter ← 0; counter < 5; counter ← counter + 2 do print “Welcome to CS204!” end for What will be the output?
  • 26.  Loops (Repetition)  Post-condition loops o Do loops  do actions while condition  For example do print “Welcome to CS204!” counter ← counter + 1 while counter < 5 What will be the output, if counter was initialised to 10? The body of a post-condition loop must execute at least once
  • 27.  Method declarations ◦ Return_type method_name (parameter_list) method_body ◦ For example  integer sum ( integer num1, integer num2)  start  result ← num1 + num2  end  Method calls  object.method (args)  For example mycalculator.sum(num1, num2)
  • 28.  Method returns  return value  For example integer sum ( integer num1, integer num2) start result ← num1 + num2 return result end
  • 29.  Comments ◦ /* Multiple line comments go here. */ ◦ // Single line comments go here ◦ Some people prefer braces {}, for comments  Arrays  A[i] represents the ith cell in the array A.  The cells of an n-celled array A are indexed from A[0] to A[n − 1] (consistent with Java).
  • 30.  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.  Example 2: Computing Weekly Wages  Gross pay depends on the pay rate and the number of hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-half for all hours worked over 40. Write the pseudo-code to compute gross pay given pay rate and hours worked
  • 31. 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
  • 32.  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.  Example 2: Computing Weekly Wages  Gross pay depends on the pay rate and the number of hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-half for all hours worked over 40. Write the pseudo-code to compute gross pay given pay rate and hours worked
  • 33. 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
  • 34. Input hours_worked, pay_rate if hours_worked <= 40 then gross_pay ← pay_rate x hours_worked else basic_pay ← pay_rate x 40 over_time ← hours_worked – 40 over_time_pay ← 1.5 x pay_rate x over_time gross_pay ← basic_pay + over_time_pay endfor print gross_pay
  • 35. 1. Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers. 2. Write an algorithm in pseudocode that finds the average of (n) numbers. For example numbers are [4,5,14,20,3,6]s