Top Banner
Chapter 8: Algorithms
57

Chapter 8: Algorithms

Feb 27, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 8: Algorithms

Chapter 8: Algorithms

Page 2: Chapter 8: Algorithms

�������� � ��� ��������

� ��������

� ����������� �����

Page 3: Chapter 8: Algorithms

CONCEPTCONCEPTCONCEPT

8.18.1

Page 4: Chapter 8: Algorithms

!

IInformal definitionnformal definition

� Informally, an algorithm is a step-by-step method for solving a problem or doing a task.

�An algorithm accepts an input list of data and creates an output list of data.

Page 5: Chapter 8: Algorithms

EExamplexample

The algorithm uses the following five steps to find the largest integer.

Page 6: Chapter 8: Algorithms

"

DDefining actions in efining actions in FindLargestFindLargest algorithmalgorithm

Page 7: Chapter 8: Algorithms

RRefinementefinement

Page 8: Chapter 8: Algorithms

#

GGeneralizationeneralization

Page 9: Chapter 8: Algorithms

THREE CONSTRUCTSTHREE CONSTRUCTSTHREE CONSTRUCTS

8.28.2

Page 10: Chapter 8: Algorithms

�$

TThree constructshree constructs

�A program is a combination of sequence constructs, decision constructs, and repetition constructs.

Page 11: Chapter 8: Algorithms

��

ALGORITHMREPRESENTATION

ALGORITHMALGORITHMREPRESENTATIONREPRESENTATION

8.38.3

Page 12: Chapter 8: Algorithms

��

FFlowcharts for three constructslowcharts for three constructs

�A flowchart is a pictorial representation of an algorithm.

Page 13: Chapter 8: Algorithms

�%%�&'�( )* +��,����

SYMBOL NAME APPLICATION

n

Terminal

Flow Lines

Connector

Shows the beginning or end of an algorithm

Show the action order in an algorithm

Shows the continuity of the algorithm on the next page

Page 14: Chapter 8: Algorithms

�!

�-�.- �&' �-/�

START

STOP

Page 15: Chapter 8: Algorithms

��

)�&&�����

START

1

1

STOP

Page 16: Chapter 8: Algorithms

�"

��0��&�� �1�2���

Assignment statement

Input/output statement

Module call

Compound statement

Page 17: Chapter 8: Algorithms

��

�����&��& �����&

variable � expression

Page 18: Chapter 8: Algorithms

�#

3�'����)��� �����&

START

STOP

AVRG

AVRG

RETURN

Page 19: Chapter 8: Algorithms

��

-,����1 �������&

Condition

True StatementFalse Statement

F T

Page 20: Chapter 8: Algorithms

�$

4�� 5��%

End of loop action

Initialization

Limit test

T

F

Page 21: Chapter 8: Algorithms

��

PPseudocodeseudocode for three constructsfor three constructs

�Pseudocode is an Englishlike representation of an algorithm.

Page 22: Chapter 8: Algorithms

��

Example 1Example 1

Write an algorithm in pseudocode that finds the average of two numbers

SolutionSolution

See Algorithm 8.1 on the next slide.

Page 23: Chapter 8: Algorithms

AverageOfTwoInput: Two numbers

1. Add the two numbers2. Divide the result by 23. Return the result of Step 2

End

Algorithm 8.1:Algorithm 8.1: Average of twoAverage of twoSTART

Sum = X + Y

Input X, Y

Avg = Sum / 2

Output Avg

End

Page 24: Chapter 8: Algorithms

�!

Example 5Example 5

Write an algorithm to find the largest of 1000 numbers.

SolutionSolution

See Algorithm 8.5 on the next slides.

Page 25: Chapter 8: Algorithms

��

+��, )��START

Largest = 0 i = 0

Input 1000 integers

Largest = integer[i]

Output Largest

End

While i < 1000

integer[i] > Largest

T

T

F

F

i = i + 1

Page 26: Chapter 8: Algorithms

�"

FindLargestInput: 1000 positive integers

1. Set Largest to 02. Set Counter to 03. while (Counter less than 1000)

3.1 if (the integer is greater than Largest)then3.1.1 Set Largest to the value of the integer

End if3.2 Increment Counter

End while4. Return Largest

End

Algorithm 8.5:Algorithm 8.5: Find largest of 1000 numbersFind largest of 1000 numbers

Page 27: Chapter 8: Algorithms

��

MORE FORMALDEFINITION

MORE FORMALMORE FORMALDEFINITIONDEFINITION

8.48.4

FFormally, an algorithm is an ordered set of ormally, an algorithm is an ordered set of unambiguous steps that produces a result and unambiguous steps that produces a result and terminates in a finite time.terminates in a finite time.

Page 28: Chapter 8: Algorithms

�#

SUBALGORITHMSSUBALGORITHMSSUBALGORITHMS

8.58.5

Page 29: Chapter 8: Algorithms

��

CConcept of a oncept of a subalgorithmsubalgorithm�An algorithm can be broken into smaller units called

subalgorithms.

START

X, Y

i=gcd(X,Y)

i=1

“They are mutual prime.”

“Greatest common divider = “ , i

END

gcd(a, b)

RETURN

.

.

.

