0 views

Uploaded by Ñëhâl Khäñ

www.vukhi.com.pk
paper of midterm spring 2019

- THE POWER OF TEN - Math in the Parsha
- part1.1-3.pdf
- B2 M05 2011.11.29 Digital Techniques
- Finite Mathematics
- ch-2
- U-3 Sorting
- Bubble Search - A Simple Heuristic for Improving Priority-based Greedy Algorithms
- Pseudo Code
- MELJUN CORTES StudyGuide TOC ASSEMBLY LANGAUGE
- Math in Computers
- Dasar Pemrograman 4
- Structured Programming
- Towards efficient techniques to discover moving flock patterns: How can association rules help?
- Self-Learning Archetypes for Rasterization
- A Flowchart to generate Code
- MIT6_006F11_lec01.pdf
- Scimakelatex.56972.Raymond+Sheppard
- Scimakelatex.27812.None
- 1 Introduction
- CC 113 a the Computer Number Systems

You are on page 1of 56

Lecture 17

Algorithms II

1

Focus of the last lecture was on

Algorithms

Became familiar with the concept of algorithms:

– What is their use?

– What are their types?

– What are the techniques used for

representing them?

• Pseudo code

• Flowcharts

• Actual code 2

Today …

• We will continue our discussion on algorithms

that we started during the 16th lecture

blocks that are used in all algorithms

flowcharts for particular problems

of those two techniques 3

Algorithm Building Blocks

All problems can be solved by employing

any one of the following building blocks or

their combinations

1. Sequences

2. Conditionals

3. Loops

4

Start or stop

Process

Flowchart

Decision

Elements

Flow line

Connector

Off-page connector 5

This review was essential because we we

will be using these building blocks quite

often today.

blocks of algorithms. First ..

6

Sequences

A sequence of instructions that are executed

in the precise order they are written in:

statement block 1

statement block 1

statement block 2 statement block 2

statement block 3

statement block 3

7

Conditionals

Select between alternate courses of action

depending upon the evaluation of a condition

If ( condition = true )

statement block 1 True False

Else condition

statement block 2

End if statement statement

block 1 block 2

8

Loops

Loop through a set of statements as long as a

condition is true

statement block

End Loop

statement True

condition

block

False

9

We will now present the algorithm for a

problem whose solution is familiar to us

statement and then present the algorithm

in three different formats:

1. Pseudo code

2. Flowchart

3. Actual code

10

Problem Statement

Convert a decimal number into binary

11

Convert 75 to Binary

2 75 remainder

2 37 1

2 18 1

2 9 0

2 4 1

2 2 0

2 1 0

0 1

1001011 12

We did write down the pseudo code for

this problem last time

formal way

13

Solution in Pseudo Code

1. Let the decimal number be an integer x, x > 0

2. Let the binary equivalent be an empty string y

3. Repeat while x > 0 {

Determine the quotient & remainder of x ÷ 2

y = CONCATENATE( remainder, y )

x = quotient

}

4. Print y

5. Stop 14

Q: Is this the only possible algorithm for

converting a decimal number into a binary

representation?

In terms of speed?

In terms of memory requirement?

In terms of ease of implementation?

writing any algorithm!

15

Tips on Writing Good Pseudo Code

• Use indention for improved clarity

• Do not put “code” in pseudo code – make your

pseudo code language independent

• Don’t write pseudo code for yourself – write it

in an unambiguous fashion so that anyone with

a reasonable knowledge can understand and

implement it

• Be consistent

• Prefer formulas over English language

16

descriptions

Start Flowchart of Decimal

to Binary Conversion

Get x

x>0 ? & remainder

of x ÷ 2 x = quotient

No

Print y

x is the decimal number

Stop y is the binary equivalent

17

1. Does the flowchart depict the “correct”

algorithm?

2. What do we mean by “correct”, or better

yet, what do we check for “correctness”?

3. One way is to check the algorithm for a

variety of inputs

4. Does it perform satisfactorily for:

• x=0?

• negative numbers?

• numbers with fractional parts? 18

Decimal to Binary Conversion in JavaScript

<SCRIPT>

x = 75; // x is the decimal number

y = “”; // y is the binary equivalent

