04 Programming Concepts
04 Programming Concepts
DIONEDA (FACULTY-IN-CHARGE)
1
Definition of Algorithm
2
Algorithm Representation
3
Folding a Bird From a Square
Piece of Paper
4
Origami Primitives
5
Pseudocode Primitives
• Assignment
name = expression
• Example
RemainingFunds = CheckingBalance +
SavingsBalance
6
Pseudocode Primitives
Conditional selection
if (condition):
activity
Example
if (sales have decreased):
lower the price by 5%
7
Pseudocode Primitives
Conditional selection
if (condition):
activity
else:
activity
Example
if (year is leap year):
daily total = total / 366
else:
daily total = total / 365
8
Pseudocode Primitives
Repeated execution
while (condition):
body
Example
while (tickets remain to be sold):
sell a ticket
9
Pseudocode Primitives
10
Pseudocode Primitives
• Define a function
def name():
• Example
def ProcessLoan():
• Executing a function
if (. . .):
ProcessLoan()
else:
RejectApplication()
11
The Procedure Greetings in
Pseudocode
12
Pseudocode Primitives
• Using parameters
def Sort(List):
.
.
13
Polya’s Problem Solving Steps
14
Polya’s Steps in the Context of
Program Development
1. Understand the problem.
2. Get an idea of how an algorithmic function
might solve the problem.
3. Formulate the algorithm and represent it as
a program.
4. Evaluate the solution for accuracy and its
potential as a tool for solving other
problems.
15
Getting a Foot in the Door
17
Analyzing the Possibilities
18
The Sequential Search Algorithm
Pseudocode
def Search (List, TargetValue):
if (List is empty):
Declare search a failure
else:
Select the first entry in List
to be TestEntry
while (TargetValue > TestEntry
and entries remain):
Select the next entry in
List as TestEntry
if (TargetValue == TestEntry):
Declare search a success
else:
Declare search a failure
19
Components of Repetitive
Control
20
Iterative Structures
Pretest loop:
while (condition):
body
Posttest loop:
repeat:
body
until(condition)
21
The While Loop Structure
22
The Repeat Loop Structure
23
Sorting the List Fred, Alex,
Diana, Byron, and Carol
Alphabetically
24
The Insertion Sort Algorithm
expressed in pseudocode
def Sort(List):
N = 2
while (N <= length of List):
Pivot = Nth entry in List
Remove Nth entry leaving a hole in
List
while (there is an Entry above the
hole and Entry > Pivot):
Move Entry down into the hole
leaving
a hole in the list above the
Entry
Move Pivot into the hole
N = N + 1
25
Recursion
26
Applying Our Strategy to Search
a List for the Entry John
27
A First Draft of the Binary Search
Technique
if (List is empty):
Report that the search failed
else:
TestEntry = middle entry in the List
if (TargetValue == TestEntry):
Report that the search succeeded
if (TargetValue < TestEntry):
Search the portion of List preceding
TestEntry for
TargetValue, and report the result of
that search
if (TargetValue > TestEntry):
Search the portion of List following
TestEntry for
TargetValue, and report the result of
that search
28
The Binary Search Algorithm in
Pseudocode
def Search(List, TargetValue):
if (List is empty):
Report that the search failed
else:
TestEntry = middle entry in the List
if (TargetValue == TestEntry):
Report that the search succeeded
if (TargetValue < TestEntry):
Sublist = portion of List
preceding TestEntry
Search(Sublist, TargetValue)
if (TargetValue > TestEntry):
Sublist = portion of List
following TestEntry
Search(Sublist, TargetValue)
29
Recursively Searching
30
Second Recursive Search, First
Snapshot
31
Second Recursive Search, Second
Snapshot
32
Algorithm Efficiency
33
Applying the Insertion Sort in a
Worse-case Situation
34
Graph of the Worse-case Analysis
of the Insertion Sort Algorithm
35
Graph of the Worse-case Analysis
of the Binary Search Algorithm
36
Software Verification
Proof of correctness
Assertions
• Preconditions
• Loop invariants
Testing
37
Chain Separating Problem
38
Separating the Chain Using Only
Three Cuts
39
Solving the Problem with Only
One Cut
40
The Assertions Associated with a
Typical While Structure
41
THANK YOU FOR
LISTENING!!!
42
Reference
43