1 / 19

C021TV-I2-S3

Learn about elementary instructions in algorithmic tasks, including variable assignments, testing conditions, and loops. Understand the concepts of variables, loops, and sequences in algorithms. Explore examples such as binary search.

ceciliam
Download Presentation

C021TV-I2-S3

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. C021TV-I2-S3 0

  2. I.2Algorithmique Tasks Variables Elementary instructions Algorithmic culture SylvieBoldo 1

  3. Elementary instructions: assignment variable ←value We change a variable’s value. ⇒ i ←17 ⇒ i ←i+1

  4. Elementary instructions:test ifBooleantheninstructionsif notinstruction ⇒ Is one value ≥ 10? ⇒ Is a calculationlikely to exceedthe maximum/minimal values of the variable’s type? ⇒ Is itlikelythat I willbebeyond the chart?

  5. Elementary instructions: thesequence • instruction ;instruction • For example: • assignment, thentest • assignment, thenloop, thentest • assignment, thenassignment, thenassignment, thenassignment

  6. Elementary instructions: the loop The for loop: forvariable fromvaluetovalueDoinstructionn_for ⇒ browse a chart (search for a maximum, calculate an average, filling a chart. . .) The as long as loop(more general) : Aslongasbooleandoinstructionn_aslongas ⇒ convergent sequence ⇒ non boundediteration(terminationproblem)

  7. Instructions ‹→algorithms • Take the desiredamount: • theséquence • thetest • The loop • Variable assignment • Mix well • You have all the algorithms in the world!

  8. Example: binarysearch Entries/outputs Entries: classified chart t of integers, of a size n > 0 and one valuev Outputs: booleansaying if v appears in t inf ← 0; sup ← n−1; aslongas(inf ≤ sup)do i ←(inf+sup)/2; if(t[i] = v) thensendVRAI; if not if(t[i] > v) thensup ←i−1; If notinf ←i+1; n_if n_if n_aslongas sendFALSE;

  9. Example: binarysearch I search for 5. 8

  10. Example : binarysearch I search for 5. ↑ ↑ inf sup 8

  11. Example : binarysearch I search for 5. ↑ ↑ ↑ inf sup i 8

  12. Example : binarysearch I search for 5. ↑ ↑ inf sup 8

  13. Example : binarysearch I search for 5. ↑ ↑ ↑ inf sup i 8

  14. Example : binarysearch I search for 5. ↑ ↑ inf sup 8

  15. Example : binarysearch I search for 5. ↑ ↑ inf,i sup 8

  16. Example : binarysearch I search for 5. ↑ inf,i,sup 8

  17. Example : binarysearch I search for 5. ↑ ↑ sup inf 8

  18. Example : binarysearch I search for 5. ↑ ↑ sup inf donc 5 n'est pas dans letableau. 8

  19. Example: binarysearch I search for 5. ↑ ↑ sup inf so5 is not in the chart. In fact(inf+sup)/2 shouldbeinf+(sup-inf)/2. 8

More Related