while ( x > 0) {

remainder = x % 2;

quotient = Math.floor( x / 2 );

y = remainder + y; NOTE: Don’t

x = quotient; worry if you

} don’t

understand this

document.write(“y = ” + y);

code for now;

</SCRIPT> 19

you will - later!

Another Example: Sorting

Sort the following objects w.r.t. their heights

20

Expected Result

21

Strategy

There are many strategies for solving this

problem. We demonstrate a simple one:

Swap it with the one next to it if they are in the wrong order

Keep on repeating until you reach the last object in the list

22

Back to the Objects to be Sorted

23

Q: Is the list sorted?

A: No

24

Sorting: Step A1

25

Sorting: Step A1

Swap? Yes

26

Sorting: Step A2

27

Sorting: Step A2

Swap? Yes

28

Sorting: Step A3

29

Sorting: Step A3

Swap? No

30

Sorting: After Step A7

31

Q: Is the list sorted?

A: No

32

Sorting: Step B1

33

Sorting: Step B1

Swap? Yes

34

Sorting: Step B2

35

Sorting: Step B2

Swap? No

36

Sorting: After Step B7

37

Q: Is the list sorted?

A: No

38

Sorting: Step C1

39

Sorting: Step C1

Swap? No

40

Sorting: After Step C7

41

Q: Is the list sorted?

A: Yes

42

43

Let’s now look at this same process of

sorting being applied to a bigger list

SORT GOES HERE---

44

Start Flowchart for the Sorting Process

list is an array containing the heights

Get list N is the total number of objects in the list

Yes No

n>N ?

n = n+1

n=0

sorted? list[n+1]?

Yes Yes

SWAP

45

Stop list[n], list[n+1]

Start

Get list

Yes No

n>N ?

n = n+1

n=0

sorted? list[n+1]?

Yes Yes

SWAP

46

Stop list[n], list[n+1]

Dim swapFlag As Boolean, list(8) As Integer

readList( list() ) ‘this needs to be defined

swapFlag = True

Do While swapFlag = True

For n = 1 To 8

If list(n) > list(n + 1) Then

temp = list(n)

list(n) = list(n + 1)

list(n + 1) = temp

swapFlag = True

End If

Next VisualBasic Code for

Loop the Sorting Function

For n = 1 To 8

Debug.Print list(n) NOTE: Don’t worry if you

47

Next don’t understand this code

Q: Is this the only possible algorithm for

sorting a list?

the “Bubble sort”) is probably the worst

(reasonable) algorithm for sorting a list – it

is just too slow

your future courses

48

Pros and Cons of Flowcharts (1)

• I personally don’t find flowcharts very useful

of a flowchart is just too cumbersome

code is not straight forward

called Structured Flowcharts – that may be

better suited for software developers 49

Pros and Cons of Flowcharts (2)

• The good thing about flowcharts is that their

symbols are quite intuitive and almost

universally understood

explaining an algorithm to one’s peers quite

straightforward

50

Pros and Cons of Pseudo Code (1)

• Quite suitable for SW development as it is

closer in form to real code

starting point or outline for writing real code

and then incrementally comment each line out

while converting that line into real code

51

Pros and Cons of Pseudo Code (2)

• Pseudo code can be constructed quite quickly

as compared with a flowchart

writing pseudo code

52

With that we have reached the end of the

materials that we wanted to cover today.

assignment #6

53

Assignment # 6

There are many algorithms for sorting a list; Bubble

sort – the sorting algorithm discussed today in - is just

one example.

the flowchart for a sorting algorithm other than the

“Bubble sort”.

for an algorithm or come up with a scheme on your

own.

submission instructions & deadline 54

In Today’s Lecture, We …

• We continued our discussion on algorithms

that we had started during the 16th lecture

that are used in all algorithms

flowcharts for particular problems

those two techniques 55

Focus of the Next Lecture:

Programming Languages

languages in computing

high-level, interpreted & compiled, and

structured & object-oriented programming

languages

56

