Page 67 - 2502_Pakistan-kifayat_C-8
P. 67

We ask questions like:
                 1.  Which algorithm finishes the task faster?

                 2.  Which uses less memory?

                 3.  Which one is easier to understand?
                 4.  Which one works better when the problem gets bigger?

                 Example: Sorting a pile of cards from smallest to largest.
                    Algorithm 1: Check each card and swap if needed, over and over (slow but simple).

                    Algorithm 2: Divide cards into smaller piles, sort each pile, then combine (faster but a bit harder).
                 If you have only a few cards, Algorithm 1 is acceptable. However, if you have lots of cards, Algorithm 2
                 is better because it’s faster.


                      MULTIPLE SOLUTIONS OF THE SAME PROBLEM


                 In real life and in computers, there can be more than one way to solve a problem. In algorithm design,
                 multiple solutions refer to the different ways a single problem can be solved using different techniques
                 or approaches. Each solution may vary in efficiency, readability, scalability, and complexity.
                 Even if the goal is the same, each solution might:

                    Use different steps,

                    Take different amounts of time,
                    Need more or less memory (space),

                    Be easy or hard to understand.

                 For example, to find the greatest number among three numbers.
                 Solution 1: Using If-Else Statements (Direct Comparison)

                 Step 1:  Start

                 Step 2:  Input three numbers: A, B, and C.
                 Step 3:  If A > B and A > C, then A is the greatest.

                 Step 4:  Else if B > A and B > C, then B is the greatest.
                 Step 5:  Else, C is the greatest.

                 Step 6:  Print the greatest number.
                 Step 7:  Stop

                 Solution 2: Using Temporary Variable (Step-by-Step Comparison)

                 Step 1:  Start
                 Step 2:  Input three numbers: A, B, and C.

                 Step 3:  Set Max = A.
                 Step 4:  If B > Max, then set Max = B.

                 Step 5:  If C > Max, then set Max = C.


                                                                                          #Solving Complex Problems  65
   62   63   64   65   66   67   68   69   70   71   72