Page 30: Chapter 8: Algorithms

$

BASICALGORITHMS

BASICBASICALGORITHMSALGORITHMS

8.68.6

Page 31: Chapter 8: Algorithms

SSummationummation

Page 32: Chapter 8: Algorithms

BBubble sortubble sort

Page 33: Chapter 8: Algorithms

EExample of bubble sortxample of bubble sort

8 45

Page 34: Chapter 8: Algorithms

!

EExample of bubble sortxample of bubble sort

8 458 78

Page 35: Chapter 8: Algorithms

EExample of bubble sortxample of bubble sort

8 458 78823

The smallest number is moved to the head.

Page 36: Chapter 8: Algorithms

"

EExample of bubble sortxample of bubble sort

4532

Page 37: Chapter 8: Algorithms

EExample of bubble sortxample of bubble sort

45327832

Page 38: Chapter 8: Algorithms

#

EExample of bubble sortxample of bubble sort

45327832

The second smallest number

Page 39: Chapter 8: Algorithms

EExample of bubble sortxample of bubble sort

Page 40: Chapter 8: Algorithms

!$

EExample of bubble sortxample of bubble sort

And so on …

Page 41: Chapter 8: Algorithms

!�

SSearch conceptearch concept

�Searching, a process to locate a target in a list of data, is a basic algorithm.

�Sequential search is used for unordered lists.

�Binary search is used for ordered lists.

Page 42: Chapter 8: Algorithms

!�

EExample of a sequential searchxample of a sequential search

Page 43: Chapter 8: Algorithms

!

EExample of a sequential searchxample of a sequential search

Page 44: Chapter 8: Algorithms

!!

EExample of xample of a binary searcha binary search Target: 22

Page 45: Chapter 8: Algorithms

!�

Chapter 9:Programming

Languages

Page 46: Chapter 8: Algorithms

!"

EVOLUTIONEVOLUTIONEVOLUTION

9.19.1

Page 47: Chapter 8: Algorithms

!�

EEvolution of computer languagesvolution of computer languages

Page 48: Chapter 8: Algorithms

!#

00000000 00000100 000000000000000001011110 00001100 11000010 0000000000000010

11101111 00010110 000000000000010111101111 10011110 0000000000001011

11111000 10101101 11011111 000000000001001001100010 11011111 0000000000010101

11101111 00000010 11111011 000000000001011111110100 10101101 11011111 000000000001111000000011 10100010 11011111 000000000010000111101111 00000010 11111011 000000000010010001111110 11110100 1010110111111000 10101110 11000101 000000000010101100000110 10100010 11111011 000000000011000111101111 00000010 11111011 0000000000110100

00000100 000000000011110100000100 0000000000111101

Program 9.1Program 9.1 Program in machine languageProgram in machine language

1122334455667788991010111112121313141415151616

Page 49: Chapter 8: Algorithms

!�

The only language understood byThe only language understood bya computer is machine language.a computer is machine language.

Note:Note:

Page 50: Chapter 8: Algorithms

�$

�A symbolic languageuses symbols to represent various machine language instructions. Symbolic languages are also called assembly languages.

�A high-level language is portable from one computer type to another and free the programmer from one computer type to another and frees theprogrammer from hardware concerns. BASIC, Pascal, Ada, C, C++, and Java are high-level languages.

�Natural language

EEvolution of computer languagesvolution of computer languages

Page 51: Chapter 8: Algorithms

��

Entry main, ^m<r2>subl2 #12,spjsb C$MAIN_ARGSmovab $CHAR_STRING_CON

pushal -8(fp)pushal (r2)calls #2,readpushal -12(fp)pushal 3(r2)calls #2,readmull3 -8(fp),-12(fp),-pushal 6(r2)calls #2,printclrl r0ret

Program 9.2Program 9.2 Program in symbolic languageProgram in symbolic language

1122334455667788991010111112121313141415151616

Page 52: Chapter 8: Algorithms

��

/* This program reads two integer numbers from the keyboard and prints their product.

*/ #include <iostream.h>

int main (void){// Local Declarations

int number1;int number2;int result;

// Statementscin >> number1;cin >> number2;result = number1 * number2;cout << result;return 0;

} // main

Program 9.3Program 9.3 Program in C++ languageProgram in C++ language

112233445566778899101011111212131314141515161617171818

Page 53: Chapter 8: Algorithms

BUILDINGA

PROGRAM

BUILDINGBUILDINGAA

PROGRAMPROGRAM

9.29.2

Page 54: Chapter 8: Algorithms

�!

BBuilding a programuilding a program

�The steps to building a program include writing, editing, compiling, and linking code.

Page 55: Chapter 8: Algorithms

��

PROGRAMEXECUTIONPROGRAMPROGRAM

EXECUTIONEXECUTION

9.39.3

Page 56: Chapter 8: Algorithms

�"

PProgram executionrogram execution

Page 57: Chapter 8: Algorithms

��

3�'�-��� 6(��

� Date: 10/29 (Wednesday)� Time: 14:10-17:00

� Scope: Chapter 1,2,3,4,5,8,9

� Close Book� You have seen all the English vocabularies in the textbook

or the homework, so do not ask the TA to explain the questions.