- THE POWER OF TEN - Math in the ParshaUploaded byRabbi Benyomin Hoffman
- part1.1-3.pdfUploaded byJeetendra Kumar
- B2 M05 2011.11.29 Digital TechniquesUploaded byPilot Tech 918
- Finite MathematicsUploaded byHaman Salem
- ch-2Uploaded bypankajusb
- U-3 SortingUploaded bySharron Smilee
- Bubble Search - A Simple Heuristic for Improving Priority-based Greedy AlgorithmsUploaded byluism321
- Pseudo CodeUploaded bylabstudent
- MELJUN CORTES StudyGuide TOC ASSEMBLY LANGAUGEUploaded byMELJUN CORTES, MBA,MPA
- Math in ComputersUploaded byL'Homme Révolté
- Dasar Pemrograman 4Uploaded byJulen K Simatupang
- Structured ProgrammingUploaded byujjwol
- Towards efficient techniques to discover moving flock patterns: How can association rules help?Uploaded byandresscalderon
- Self-Learning Archetypes for RasterizationUploaded byajitkk79
- A Flowchart to generate CodeUploaded byshu2u
- MIT6_006F11_lec01.pdfUploaded byAnjali Ghosh
- Scimakelatex.56972.Raymond+SheppardUploaded byd_lumsdon
- Scimakelatex.27812.NoneUploaded bydsogh
- 1 IntroductionUploaded bySridhar Dv
- CC 113 a the Computer Number SystemsUploaded byRaymark Timothy
- numSystems.docUploaded bybus9
- Guidelines for Programming Assignment 3_revised (1).pdfUploaded byMoazzam Hussain
- Practice Quiz1Uploaded bysyrissco123
- PramodUploaded byKrishan Kant Shukla
- Chapter 1 Introduction to Digital Techniques1 (1)Uploaded byAlluda Shende
- Binary LogicUploaded byartmaker43
- MHA CET SyllabusUploaded byAnonymous YBrT6DoNhS
- lec8Uploaded byraw.junk
- 06_HeapsortUploaded bypakku009
- PV coupling.pdfUploaded byWalter Henderson

- Hypermorph TutorialUploaded byChrissy Watson
- A ZEOS Basics Tutorial Not Only for FirebirdUploaded byRolly Ar
- jutaan-umkm-pahlawan-pajak-urus-pajak-itu-sangat-mudah-indonesian-edition-by-chandra-budi.pdfUploaded byNAJA ASOKA
- Details of 8086 KitsUploaded bykiranmai
- 463-1429-1-PBUploaded byYuri Adixs Ucux'sae
- Project 2nd 1Uploaded bydatdraku
- Factoring TrinomialsUploaded byRam Singh
- Practice Test 20-Logical AptitudeUploaded bypd1995
- STAAD Pro V8iUploaded byVivek Gosavi
- mat-f90Uploaded byKevin Devalentino Versi II
- Animal Model in R_Example MrodeUploaded bymarcoantonioramoscar
- Pre Assess Report 2692505Uploaded byAnuj Tiwari
- Clean, Secure and Fast IOSUploaded byVladimir Petrovski
- Mobile Based Attendance System Using QR CodeUploaded byWorld of Computer Science and Information Technology Journal
- A Hybrid Particle Swarm Optimization Algorithm to Human Skin Region DetectionUploaded byJournal of Computing
- Nokia E5 Data SheetUploaded bymobiFlip.de
- DFL-WDII Version 2.4, Newer Height In WD HDD Repair & Data Recovery.pdfUploaded byStanley Morgan
- Homework 2 Heat TransferUploaded byGonzalo Salfate Maldonado
- 10. Development of Fingerprint Biometric Attendance System for Non-Academic Staff in a Tertiary Institution.pdfUploaded byimdad ali
- Upgrade to Skype for Business 2015 Step by Step eBookUploaded byzawmoetun
- OAFUploaded byRaj
- Digital LabmanualUploaded byRaji Sharmi
- EDPDAUploaded byVishal Merchant
- CUPL ReferenceUploaded byHeraclio ßernal
- Basics of Document Management & InstallationUploaded bysid_chh26
- IBM Tape Device DriversUploaded bykadhiresan_pm5283
- Ansys Design Experiments Usinf AutoDOEUploaded byjohn_max03
- Conceptual dependency in Artificial intelligenceUploaded byKrishanaKantPareek
- Installation of MQUploaded bynandoooo86
- Number StoryUploaded bybigstrand4476