0% found this document useful (0 votes)
40 views

Chapter 7

The document discusses algorithm design and problem solving. It covers topics like the program development life cycle, problem decomposition, common algorithms like linear search and bubble sort, and testing. Problem decomposition involves breaking problems down into inputs, processes, outputs, and storage. Linear search checks an array to find a target value, while bubble sort repeatedly compares and swaps adjacent elements to sort data in ascending or descending order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Chapter 7

The document discusses algorithm design and problem solving. It covers topics like the program development life cycle, problem decomposition, common algorithms like linear search and bubble sort, and testing. Problem decomposition involves breaking problems down into inputs, processes, outputs, and storage. Linear search checks an array to find a target value, while bubble sort repeatedly compares and swaps adjacent elements to sort data in ascending or descending order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Algorithm design and problem solving

Topics to be covered:
1. Program development life cycle(PDLC)
2. Computer system and subsystem
3. Problem decomposition
4. Methods used to design/construct solution for problem
5. Purpose of an algorithm
6. Standard methods of solution:
• Linear search
• Bubble sort
• Totalling
• Counting
• Finding average, maximum and minimum from data
7. Validation check
8. Verification check
9. Different types of test data/Tracetable
10. Writing,amending,identifying ,Correcting errors
7.1 Program development life cycle(PDLC)

Analysis

Maintenance Design

Testing Coding
Phases of program development
1. Analysis:
• Clearly define your problem
• Requirement specification
• Decomposition –Breaking down a problem in smaller parts

2. Design
• Formal documentation
• Structure chart
• Flow chart
• Pseudocode
3. Coding
• Programing language to build program
Phases of program development
4. Testing
Whole program will be tested using different sets of test data
• Validation
• Verification
• Error correction
5. Maintenance
• Documentation
7.2.2 Decomposing a problem Analysis
→ 𝐶𝑙𝑒𝑎𝑟 𝐷𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛
→ 𝑅𝑒𝑞𝑢𝑖𝑟𝑚𝑒𝑛𝑡
Breaking down of a
problem
Big Task

Task 1 Task 2

Task 1 Task 2 Task 1 Task 2 Task 3

Task 1
7.2.2 Decomposing a problem
• Components in which a problem can be decomposed:
1. INPUTS → Data used by the system That needs to be entered while
the system is active

2. Process → Task that are needed to be performed using the input


data and any other previously stored data

3. OUTPUTS →Information that needs to be displayed

4. Storage → Data that needs to be stored in files on an appropriate


medium for use in the future
An Alarm App

INPUT Processes OUTPUT Storage

1. Time to set alarm 1. Clock time= Alarm 1. Make Sound/tune 1. Times for alarm set
2. Remove previous time [at alarm time and
alarm 2. Store and Remove after snooze expires]
3. Switching off alarm alarm time
4. Press snooze button 3. Management of
snooze
7.6 Testing
• Repeated use of a system to try all different possibilities to make sure that the system is:
• fully working

• Does not crash.

• Meets all requirements

Testing is done using test data

There are four types of test data.

1. Normal: Data that program should accept

2. Abnormal: Data that program should not accept

3. Extreme: data that is at the edge of what is allowed

4. Boundary: Data that is at the edge of being accepted or being rejected.


Validation on INPUT
• The checking of data to make sure it is reasonable and within set bounds.

1. Range check

2. Length check

3. Type check

4. Presence check

5. Format check

6. Digit check
• Range check • Type check • Format check
A type of validation check It check if the data is it checks if data
that makes sure data is in correct datatype. meets a specific
between maximum and order, e.g. 1 Number
minimum range 3 letters

• Check Digit
• Presence check
• Length check Error detection method
it checks if data has that is used for data entry.
It checks the number of been entered
characters within a set limit

v Check examples given in your book


Length Check
• An algorithm checks passwords.
• Each password must be 10 or more characters in length; the predefined function
Length returns the number of characters. [Validation]
• Each password is entered twice, and the two entries must match. [Verification]
• Either Accept or Reject is output.
• An input of 111 stops the process.
Common Algorithms:
• Searching Algorithm: A searching algorithm checks a set of data to identify
whether a specific value exists in the data or not. One example of this is , linear
search algorithm.

• Sorting Algorithm: A sorting algorithm takes a set of data and rearranges it to be


in a specific order, e.g. in ascending alphabetical order.
Linear Search :
oGiven an array arr[] of N elements, the task is to search a given element x in arr[].

§ Process:

1. Check the first element of the array . Compare if it is the target element x .

2. If it the target element then return its index.

3. If it is not then check the next value and repeat step-2 .

4. If the target element is not found and there is no more item left in the array then
output that the element was not found.

#Iterate from 0 to N-1 and compare the value of every index with x, if they match return
index
Num←[20,43,32,12,18,4,16,8,40,99]
x←4

Num 20 43 32 12 18 4 16 8 40 99
index 0 1 2 3 4 5 6 7 8 9

When , index ← 0 When , index ← 1 When , index ← 2 When , index ← 3


Num[index] ← 20 Num[index] ← 43 Num[index] ← 32 Num[index] ← 12
Num[index] <> x Num[index] <> x Num[index] <> x Num[index] <> x
Checking next index Checking next index Checking next index Checking next index

When , index ← 4 When , index ← 5


Num[index] ← 18 Num[index] ← 4
Num[index] <> x Num[index] = x
Checking next index Return index 5.
v Check pseudocode examples given in your book
Bubble sort:
• Bubble sort, also known as sinking sort, is the easiest sorting algorithm. It works
on the idea of repeatedly comparing the adjacent elements, from left to right,
and swapping them if they are not-in-order.
• Consider a list with the elements 5, 4, 3, 2 in it. You are asked to arrange these
elements in both ascending and descending order.

Ascending sorting Descending sorting


• How does bubble sort work?
Consider that we want to sort a list in ascending order, here are the
steps that the algorithm would follow:
1. Start with the first element.
2. Compare the current element with the next element.
3. If the current element is greater than the next element, then swap
both the elements. If not, move to the next element.
4. Repeat steps 1 – 3 until we get the sorted list. To better understand
bubble sort, recall the list that contains the elements 5, 3, 4,
2 initially.
Simulation:
List 5 3 4 2

First pass:
Swap

5 3 4 2 Since 5>3 ; Elements are not in order

Swap

3 5 4 2 Since 5>4 ; Elements are not in order

Swap

3 4 5 2 Since 5>2 ; Elements are not in order

3 4 2 5 List is still unsorted.


i i+1 i+2

j j+1
Pseudocode:
1. Data_array ← [ 5,3,4,2]
2. Number_of_passes ← 0
3. Changes ← False
4. WHILE Changes = False OR Number_of_passes <= LENGTH(Data_array)-1
5. FOR i ← 0 to LENGTH(Data_array)-1
6. IF Data_array[i] > Data_array[i+1] THEN
7. Temp ← Data_array[i]
8. Data_array[i] ← Data_array[i+1]
9. Data_array[i+1] ← Temp
10. Change ← TRUE
11. ENDIF
12. NEXT I
13. Number_of_passes ← Number_of_passes + 1
14. Changes ← False
15. ENDWHILE

You might also like