0% found this document useful (0 votes)
4 views14 pages

CS Chapter 1

The document provides an overview of algorithms, including their properties, differences from code programs, and methods of display such as pseudocode and flowcharts. It also discusses constructs for creating algorithms, sorting and searching algorithms like bubble sort and binary search, and concepts of decomposition and abstraction in computational thinking. Key definitions and examples are included to illustrate these concepts.

Uploaded by

zc67920
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

CS Chapter 1

The document provides an overview of algorithms, including their properties, differences from code programs, and methods of display such as pseudocode and flowcharts. It also discusses constructs for creating algorithms, sorting and searching algorithms like bubble sort and binary search, and concepts of decomposition and abstraction in computational thinking. Key definitions and examples are included to illustrate these concepts.

Uploaded by

zc67920
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Prepared By: Zuhayer

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-

 It is unambiguous (Clear and easy to understand without any confusion or doubt.)


 It has a sequence of steps
 It can be used again and will always provide the same result.
 It provides a solution to a problem.

Properties of a Successful Algorithm-


 Accuracy – it must lead to the expected outcome.
 Consistency – it must produce the same result each time it is run.
 Efficiency – it must solve the problem in the shortest possible time, using as few computer
resources as possible.

Difference between an algorithm and Code Program-


Algorithm- It is the design of a solution.
Code Program-It is the implementation of that design using a programming language.

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.

Naming Conventions of a variable-


 Variable names are case sensitive (apple, APPLE, Apple are treated as different variables).
 A variable name can only contain alpha numeric symbols(A-Z,a-z,0-9) and underscore(_).
 A variable name cannot start with a number.
 A variable name may start either with a letter or with an underscore.
Arithmetic Operators-

definitions:
Algorithm- Steps that a computer takes to do a task. It is a stepwise precise method to solve a problem.

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.

Pseudocode- pseudocode is a structured, code-like language that can be used to describe an algorithm.

Flowchart- flowchart shows an algorithm as a diagram.


operator- a character that represents an action.
Ch-2
Creating 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.

 Sequence: an ordered set of instructions.


 Selection: a construct that allows a choice to be made between different alternatives.
 Iteration: a construct that means a process is repeated. An action is repeated until a condition
is met or a particular outcome is reached.

Flowchart Symbol Decision:

IS

NO

YES

Properties:
 Always Starts with IS
 Will always have an outcome of either YES or NO

pseudocode:

In pseudocode, if and else statements are used for decision-making.

1. Basic Rule: IF condition THEN

// Code to execute if condition is


true

ELSE

// Code to execute if condition is


false

END IF

The condition inside IF must be either TRUE or FALSE

2. Using ELSE IF(For multiple conditions):

IF condition1 THEN

// Code for condition1

ELSE IF condition2 THEN

// Code for condition2

ELSE

// Code if none of the conditions are


true

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:

Guessing game in 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.

Q. Perform bubble sort on this list of data and find out:


1. The number of passes
2. The number of swaps
Number of pass:4
Number of swaps:6

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.

2 Compare the item with the search item.

3 If they are the same, then stop.

4 If they are not, then move to the next item.

5 Repeat 2 to 4 until the end of the list is reached.

Binary Search:
1 Select the median item of the list.

2 If the median item is equal to the search item, then stop.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy