Understanding Algorithms: Definitions, Representation, and Pseudocode, Cheat Sheet of Computer Science

An introduction to algorithms, their definition, representation through pseudocode, and an example of calculating finishing time in a marathon using pseudocode. It also covers Polya's problem-solving steps and techniques for getting a foot in the door when solving complex problems.

Typology: Cheat Sheet

2019/2020

Uploaded on 09/20/2021

mohammed-hu-3
mohammed-hu-3 🇱🇾

4 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10/14/2009
1
Algorithms
Definition of Algorithm
An algorithm is an ordered set of
unambiguous, executable steps that
defines a (ideally) terminating process.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Understanding Algorithms: Definitions, Representation, and Pseudocode and more Cheat Sheet Computer Science in PDF only on Docsity!

Algorithms

Definition of Algorithm

An algorithm is an ordered set of

unambiguous , executable steps that

defines a (ideally) terminating process.

Algorithm Representation

  • Requires well-defined primitives
  • A collection of primitives that the computer can follow constitutes a programming language.

Folding a bird from a square piece of

paper

Pseudocode Primitives (continued)

• Repeated execution

while condition do activity

• Procedure (aka Method, Subroutine,

Function)

procedure name list of primitives associated with name

The procedure Greetings in

pseudocode

Running Example

  • You are running a marathon (26.2 miles) and would like to know what your finishing time will be if you run a particular pace. Most runners calculate pace in terms of minutes per mile. So for example, let’s say you can run at 7 minutes and 30 seconds per mile. Write a program that calculates the finishing time and outputs the answer in hours, minutes, and seconds.
  • Input: Distance : 26. PaceMinutes: 7 PaceSeconds: 30
  • Output: 3 hours, 16 minutes, 30 seconds

One possible solution

  • Express pace in terms of seconds per mile by multiplying the minutes by 60 and then add the seconds; call this SecsPerMile
  • Multiply SecsPerMile * 26.2 to get the total number of seconds to finish. Call this result TotalSeconds.
  • There are 60 seconds per minute and 60 minutes per hour, for a total of 60*60 = 3600 seconds per hour. If we divide TotalSeconds by 3600 and throw away the remainder, this is how many hours it takes to finish.
  • The remainder of TotalSeconds / 3600 gives us the number of seconds leftover after the hours have been accounted for. If we divide this value by 60, it gives us the number of minutes.
  • The remainder of ( the remainder of(TotalSeconds / 3600) / 60) gives us the number of seconds leftover after the hours and minutes are accounted for
  • Output the values we calculated!

Getting a Foot in the Door

  • Try working the problem backwards
  • Solve an easier related problem
    • Relax some of the problem constraints
    • Solve pieces of the problem first (bottom up methodology)
  • Stepwise refinement: Divide the problem into smaller problems (top-down methodology)

Ages of Children Problem

  • Person A is charged with the task of determining the ages of B’s three children. - B tells A that the product of the children’s ages is 36. - A replies that another clue is required. - B tells A the sum of the children’s ages. - A replies that another clue is needed. - B tells A that the oldest child plays the piano. - A tells B the ages of the three children.
  • How old are the three children?

Solution

Iterative Structures

  • Pretest loop:

while ( condition ) do ( loop body )

  • Posttest loop:

repeat ( loop body ) until( condition )

Components of repetitive control

Example: Sequential Search of a List

Fred

Alex

Diana

Byron

Carol

Want to see if Byron is in the list

The sequential search algorithm in

pseudocode

procedure Search(List, TargetValue) If (List is empty) Then (Target is not found) Else ( name  first entry in List while (no more names on the List) ( if (name = TargetValue) (Stop, Target Found) else name  next name in List ) (Target is not found) )

Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically

Insertion Sort: Moving to the right, insert each name in the proper sorted location to its left Fred Alex Diana Byron Carol

Applying our strategy to search a list for the entry John

Alice Bob Carol David Elaine Fred George Harry Irene John Kelly Larry Mary Nancy Oliver

A first draft of the binary search

technique

The binary search algorithm in

pseudocode

Searching for Bill

Applying the insertion sort in a worst-case situation

Graph of the worst-case analysis of the insertion sort algorithm

Graph of the worst-case analysis of the binary search algorithm

Software Verification

  • Proof of correctness
    • Assertions
      • Preconditions
      • Loop invariants
  • Testing

Solving the problem with only one cut