190 likes | 212 Views
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.
E N D
I.2Algorithmique Tasks Variables Elementary instructions Algorithmic culture SylvieBoldo 1
Elementary instructions: assignment variable ←value We change a variable’s value. ⇒ i ←17 ⇒ i ←i+1
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?
Elementary instructions: thesequence • instruction ;instruction • For example: • assignment, thentest • assignment, thenloop, thentest • assignment, thenassignment, thenassignment, thenassignment
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)
Instructions ‹→algorithms • Take the desiredamount: • theséquence • thetest • The loop • Variable assignment • Mix well • You have all the algorithms in the world!
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;
Example: binarysearch I search for 5. 8
Example : binarysearch I search for 5. ↑ ↑ inf sup 8
Example : binarysearch I search for 5. ↑ ↑ ↑ inf sup i 8
Example : binarysearch I search for 5. ↑ ↑ inf sup 8
Example : binarysearch I search for 5. ↑ ↑ ↑ inf sup i 8
Example : binarysearch I search for 5. ↑ ↑ inf sup 8
Example : binarysearch I search for 5. ↑ ↑ inf,i sup 8
Example : binarysearch I search for 5. ↑ inf,i,sup 8
Example : binarysearch I search for 5. ↑ ↑ sup inf 8
Example : binarysearch I search for 5. ↑ ↑ sup inf donc 5 n'est pas dans letableau. 8
Example: binarysearch I search for 5. ↑ ↑ sup inf so5 is not in the chart. In fact(inf+sup)/2 shouldbeinf+(sup-inf)/2. 8