User Tools

Site Tools


computer_science:programming:algorithm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
computer_science:programming:algorithm [2021/09/25 05:25] – [Loop] manacomputer_science:programming:algorithm [2021/10/14 03:35] (current) – [Gauss Sum] mana
Line 3: Line 3:
 Pseudocode is not unique. That is to say that it does not have rigid rules. Different people may have different pseudocode syntax. Pseudocode is not unique. That is to say that it does not have rigid rules. Different people may have different pseudocode syntax.
  
 +===== Design =====
 +  * Step 1: Do something
 +  * Step 2: Do something
 +  * Step 3: Do something
 +  * ...   : ...
 +==== Sequential steps ====
 +A sequential step is a single task; for example ''%%a 1 to x%%''.
 +
 +==== Conditional steps ====
 +Ask a question that is answered with a logic answer. The answer can be ''%%true%%''/''%%false%%''.
 +For example:
 +<code>
 +if (x > 0) add 1 to x
 +else substract 1 from x
 +</code>
 +
 +==== Loops ====
 +Iterative steps.
 +<code>
 +add 1/2 cup to mixture
 +while mixture is dry
 +</code>
 +
 +==== Recursion ====
 +FIXME (This will be filled at a later point.)
 +
 +==== Case study: addition ====
 +Example: 
 +
 +<code>
 + 472
 ++593
 +</code>
 +
 +----
 +FIXME
 +Conceptualise:
 +<code>
 +Let m => 1 be number of digits
 +
 +a<sub>m-1</sub> a<sub>3</sub> a<sub>2</sub> a<sub>1</sub>
 +
 +=== Algorithm ===
 +<code>
 +Step 1: get m (user provides m)
 +Step 2: get a<sub>m-1</sub>・・・a<sub>1</sub> a<sub>0</sub>, b<sub>m-1</sub>・・・b<sub>1</sub>b<sub>0</sub>
 +Step 3: set i=0, set carry=0
 +Step 4: while(i ≤ m-1) do Step 5 to 7
 +Step 5:   set c<sub>i</sub> = a<sub>i</sub> + b<sub>i</sub> + carry
 +Step 6:   if(c<sub>i</sub> ≥ 10) then
 +            set c<sub>i</sub> = c<sub>i</sub> - 10
 +            set carry = 1
 +          else
 +            set carry = 0
 +Step 7:   set i = i + 1
 +Step 8: set c<sub>m</sub> = carry
 +step 9: print c<sub>m</sub> c<sub>m-1</sub> ... c<sub>2</sub> c<sub>1</sub> c<sub>1</sub>
 +step 10: stop
 +</code>
 ===== Squential statements ===== ===== Squential statements =====
   * Inputs: Get "variable"   * Inputs: Get "variable"
Line 22: Line 81:
 while(condition) while(condition)
 </code> </code>
 +
 +* ''Do while'' loops will execute at least once, online ''while'' loops
 +
 +===== Sequential Search =====
 +Data set of N elements: ''13'' ''4'' ''-20'' ''45'' ''112'' ・・・ ''N=10¹³ / 38944''
 +Therefore the list will be L<sub>1</sub>, L<sub>2</sub>, L<sub>3</sub>... L<sub>N</sub>
 +Target: 45 〇 130 ×
 +This is easy for the brain because the brain is made pattern matching.
 +
 +<code>
 +Step 1 get L1, L2, LN, N, Target
 +Step 2 Set Found = No
 +Step 3 Set i = 1
 +Step 4 while (Foundo = No AND i ≤ N) do Step 5 to Step 6
 +Step 5     if (Li = Target) then
 +               set Found = Yes
 +Step 6     else
 +               set i = i + 1
 +Step 7 if (Found = Yes) then
 +           print "Target found"
 +Step 8 else
 +           print "Target not found"
 +Step 9 stop
 +</code>
 +
 +===== The Swap =====
 +x y
 +5 3
 +Swap the content of both variable so that X=3 and Y=5
 +<code>
 +Step 1 get x, y
 +Step 2 set temp = x
 +Step 3 x = y
 +Step 4 y = temp
 +Stop 5 Stop
 +</code>
 +
 +* For swapping in other algorithm ''swap(x,y)'' can be used instead.
 +
 +===== Gauss Sum =====
 +n ≥ 1 
 +1+2+3+4+...+n
 +
 +Formula: $\frac{(n+1)*n}{2}$  $\Theta$
 +FIXME (double check this information)
 +When talking about efficiency we say that the efficiency of n is $\Theta(n^2)$
 +
computer_science/programming/algorithm.1632515103.txt.gz · Last modified: 2021/09/25 05:25 by mana