CS Chapter 1
CS Chapter 1
Chowdhury
Problem Solving
Ch-1
Understanding Algorithm
Algorithm-
Steps that a computer takes to do a task. It is a stepwise precise method to solve a problem.
Properties of an Algorithm-
Displaying Algorithm-
Written description
Pseudocode
Flowchart
Flowchart symbols-
Start/End Input/Output
Process/Task Decision
Flow
Pseudocode-
SEND “text” TO DISPLAY (Used to send a text to the screen)
SEND variable TO DISPLAY (Used to send a variable to the screen)
RECEIVE variable FROM KEYBOARD (Used to receive a variable from the user).
SET variable TO a specific value or variable (any operation sign) variable …….
Variable-
A variable is a container that holds a value which can change.
Constant-
A Constant is a container that holds a value which never change.
definitions:
Algorithm- Steps that a computer takes to do a task. It is a stepwise precise method to solve a problem.
Pseudocode- pseudocode is a structured, code-like language that can be used to describe an algorithm.
Constructs: a smaller part from which something is built. Letters and numbers (i.e. a to z and 0 to
9) are the constructs we use to build our language and convey meaning.
IS
NO
YES
Properties:
Always Starts with IS
Will always have an outcome of either YES or NO
pseudocode:
ELSE
END IF
IF condition1 THEN
ELSE
END IF
END IF
Examples of Flowchart:
Q. A student is creating a guessing game. A player has to enter a number no greater than 10. If it is too
high, they are informed that they have made an error. But if it is within the range 1 to 10, they are told
whether or not they have guessed the correct number. (Assume that the correct number is 3. Express
the algorithm as a flowchart?
Q. Develop an algorithm expressed as a flowchart to control the heating in a house. A thermostat
monitors the temperature within the house. During the week the temperature should be 20°C between
06.00 and 08.30 in the morning and between 17.30 and 22.00 at night. At weekends it should be 22°C
between 08.00 and 23.00. If the temperature in the house falls below 10°C at any time the boiler is
switched on?
Examples of Pseudocode:
A school uses this algorithm to calculate the grade that students achieve
Algorithm for BMI Calculation:
Ch-3
Sorting and Searching Algorithm
Bubble Sort:
Steps:
1. Start at the beginning of the list.
2. Compare the element in the 1st position with the element in the 2nd position and swap them if
they aren’t in the correct order.
3. Compare the element in the 2nd position with the element in the 3rd position and swap them if
they aren’t in the correct order.
4. Continue to the end of the list.
5. Report steps 1-4 if there have been any swaps.
Merge Sort:
Steps:
1. Find the middle index of the given data set.
2. Divide the dataset from the middle
3. Call mergesort for the first half of the dataset.
4. Call mergesort for the second half of the dataset.
5. Merge the sorted halves into one dataset.
Linear Search:
1 Start at the first item in the list.
Binary Search:
1 Select the median item of the list.
3 If the median is too high, then repeat 1 and 2 with the sub-list to the left.
4 If the median is too low, then repeat 1 and 2 with the sub-list to the right.
5 Repeat steps 3 and 4 until the item has been found or all of the items have been checked
Best and worst cases for linear search and binary search:
Linear search: A linear search starts at the first item and then works through sequentially. The best case
would be if the item is first in the list. The worst case would be if it is last in the list.
Binary search The best case would be if the item is in the median position in the list. The search would
require only one comparison. For the worst case it would have to choose the following medians until it
finally hit the target.
Ch-4
Decomposition and Abstraction
Computational thinking: the thought processes involved in formulating problems and their
solutions so that the solutions are represented in a form that can be effectively carried out by a
computer.
Decomposition: breaking a problem down into smaller, more manageable parts, which are then easier
to solve.
Abstraction: the process of removing or hiding unnecessary detail so that only the important points
remain.