Chapter 2
Chapter 2
Computational thinking is a thought process that helps us to understand the problem and solve it as a
computer can do. A computer works on the input-process-output model.
a) Decomposition
b) Pattern Recognition
c) Abstraction
d) Algorithm
a) Decomposition:
The process of breaking down a complex problem into smaller and more manageable tasks/sub problems is
called decomposition. Decomposition makes easier to understand and solve each part of the problem
individually.
Example:
b) Pattern Recognition:
The Process of identifying patterns, and trends within data or problems. Recognizing these patterns helps in
predicting outcomes and making informed decisions.
Example:
• Temperature
• Humidity
• atmospheric pressure
By using the above data Meteorologists can forecast events like storms, heatwaves, and rainfall.
Prof. Muhammad Zubair Zaib
c) Abstraction:
The process of ignoring unnecessary details and focusing on the most relevant aspects of the problem is
called abstraction. This helps in managing complexity and making easir to solve the problem.
Example:
Creating a website
Focusing on main design and functionality without getting into code details. Thinking about:
• Layout of website
d) Algorithm:
• It is a creative development process that refers to the creation of digital objects and systems by using
different computational techniques and tools.
• Computational artifacts could be programs, websites, videos, simulations, databases, digital animation,
e-commerce platforms, and mobile apps etc.
• In software development, computational artifacts are the various digital products that are created
during the process of designing, coding, testing, and maintaining of software.
• These artifacts can be code, documents, diagrams, images, prototype and meeting notes etc.
Prof. Muhammad Zubair Zaib
Computational solution design is the process of creating and planning strategies and methodologies to solve
computational problems.
a) Design Methods.
b) Algorithm Design
a. Design Methods
• Algorithms
• Flowcharts
• Pseudocode
Algorithms:
An algorithm is a step-by-step procedure to find the solution to a problem.
Example:
Find the student’s result
1. Start
2. input marks
3. If marks are greater than 40 display pass else display fail
4. End
Flow Chart:
The diagrammatic/graphical representation of the algorithm is called a flow chart.
Example:
Prof. Muhammad Zubair Zaib
Pseudocode:
• Pseudocode is a high-level, informal way of describing the steps of an algorithm or program.
• It uses a simplified, human-readable syntax that is not linked to any specific programming language.
• It focuses on the logic and flow of the algorithm.
Example:
b. Algorithm Design
• The primary goal of pseudocode is to understand the logic of an algorithm in clear and specific
programming language conventions.
• Pseudocode writing is not a standardized set of rules; however, some sets of guidelines are often
followed:
ii) Indentation:
iii) Case:
Pseudocode is not case-sensitive, Upper-case letters and lower-case letters can be used. Mostly Keywords
in Uppercase letters and variables in lowercase letters.
It is optional and mostly depends on personal preference or requirement. Line numbers can assist the reader
in understanding the logic of the algorithm.
v) Comments:
Comments are essential for explaining the logic of the algorithm. They are often denoted by symbols like
// for single-line comments and /* */ for multiline comments.
Some pseudocode styles use datatypes for different types of data such as INTEGER, STRING, FLOAT
etc. but it is optional.
The variable can be declared by using the DECLARE keyword and the assignment is denoted by = sign
Key commands like INPUT, OUTPUT, IF, WHILE, FOR can be used to represent any action
b. List of Inputs:
Information (Input) needed to solve the problem and expected output (Algorithm used to
produce output)
c. Plan:
i) Outline the Logic: Break the problem into smaller parts and make a plan for how to solve these
problems
Prof. Muhammad Zubair Zaib
ii) Choose Data Structures: A data structure is a storage mechanism that is used to store and organize
data. We can choose different data structures such as arrays, stacks, queues, etc.
iii) Choose Control Structures: to control the execution of the program, the following control structure
can be used:
• Conditional Structure: used when some statements are selected and some are skipped.
• Loops/Iterations: used when one or more statements are repeated
d. Development:
• Describe the steps needed to convert the inputs and produce the output after the processing.
• Start at a high level first and keep refining the steps until they reach the computable operations.
• Keep the process simplified and the number of steps should be finite.
Choose the data set and verify whether your algorithm is working properly or not
Prof. Muhammad Zubair Zaib
Example:
Jeroo is a kangaroo-like animal. A Jeroo can pick flowers from one location and plant them to
others. The Jeroo moves by hopping in four directions e.g. forward, backward, right and left. As
the Jeroo hops; it must avoid water, net, and other hurdles.
Problem Definition:
Consider the given diagram, where the field is divided into a 5x5 matrix and a Jeroo is available at
the position (0,0) facing East. The flower is located at (3,0). Write an algorithm where the Jeroo
picks the flower and plant to another location(3,2). After its plantation, the Jeroo should move one
hop East and stop there. Assume no other Jeroo, flowers or nets are available in the field.
The top left corner of the matrix is the (0,0) value of x increases horizontally and y
downwards.
Prof. Muhammad Zubair Zaib
a) Tracing an Algorithm:
• Tracing of an Algorithm is also known as a “Desk check or Dry run”.
• The programmer converts the algorithm into code and writes it on a compiler for execution.
• Before converting and writing a program on the compiler user manually verifies the working of an
algorithm.
• In tracing we simulate the execution of the algorithm and keep track of all the variables values and
flow logic control.
• The following steps are involved in the Tracing of an algorithm.
a. Understand the Algorithm
b. Choose Test input
c. Initialization
d. Trace Each Step:
e. Repeat until Completion:
f. Verify output
Trace Table:
The dry run of an algorithm can be done with a technique known as a “Trace Table”.
• In this technique, the change in variable values is listed in the table.
• The trace table generates the output on the base of the given input data set.
• It also helps to check that our logic is correct or not, and find logical errors, if any.
Prof. Muhammad Zubair Zaib
Example:
Pseudocode:
1. INPUT Number=3
2. PRINT Number
3. FOR i from 1 to 3
4. Number=Number+5
5. PRINT Number
6. PRINT “END”
1. Correctness:
Correctness means the algorithm must produce the desired output for all possible valid inputs.
Therefore, various inputs are used during the evaluation of an algorithm.
2. Efficiency:
Efficiency deals with the time and space (amount of memory) complexity of an algorithm.
Time Complexity: The amount of time taken by an algorithm to run
Space Complexity: The amount of memory required by the algorithm to run.
3. Clarity:
Clarity means how easily the steps and logic of an algorithm is understood by a human.
4. Reliability:
Reliability refers to the ability of an algorithm to produce correct and accurate results each time we
execute it. A reliable algorithm should be accurate and consistent in terms of performing tasks.
Prof. Muhammad Zubair Zaib
1. Sorting Algorithm:
• Sorting is a process of arranging the values in a specific sequence/Order.
• There are two types of sorting used ascending order sorting and Descending order sorting.
• Insertion Sort
• Bubble Sort
Working:
• Suppose we have an unsorted Array
Prof. Muhammad Zubair Zaib
Step-1:
Select the 1st element and compare it with the next adjacent element.
Step-2 :
Step-3:
Now compare 11 With 77, 11< 77, 11<44, and 11< 33 now shift/swap 11 in place of 33
Prof. Muhammad Zubair Zaib
Step-4:
Now compare 88 77 , as 88> 77, 88>44, 88>33 and 88>11, there is no need to shift/swap any element. An
array is already sorted
Working:
Suppose we have an unsorted Array
Pass-1
Step-1:
Select the 1st two elements from the array and compare them
Step-2:
Step-3:
Step-4:
Compare the next two adjacent elements
As 88>77 So we will not swap these two values the values remain on their current locations
Now again moves to the 1st location(index) of an array and repeat the process againPass-3
Step-1:
Select the 1st two elements from the array and compare them
Prof. Muhammad Zubair Zaib
As the array is sorted, there is no need to perform step 4 or proceed to the next Pass.
6. the number of passes needed is one less than total number of items in the list.
2. Searching Algorithm:
• The process of finding desired elements from a group of elements is called searching.
• The following searching algorithms are used:
❖ Sequential Search
❖ Binary Search
Example:
Search 3 from Array
Sorted Output
1. Visit the first element of the array and compare its value with the required value.
2. If the value of the array matches the desired value, the searching is complete.
3. If the value does not match, move to the next element and repeat the same process.
Working:
Consider the given unsorted array and we need to find the location of item 44.
Step-1:
Select the 1st element of an array and compare it with desired value that we want to search. If the values
match the searching will be completed.
Working:
Consider the given sorted array and we need to find the location of item 69.
Step-1:
Step-: 2
Now check whether the value of the middle element is equal to the desired value or not.
As 29 is not equal to 69 and the search is unsuccessful now the array is reduced into two half
Step-: 3
Prof. Muhammad Zubair Zaib
As we see that the search number 69 is greater than the value of the middle element 29 the searching
process will be performed on 2nd half of the array.
Step-4:
Step-5:
Now check whether the value of the middle element is equal to the desired value or not
As 35 is not equal to 69 and the search is unsuccessful now the array is reduced into two halves.
Step-: 6
As we see that the search number 69 is greater than the value of the middle element 35 the searching
process will be performed on 2nd half of the array.
Only one element remains in an array so it value matches with the desired value so there is no need to
perform further steps. The desired value 69 is found at index 4.
Prof. Muhammad Zubair Zaib