1 / 9

Algorithms

Algorithms. What is an algorithm?. An algorithm is a clear, concise, and correct step-by-step sequence of actions used to solve a problem or set of problems. Example 1 Problem. Write an algorithm that describes how someone is to sort a standard deck of cards from top to bottom as follows

kioko
Download Presentation

Algorithms

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

  2. What is an algorithm? An algorithm is a clear, concise, and correct step-by-step sequence of actions used to solve a problem or set of problems

  3. Example 1 Problem • Write an algorithm that describes how someone is to sort a standard deck of cards from top to bottom as follows • 2 of clubs through the ace of clubs • 2 of diamonds through the ace of diamonds • 2 of hearts through the ace of hearts • 2 of spades through the ace of spades

  4. Example 1 Solution choosing the top card of the stack in turn, make 4 new stacks of 13 cards, one stack for each suit with each of these four stacks, order the cards 2 through 10, jack, queen, king, ace combine the three stacks in this order: club, diamond, heart, and spade

  5. Example 2 Problem Sorting a stack of papers, each with a number on it

  6. Example 2 Solution Compare the top two papers of the stack; place the smaller valued paper on the bottom of the stack and keep the larger valued paper Grab the next paper off of the top of the stack and compare it to the paper you kept; place the smaller of these two on the bottom of the stack and keep the larger Repeat step 2 until you have reached the (original)bottom of the stack; you should now be holding the largest valued paper in the stack; set this paper aside into a new “sorted stack” Repeat steps 1, 2 & 3, placing the paper from step 3 onto the “sorted stack”, until no more papers remain

  7. Algorithm Exercises • Write an algorithm for… • Tying your shoes • Telling time from an analogue clock • Helping 5 of your friends to find a lost key on a soccer field

  8. Applying algorithms to your coding problems Control Flow Charts - Some programmers prefer to develop drawings showing how information passes from state to state in a solution. Pseudocode– You may develop your own pseudocode language containing all the usual coding constructs of high-level languages and use it to write algorithms to solve your problem.

  9. End of Session

More Related