SlideShare a Scribd company logo
Standard Algorithms
HIGHER COMPUTING SCIENCE
Learning Intentions
By the end of this lesson learners will be able to write and utilise the following standard
algorithms
❏ Find Min and max
❏ Linear Search
❏ Counting Occurrences
❏ Write and debug the above algorithms
Input validation
Although covered at National 5
The purpose of this algorithm is to ensure that the user inputs data that is in a specified
format or range
Input validation Algorithm
GET input FROM KEYBOARD
WHILE input is invalid
SEND message TO DISPLAY
GET input FROM KEYBOARD
END WHILE
Input validation example
userscore = int(input("Enter your score: "))
while userscore<0 or userscore>9999:
print("Invalid score. Try again.")
userscore = int(input("Enter your score: "))
Find Min and Max Algorithm
The purpose of the Find Min and Find Max algorithms are to look through a list of
items and find the minimum and maximum values respectively.
It uses a variable to hold the current minimum and maximum value.
If a value is found that is lower or higher than the current lowest or highest then that
item is set to the new lowest value
Find Min Algorithm
SET Lowest = array(0)
FOR counter FROM 1 to length(array)
IF current item (array(counter)) < lowest
SET Lowest = array(counter)
SET Position = counter
END IF
END FOR
Find Min Example
minimum = scores[0]
for counter in range(1,len(times)):
if times[counter] < minimum:
minimum = times[counter]
print ("Fastest time is", minimum)
Find Max Algorithm
SET max = array(0)
FOR counter FROM 1 to LENGTH(array)
IF current item (array(counter)) > max
SET max = array(counter)
SET Position = counter
END IF
END FOR
Find Max Example
maximum= scores[0]
for counter in range(1,len(scores)):
if scores[counter] > maximum:
maximum= scores[counter]
print ("Best score is", maximum)
Linear Search Algorithm
The Linear Search algorithm looks through a list of items and finds if a value is in
the list.
It checks every item in the list and compares it to the search item
After the algorithm has traversed the entire list then it will usually display an output
message
Linear Search Algorithm
GET SearchItem FROM KEYBOARD
FOR counter FROM 0 TO length(array)
IF array(counter) = SearchItem
SEND “Item found” TO DISPLAY
END IF
END FOR
Linear Search Algorithm improvement?
But what if after we have processed the list we then need to display a message to the
user either showing
That their search it was found (and its position)
Or
A message stating that their item wasn’t in the list.
How do we achieve this?
Linear Search Algorithm + Flag
We can improve the algorithm by using a Boolean flag to store whether a match has
been found or not
This will only be used after the entire list has been searched.
Linear Search Algorithm + Flag
We can improve the algorithm by using a Boolean flag to store whether a match has
been found or not
This will only be used after the entire list has been searched.
Linear Search Example
choice = input("Please enter your colour: ")
for counter in range(0, len(colours)):
if colours[counter] == choice:
print("Your colour was found at position",counter)
Linear Search using found flag
found = False
choice = input("Please enter the item to search for: ")
for counter in range(len(array)):
if array[counter] == choice:
found = True
position = counter
if found == False:
print("No item found in list”)
else:
print(“Item details: ",array[position])
Linear Search Algorithm Using conditional loop
A further improvement would be to utilise a conditional loop and a found flag
This will allow the algorithm to stop traversing through the loop when a match has
been found
The condition will to be continue the loop until either an item has been found or
there are still items in the list
Meaning for more efficient code due to less operations required…unless the search
item is at the end of the list.
Linear Search
using
conditional
loop
Will stop searching if
it finds an item or
reaches the end of the
list
Counting Occurrences
The counting occurrences algorithm looks through a list of items and counts the
number of times (occurrences) that a match is found.
It again checks every item in the list and increments a counter when a match is
found.
It is important to only output the matches after the entire list has been traversed.
Counting Occurrences Algorithm
GET SearchItem FROM KEYBOARD
SET Total = 0
FOR counter = 0 TO Length(array)
IF array(counter) = SearchItem
SET Total = total +1
END IF
END FOR
Counting Occurrences Example
def CountPasses (passes):
occurences = 0
for counter in range(len(passes)):
if passes[counter].upper() == "PASS":
occurences += 1
print("There were",occurences,"passes")
#-------------Main Program-----------------
#set initial values
passes = ["pass","fail","pass","fail","pass"]
CountPasses(passes)
Extension material
Further Research
There are other algorithms that can be used. Investigate the following:
1. Simple Sort
2. Selection Sort using two lists
3. Bubble Sort
4. Binary Search

More Related Content

PDF
Binary search algorithm
PDF
Searching
PPTX
Searching techniques
PPTX
Dsa – data structure and algorithms sorting
PPT
Searching algorithms
PPSX
Algorithm and Programming (Searching)
PPTX
7 searching injava-binary
Binary search algorithm
Searching
Searching techniques
Dsa – data structure and algorithms sorting
Searching algorithms
Algorithm and Programming (Searching)
7 searching injava-binary

What's hot (20)

PDF
Search Algprithms
PDF
linear search and binary search
PPTX
Sorting and searching arrays binary search algorithm
PPT
Ch05 Black Jack
PPTX
Dsa – data structure and algorithms searching
PPTX
Rahat &amp; juhith
PPT
Searching algorithm
PPTX
Linear search-and-binary-search
PPTX
Binary search
PPTX
Searching linear &amp; binary search
PPT
Sorting algorithms
PPTX
Binary search python
PPTX
Binary search
PPTX
Searching Techniques and Analysis
PPT
Binary Search
PDF
Python basic operators
PPTX
Searching
PPTX
Presentation
PDF
Linear search algorithm
PPTX
Searching techniques in Data Structure And Algorithm
Search Algprithms
linear search and binary search
Sorting and searching arrays binary search algorithm
Ch05 Black Jack
Dsa – data structure and algorithms searching
Rahat &amp; juhith
Searching algorithm
Linear search-and-binary-search
Binary search
Searching linear &amp; binary search
Sorting algorithms
Binary search python
Binary search
Searching Techniques and Analysis
Binary Search
Python basic operators
Searching
Presentation
Linear search algorithm
Searching techniques in Data Structure And Algorithm
Ad

Similar to Standard algorithms (20)

PPT
PPT
Introduction to Algorithm for computer engineering students
PPT
Introduction to Algorithm for computer engineering students
PPT
Chapter 14
PPTX
Chapter3.pptx
PPTX
Algorithm designand their features used.pptx
DOCX
Please Please Please Read the instructions and do everything li.docx
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PPT
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
PPT
Algorithms the fundamentals, For computer Science.ppt
PPTX
Searching and Sorting Algorithms in Data Structures
PDF
Standard Algorithms
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
PPTX
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
PPTX
Advance excel
PPT
1 class linear and Binary search (3).ppt
PPT
4.1 sequentioal search
PPTX
data structures and algorithms Unit 3
PPTX
application of algorithm Presentation1.pptx
PPTX
Algorithms
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
Chapter 14
Chapter3.pptx
Algorithm designand their features used.pptx
Please Please Please Read the instructions and do everything li.docx
Chapter 3 - Data Structure and Algorithms.pptx
data stracyturwe waaure semeer gorbe eidd fata sahttacuyeiwi
Algorithms the fundamentals, For computer Science.ppt
Searching and Sorting Algorithms in Data Structures
Standard Algorithms
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Advance excel
1 class linear and Binary search (3).ppt
4.1 sequentioal search
data structures and algorithms Unit 3
application of algorithm Presentation1.pptx
Algorithms
Ad

More from missstevenson01 (20)

PPTX
S3 environment
PPTX
The Processor.pptx
PPTX
How Computers Work
PPTX
Lesson 3 - Coding with Minecraft - Variables.pptx
PPTX
Lesson 2 - Coding with Minecraft - Events.pptx
PPTX
Lesson 1 - Coding with Minecraft -Introduction.pptx
PPTX
Lesson2 - Coding with Minecraft - Events.pptx
PPTX
Ethical hacking trojans, worms and spyware
PPTX
Ethical hacking anti virus
PPTX
Ethical hacking introduction to ethical hacking
PPTX
S1 internet safety-chattingonline
PPTX
S3 wireframe diagrams
PPTX
Alien database
PPTX
Video Games and Copyright laws
PPTX
Games Design Document
PPTX
Video game proposal
PPTX
Evaluation
PPTX
H evaluation
PPTX
H testing and debugging
S3 environment
The Processor.pptx
How Computers Work
Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson2 - Coding with Minecraft - Events.pptx
Ethical hacking trojans, worms and spyware
Ethical hacking anti virus
Ethical hacking introduction to ethical hacking
S1 internet safety-chattingonline
S3 wireframe diagrams
Alien database
Video Games and Copyright laws
Games Design Document
Video game proposal
Evaluation
H evaluation
H testing and debugging

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Presentation on HIE in infants and its manifestations
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
RMMM.pdf make it easy to upload and study
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Chinmaya Tiranga quiz Grand Finale.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Presentation on HIE in infants and its manifestations
Microbial disease of the cardiovascular and lymphatic systems
O7-L3 Supply Chain Operations - ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
RMMM.pdf make it easy to upload and study
A systematic review of self-coping strategies used by university students to ...
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Standard algorithms

  • 2. Learning Intentions By the end of this lesson learners will be able to write and utilise the following standard algorithms ❏ Find Min and max ❏ Linear Search ❏ Counting Occurrences ❏ Write and debug the above algorithms
  • 3. Input validation Although covered at National 5 The purpose of this algorithm is to ensure that the user inputs data that is in a specified format or range
  • 4. Input validation Algorithm GET input FROM KEYBOARD WHILE input is invalid SEND message TO DISPLAY GET input FROM KEYBOARD END WHILE
  • 5. Input validation example userscore = int(input("Enter your score: ")) while userscore<0 or userscore>9999: print("Invalid score. Try again.") userscore = int(input("Enter your score: "))
  • 6. Find Min and Max Algorithm The purpose of the Find Min and Find Max algorithms are to look through a list of items and find the minimum and maximum values respectively. It uses a variable to hold the current minimum and maximum value. If a value is found that is lower or higher than the current lowest or highest then that item is set to the new lowest value
  • 7. Find Min Algorithm SET Lowest = array(0) FOR counter FROM 1 to length(array) IF current item (array(counter)) < lowest SET Lowest = array(counter) SET Position = counter END IF END FOR
  • 8. Find Min Example minimum = scores[0] for counter in range(1,len(times)): if times[counter] < minimum: minimum = times[counter] print ("Fastest time is", minimum)
  • 9. Find Max Algorithm SET max = array(0) FOR counter FROM 1 to LENGTH(array) IF current item (array(counter)) > max SET max = array(counter) SET Position = counter END IF END FOR
  • 10. Find Max Example maximum= scores[0] for counter in range(1,len(scores)): if scores[counter] > maximum: maximum= scores[counter] print ("Best score is", maximum)
  • 11. Linear Search Algorithm The Linear Search algorithm looks through a list of items and finds if a value is in the list. It checks every item in the list and compares it to the search item After the algorithm has traversed the entire list then it will usually display an output message
  • 12. Linear Search Algorithm GET SearchItem FROM KEYBOARD FOR counter FROM 0 TO length(array) IF array(counter) = SearchItem SEND “Item found” TO DISPLAY END IF END FOR
  • 13. Linear Search Algorithm improvement? But what if after we have processed the list we then need to display a message to the user either showing That their search it was found (and its position) Or A message stating that their item wasn’t in the list. How do we achieve this?
  • 14. Linear Search Algorithm + Flag We can improve the algorithm by using a Boolean flag to store whether a match has been found or not This will only be used after the entire list has been searched.
  • 15. Linear Search Algorithm + Flag We can improve the algorithm by using a Boolean flag to store whether a match has been found or not This will only be used after the entire list has been searched.
  • 16. Linear Search Example choice = input("Please enter your colour: ") for counter in range(0, len(colours)): if colours[counter] == choice: print("Your colour was found at position",counter)
  • 17. Linear Search using found flag found = False choice = input("Please enter the item to search for: ") for counter in range(len(array)): if array[counter] == choice: found = True position = counter if found == False: print("No item found in list”) else: print(“Item details: ",array[position])
  • 18. Linear Search Algorithm Using conditional loop A further improvement would be to utilise a conditional loop and a found flag This will allow the algorithm to stop traversing through the loop when a match has been found The condition will to be continue the loop until either an item has been found or there are still items in the list Meaning for more efficient code due to less operations required…unless the search item is at the end of the list.
  • 19. Linear Search using conditional loop Will stop searching if it finds an item or reaches the end of the list
  • 20. Counting Occurrences The counting occurrences algorithm looks through a list of items and counts the number of times (occurrences) that a match is found. It again checks every item in the list and increments a counter when a match is found. It is important to only output the matches after the entire list has been traversed.
  • 21. Counting Occurrences Algorithm GET SearchItem FROM KEYBOARD SET Total = 0 FOR counter = 0 TO Length(array) IF array(counter) = SearchItem SET Total = total +1 END IF END FOR
  • 22. Counting Occurrences Example def CountPasses (passes): occurences = 0 for counter in range(len(passes)): if passes[counter].upper() == "PASS": occurences += 1 print("There were",occurences,"passes") #-------------Main Program----------------- #set initial values passes = ["pass","fail","pass","fail","pass"] CountPasses(passes)
  • 24. Further Research There are other algorithms that can be used. Investigate the following: 1. Simple Sort 2. Selection Sort using two lists 3. Bubble Sort 4. Binary Search