SlideShare a Scribd company logo
1 of 24
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

What's hot

linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmDavid Burks-Courses
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Binary search python
Binary search pythonBinary search python
Binary search pythonMaryamAnwar10
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and AnalysisAkashBorse2
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 

What's hot (20)

Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Sorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithmSorting and searching arrays binary search algorithm
Sorting and searching arrays binary search algorithm
 
Ch05 Black Jack
Ch05  Black  JackCh05  Black  Jack
Ch05 Black Jack
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Binary search
Binary search Binary search
Binary search
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Binary search python
Binary search pythonBinary search python
Binary search python
 
Binary search
Binary searchBinary search
Binary search
 
Searching Techniques and Analysis
Searching Techniques and AnalysisSearching Techniques and Analysis
Searching Techniques and Analysis
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Python basic operators
Python   basic operatorsPython   basic operators
Python basic operators
 
Searching
SearchingSearching
Searching
 
Presentation
PresentationPresentation
Presentation
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 

Similar to Standard algorithms

Please Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docxPlease Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docxtienmixon
 
Algorithms the fundamentals, For computer Science.ppt
Algorithms the fundamentals, For computer Science.pptAlgorithms the fundamentals, For computer Science.ppt
Algorithms the fundamentals, For computer Science.pptCarloCimacio
 
1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).pptKanchanRaut13
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal searchKrish_ver2
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sortingguest2cb109
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfrajeshjangid1865
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09Terry Yoast
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiB.Kirron Reddi
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
There are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdfThere are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdfaamousnowov
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxStephenRobert15
 

Similar to Standard algorithms (20)

9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Please Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docxPlease Please Please Read the instructions and do everything li.docx
Please Please Please Read the instructions and do everything li.docx
 
Algorithms the fundamentals, For computer Science.ppt
Algorithms the fundamentals, For computer Science.pptAlgorithms the fundamentals, For computer Science.ppt
Algorithms the fundamentals, For computer Science.ppt
 
Standard Algorithms
Standard AlgorithmsStandard Algorithms
Standard Algorithms
 
Advance excel
Advance excelAdvance excel
Advance excel
 
1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt1 class linear and Binary search (3).ppt
1 class linear and Binary search (3).ppt
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 
9781439035665 ppt ch09
9781439035665 ppt ch099781439035665 ppt ch09
9781439035665 ppt ch09
 
Chap10
Chap10Chap10
Chap10
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
 
There are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdfThere are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdf
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptx
 

More from missstevenson01

Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 3 -  Coding with Minecraft -  Variables.pptxLesson 3 -  Coding with Minecraft -  Variables.pptx
Lesson 3 - Coding with Minecraft - Variables.pptxmissstevenson01
 
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxLesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxmissstevenson01
 
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxLesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxmissstevenson01
 
Lesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxLesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxmissstevenson01
 
Ethical hacking trojans, worms and spyware
Ethical hacking    trojans, worms and spywareEthical hacking    trojans, worms and spyware
Ethical hacking trojans, worms and spywaremissstevenson01
 
Ethical hacking anti virus
Ethical hacking   anti virusEthical hacking   anti virus
Ethical hacking anti virusmissstevenson01
 
Ethical hacking introduction to ethical hacking
Ethical hacking   introduction to ethical hackingEthical hacking   introduction to ethical hacking
Ethical hacking introduction to ethical hackingmissstevenson01
 
S1 internet safety-chattingonline
S1 internet safety-chattingonlineS1 internet safety-chattingonline
S1 internet safety-chattingonlinemissstevenson01
 
Video Games and Copyright laws
Video Games and Copyright lawsVideo Games and Copyright laws
Video Games and Copyright lawsmissstevenson01
 

More from missstevenson01 (20)

S3 environment
S3 environmentS3 environment
S3 environment
 
The Processor.pptx
The Processor.pptxThe Processor.pptx
The Processor.pptx
 
How Computers Work
How Computers WorkHow Computers Work
How Computers Work
 
Lesson 3 - Coding with Minecraft - Variables.pptx
Lesson 3 -  Coding with Minecraft -  Variables.pptxLesson 3 -  Coding with Minecraft -  Variables.pptx
Lesson 3 - Coding with Minecraft - Variables.pptx
 
Lesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptxLesson 2 - Coding with Minecraft - Events.pptx
Lesson 2 - Coding with Minecraft - Events.pptx
 
Lesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptxLesson 1 - Coding with Minecraft -Introduction.pptx
Lesson 1 - Coding with Minecraft -Introduction.pptx
 
Lesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptxLesson2 - Coding with Minecraft - Events.pptx
Lesson2 - Coding with Minecraft - Events.pptx
 
Ethical hacking trojans, worms and spyware
Ethical hacking    trojans, worms and spywareEthical hacking    trojans, worms and spyware
Ethical hacking trojans, worms and spyware
 
Ethical hacking anti virus
Ethical hacking   anti virusEthical hacking   anti virus
Ethical hacking anti virus
 
Ethical hacking introduction to ethical hacking
Ethical hacking   introduction to ethical hackingEthical hacking   introduction to ethical hacking
Ethical hacking introduction to ethical hacking
 
S1 internet safety-chattingonline
S1 internet safety-chattingonlineS1 internet safety-chattingonline
S1 internet safety-chattingonline
 
S3 wireframe diagrams
S3 wireframe diagramsS3 wireframe diagrams
S3 wireframe diagrams
 
Sql
SqlSql
Sql
 
Alien database
Alien databaseAlien database
Alien database
 
Video Games and Copyright laws
Video Games and Copyright lawsVideo Games and Copyright laws
Video Games and Copyright laws
 
Games Design Document
Games Design DocumentGames Design Document
Games Design Document
 
Video game proposal
Video game proposalVideo game proposal
Video game proposal
 
Evaluation
EvaluationEvaluation
Evaluation
 
H evaluation
H evaluationH evaluation
H evaluation
 
H testing and debugging
H testing and debuggingH testing and debugging
H testing and debugging
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

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