1 / 56

CS101 Introduction to Computing Lecture 17 Algorithms II

CS101 Introduction to Computing Lecture 17 Algorithms II. Focus of the last lecture was on Algorithms. Became familiar with the concept of algorithms: What they are? (SEQUENCE OF STEPS) What is their use ? What are their types? What are the techniques used for representing them?

ceduardo
Download Presentation

CS101 Introduction to Computing Lecture 17 Algorithms II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS101 Introduction to ComputingLecture 17Algorithms II

  2. Focus of the last lecture was on Algorithms Became familiar with the concept of algorithms: • What they are? (SEQUENCE OF STEPS) • What is their use? • What are their types? • What are the techniques used for representing them? • Pseudo code • Flowcharts • Actual code

  3. Today … • We will continue our discussion on algorithms that we started during the 16th lecture • In particular, we will look at the building blocks that are used in all algorithms • We will also discuss the pseudo code and flowcharts for particular problems • In addition, we will outline the pros and cons of those two techniques

  4. Algorithm Building Blocks All problems can be solved by employing any one of the following building blocks or their combinations • Sequences • Conditionals • Loops

  5. Review of Flowchart Elements Start or stop Process Input or output Decision Flow line Connector Off-page connector

  6. This review was essential because we we will be using these building blocks quite often today.OK. Now on with the three building blocks of algorithms. First ..

  7. Sequences A sequence of instructions that are executed in the precise order they are written in: statement block 1 statement block 2 statement block 3 statement block 1 statement block 2 statement block 3

  8. Conditionals Select between alternate courses of action depending upon the evaluation of a condition If ( condition = true ) statement block 1 Else statement block 2 End if condition True False statement block 1 statement block 2

  9. Loops Loop through a set of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop condition True statement block False

  10. We will now present the algorithm for a problem whose solution is familiar to usWe will first go through the problem statement and then present the algorithm in three different formats: 1. Pseudo code 2. Flowchart 3. Actual code

  11. Problem Statement Convert a decimal number into binary

  12. 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

  13. We did write down the pseudo code for this problem last timeLets do it again, and in a slightly more formal way

  14. Solution in Pseudo Code • Let the decimal number be an integer x, x > 0 • Let the binary equivalent be an empty string y • Repeat while x > 0 { Determine the quotient & remainder of x÷ 2 y = CONCATENATE( remainder, y ) x = quotient } • Printy • Stop

  15. Q: Is this the only possible algorithm for converting a decimal number into a binary representation? If not, then is this the best?In terms of speed?In terms of memory requirement?In terms of ease of implementation? You must ask these questions after writing any algorithm!

  16. 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 descriptions

  17. Flowchart of Decimal to Binary Conversion Start Get x Find quotient & remainder of x÷ 2 y = CONC(remainder, x) x = quotient Yes x>0 ? No Print y x is the decimal number y is the binary equivalent Stop

  18. Does the flowchart depict the “correct” algorithm? • What do we mean by “correct”, or better yet, what do we check for “correctness”? • One way is to check the algorithm for a variety of inputs • Does it perform satisfactorily for: • x = 0 ? • negative numbers? • numbers with fractional parts?

  19. 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; x = quotient; } document.write(“y = ” + y); </SCRIPT> NOTE: Don’t worry if you don’t understand this code for now; you will - later!

  20. Another Example: Sorting Sort the following objects w.r.t. their heights

  21. Expected Result

  22. Strategy There are many strategies for solving this problem. We demonstrate a simple one: Repeat the following steps while the list is un-sorted: Start with the first object in the list Swap it with the one next to it if they are in the wrong order Repeat the same with the next to the first object Keep on repeating until you reach the last object in the list

  23. Back to the Objects to be Sorted

  24. Q: Is the list sorted?A: No

  25. Sorting: Step A1

  26. Sorting: Step A1 Swap? Yes

  27. Sorting: Step A2

  28. Sorting: Step A2 Swap? Yes

  29. Sorting: Step A3

  30. Sorting: Step A3 Swap? No

  31. Sorting: After Step A7

  32. Q: Is the list sorted?A: No

  33. Sorting: Step B1

  34. Sorting: Step B1 Swap? Yes

  35. Sorting: Step B2

  36. Sorting: Step B2 Swap? No

  37. Sorting: After Step B7

  38. Q: Is the list sorted?A: No

  39. Sorting: Step C1

  40. Sorting: Step C1 Swap? No

  41. Sorting: After Step C7

  42. Q: Is the list sorted?A: Yes

  43. STOP

  44. Let’s now look at this same process of sorting being applied to a bigger list---FLASH MOVIE FOR BUBBLE SORT GOES HERE---

  45. Flowchart for the Sorting Process Start list is an array containing the heights N is the total number of objects in the list Get list Yes No n>N ? n = n+1 list sorted? list[n] > list[n+1]? No No n = 0 Yes Yes SWAP list[n], list[n+1] Stop

  46. Start Get list Yes No n>N ? n = n+1 list sorted? list[n] > list[n+1]? No No n = 0 Yes Yes SWAP list[n], list[n+1] Stop

  47. 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 Loop For n = 1 To 8 Debug.Print list(n) Next VisualBasic Code for the Sorting Function NOTE: Don’t worry if you don’t understand this code

  48. Q: Is this the only possible algorithm for sorting a list? A: Certainly not! In fact this one (called the “Bubble sort”) is probably the worst (reasonable) algorithm for sorting a list – it is just too slow You will learn a lot more about sorting in your future courses

  49. Pros and Cons of Flowcharts (1) • I personally don’t find flowcharts very useful • The process of writing an algorithm in the form of a flowchart is just too cumbersome • And then converting this graphical form into code is not straight forward • However, there is another kind of flowcharts – called Structured Flowcharts – that may be better suited for software developers

  50. Pros and Cons of Flowcharts (2) • The good thing about flowcharts is that their symbols are quite intuitive and almost universally understood • Their graphical nature makes the process of explaining an algorithm to one’s peers quite straightforward

More Related