Comptutational Thinking Notes
Comptutational Thinking Notes
Subtopics: -
1. Algorithms
2. Flowcharts
3. Pseudocodes
4. Characteristics of pseudocode
5. Searching algorithms
6. Binary Search
7. AND, OR and NOT to create logic within algorithms
Learning Objectives :
1. Follow, understand, edit and correct algorithms that are presented as pseudocode.
2. Follow flowchart or pseudocode algorithms that use loops.
3. Know how to create algorithms using flowcharts and pseudocode.
4. Know how to use predefined sub-routines in flowcharts or pseudocode.
5. Describe and use binary searches.
6. Understand and use iteration statements, limited to count-controlled loops, presented as either flowcharts or pseudocode.
7. Predict the outcome of algorithms that use iteration.
8. Compare and contrast algorithms designed for the same tasks to determine which is best suited to the purpose.
9. Combine multiple constructs (sequence, selection, count-controlled iteration) to write algorithms as flowcharts or
pseudocode.
1. Algorithm
An algorithm is simply a set of steps used to complete a specific task. They're the building blocks for programming, and they
allow things like computers, smartphones, and websites to function and make decisions.
1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to perform.
3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to do manually.
4. They are used in various fields such as mathematics, computer science, engineering, finance, and many others to
optimize processes, analyze data, make predictions, and provide solutions to problems.
• Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all aspects
and must lead to only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not
take input.
• Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-
defined as well. It should produce at least 1 output.
• Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
• Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available
resources. It must not contain some future technology or anything.
• Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain
instructions that can be implemented in any language, and yet the output will be the same, as expected.
2. Flowcharts
A flowchart is a pictorial representation of a sequence of instructions that are required to solve a problem.
One way of representing algorithms is to use flow charts, also called flow diagrams. They are a useful way of planning how a
computer program might work and show others your thinking.
A flowchart is a graphical representation of an algorithm.it should follow some rules while creating a flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 2: Flowchart ending statement must be ‘end’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: The decision symbol in the flowchart is associated with the arrow line.
Advantages of Flowchart:
Disadvantages of Flowchart:
Pseudocodes
Pseudocode is used to design the programmings and do dryrun to check whether the programs run properly or not.
Amount← 20 Amount is 20
Advantages of Pseudocode
• Improves the readability of any approach. It’s one of the best approaches to start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough documentation, so the
program of one developer can be understood easily when a pseudo code is written out. In industries, the approach of
documentation is essential. And that’s where a pseudo-code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a program should do, hence making the code
construction phase easier for the programmer.
• What is pseudocode?
Answer: A form of representing an algorithm without a set syntax.
• When is pseudocode used?
Answer: When planning or designing algorithms.
• Why is pseudocode used?
Answer: It is language independent so anyone should be able to understand it.
• What features does pseudocode contain?
Answer: Command words, assignment symbols, mathematical symbols
if condition:
else:
A count-controlled loop, or FOR loop, iterates the number of times specified in the The variable used in the FOR statements has a starting value,
and then counts from that number to the end value.
Loops/Repetition/Iteration in a program is a piece, or section, of code that runs more than once
FOR loop the four elements are given in one line, for example:
When using count-controlled loops in flowcharts a variable needs to be declared with a start value, for example: count = 0
The value will need to be checked against the end value, for example: if count = 10
The value will need to be incremented (increased) each time through the loop, for example: count = count + 1
In a flowchart these need to be separated and placed in the correct position, for example:
Key features of the count-controlled iterations in the flowchart:
What is iteration?
Subroutines
• Sub-routine is a self-contained program that that can be called from other programs
• Sub-routines can be used multiple times in an algorithm and can be used across different algorithms.
Advantages of Subroutines
• You do not need to write the same code every time it is used
• Each small programme or algorithm only has to be written once but can be used many times within a larger programme or algorithm.
Answer: A section of code with a name that can be called from another program.
Searching Algorithms
A very common task that computers need to perform is searching for an item in a list. Many highly optimised search algorithms have
been created, as companies like Google rely on effective searching algorithms all of the time. There are two basic searching
algorithms that you need to be aware of:
1. Linear Search
2. Binary Search
Linear Search
The most basic search is called a linear search. You can think of a linear search as simply starting at the beginning of a list and checking
each item until you find the thing you are looking for.
Linear Search, also known as Sequential Search, is a straightforward method for finding a particular element within a list. It sequentially
checks each element in the list until a match is found or the whole list has been searched.
Binary Search
A binary search is a way of looking for a piece of data in an ordered list by continually splitting the list in half. You then check the
middle number and work out which half of the list the value to find it.
To find a number from given list of numbers using Binary Search algorithm:
➢ Binary search is a much more efficient way of searching through a list of items compared to a linear search.
➢ However, you can only use a binary search algorithm if the data is ordered i.e. smallest to largest.
➢ If the data that you have is unordered, you must either use a linear search algorithm or sort the data first.
Linear Search sequentially checks each element Binary Search continuously divides the sorted list,
Definition in the list until it finds a match or exhausts the comparing the middle element with the target
list. value.
Efficiency Less efficient, especially for large datasets. More efficient, especially for large datasets.
Data In a linear search, the elements don't need to be The pre-condition for the binary search is that the
Requirement arranged in sorted order. elements must be arranged in a sorted order.
Size It is preferrable for the small-sized data sets. It is preferrable for the large-size data sets.
Use Case Suitable for small and unsorted datasets. Ideal for large and sorted datasets.
There are some elements that impact the efficiency of a program, for example:
Here are two different codes for each feature, such as two codes that solve the same problem but where one uses many more variables and
lines. For example:
Problem: Take 3 numbers as input, add them together and output the result.
Code 1:
number1 = input("Enter a number")
number2 = input("Enter a number")
number3 = input("Enter a number")
value1 = number1 + number2
total = number3 + value1
print(total)
and
Code 2:
number1 = input("Enter a number")
number2 = input("Enter a number")
number3 = input("Enter a number")
print(number1 + number2 + number2)
• The second program uses fewer lines of code which means the file size will be smaller.
• It also has one less variable therefore, when running, it will use less memory.