SlideShare a Scribd company logo
1 of 23
Algorithms
Michael Heron
Introduction
• Yesterday we talked about maintainability.
• Today we talk about algorithms.
• Algorithms are implementation independent representations of
common tasks in programming.
• Mostly these get represented in pseudo-code.
• We still need to turn them into actual working programs.
• All the steps needed are listed for our use.
Programming Tasks
• Often in programming we need to do certain well defined
tasks.
• Rather than solve the problem for ourselves, we can look to
see where people have made available algorithms to solve the
problem for us.
• Implementing the algorithm into our own program requires
understanding.
– We need to know how it all fits together.
Common Programming Tasks
• Arrays introduce two fertile areas for algorithms.
– Searching
– Sorting
• Arrays let us group together data according to a certain type.
– Ints, floats, chars, etc
• But the arrays we work with would often be better if we could
do certain things easily.
Searching
• A common task for arrays is searching through to see if a
particular value can be found.
• Various ways to do this.
• Various algorithms exist to provide ways in which this can be
done.
• Linear search
• Binary search
• Not all algorithms are created equal…
Efficiency
• Algorithms give us the process by which we arrive at a result,
but they also give us another thing.
– A measure of efficiency.
• Algorithms are a fertile field of study for software developers.
– How much better is algorithm X compared to algorithm Y
• Much attention paid to how efficient they are.
– Particularly in terms of how they scale.
Scaling
• Scaling is the term we use to discuss how things change when
we deal with bigger values.
– A piece of code that works for five values may not work well for
ten
– A piece of code that works well for ten may not work as well for a
million.
• We call this ‘scaling up’
– Many algorithms have problems doing this.
Sorting
• Arrays force no ordering onto elements.
– They go in the order we tell them to.
• We often require ordering to be applied.
– Otherwise we may as well not have an array for large data sets.
• Imagine a phone-book that wasn’t in alphabetical order.
– How would you use it?
• The process of taking an unordered array and making it
ordered is called sorting.
Sorting
• As with searching, many algorithms exist.
• Today we’ll talk about one
• Bubble sort
• Sorting algorithms in particular have scaling problems.
• A result of combinatorial explosion
• Need a measure of how efficient they are for different sets of
data.
Cases
• Algorithms get assessed on the following:
– Best case
– Worst case
– Average case
• Best case is when the circumstances are as amenable to
processing as they can be.
• Worst case is when the circumstances are as bad for
processing as they can be.
• Average case is the efficiency in the average case.
Searching
• Let’s look at an example – the linear search.
For each item in the list:
Check to see if the item you're looking for matches the item in the list.
If it matches.
Return the location where you found it (the index).
If it does not match.
Continue searching until you reach the end of the list.
If we get here, we know the item does not exist in the list. Return -1.
http://en.wikipedia.org/wiki/Linear_search
Linear Search
intlinearSearch(int a[], intvalueToFind, int size) {
for (inti=0; i<size; i++) {
if (valueToFind == a[i]) {
return i;
}
}
return -1;
}
Code scales linearly.
For 100 items, it has the following performance:
Worst case – 100 comparisons
Best case – 1 comparison
Average case – 50 comparisons
Binary Search
• If we had a sorted set of data, we would have a different
situation.
• We could use a binary search.
• This allows us to cut out half of the entire array each time we
search.
• This scales much better.
• We require many fewer comparisons to find the element in
which we are interested.
Binary Search
Start at midpoint.
If desired value is at current point
Return the current index.
If desired value is in the top half of array
Discard the bottom half
Search again
If desired value is in the bottom half of array
Discard the top half
Search again
Binary Search Code
Int binarySearch(intsortedArray[], int first, int last, int key) {
while (first <= last) {
int mid = (first + last) / 2;
if (key >sortedArray[mid])
first = mid + 1;
else if (key <sortedArray[mid])
last = mid - 1;
else
return mid;
}
return -1;
}
http://www.uow.edu.au/~lukes/TEXTBOOK/notes-
cpp/algorithms/searching/binarysearch.html
Binary Search Performance
• Binary searches scale better than linear searches.
– For each element, we half the size of the search space.
• For 1024 elements:
• Much better
performance than
associated linear
search.
Sorting
• Binary searches only work on sorted data.
• We must ensure the data is sorted before we can apply it.
• May not be worth it for small data sets.
• Must sort the data before hand.
• Multiple ways to do this also.
• Again, different algorithms scale differently depending on the
number of elements involved.
Bubble Sort
Start at the beginning of the array
Set our current position to the beginning.
go through each element of the array from the beginning
if our current element is greater than the next element
swap them around
Set the current position to be the next element
Repeat until we reach the end.
Won’t look at the code for this sort.
Elements ‘bubble up’ into the right position based on the algorithm.
Bad efficiency, with combinatorial explosion problem.
Does however result in a fully sorted list.
Algorithms
• Algorithms give us the solution without the code.
• That means we don’t need to worry about converting between
different languages.
• We have a base level description we can use for actual
implementation.
• They serve as a kind of ‘shorthand jargon’ for people in the
business.
• When you talk about a ‘bubble sort’, other programmers know
what you mean.
Efficiency
• With the speed of modern processors, is efficiency
really a big deal?
• Bigger processors have led to more ambitious programs.
• For very large sets of data, efficiency can be a real
issue.
• Programs that take hours and hours to run are not
uncommon.
• Some applications of programming so processor
intensive that modern processors still cannot cope.
• Realistic 3D graphics as an example
Trade-Offs
• Programming is a process of trade-offs
• You can have perfect maintainability or perfect efficiency, you
can’t have both.
• Code that is very efficient usually has very low readability.
• Important to decide for yourself where the line lies for a given
program.
• Safest to go for maintainability first.
Optimisation
• The process of turning slow running code into
fast running code is called optimisation.
• Should be done only when there is a need.
• There exists in programming an informal
guideline called the 80/20 rule
• Used in many contexts
• In this context: 80% of your CPU’s time is spent in 20%
of the code
• Hard to tell where critical code is
• Don’t optimise first
Summary
• Efficiency often lies at the other end of the spectrum from
maintainability.
• Code that runs fast is not often code that is easily maintained.
• Algorithms exist as a shorthand for approached to
programming problems.
• Along with a way of measuring efficiency.
• Each program requires a different approach.
• No one answer.

More Related Content

Similar to CPP12 - Algorithms

Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 

Similar to CPP12 - Algorithms (20)

Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structures
 
Data Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptxData Structures and Algorithm - Module 1.pptx
Data Structures and Algorithm - Module 1.pptx
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Intro to Data Structure & Algorithms
Intro to Data Structure & AlgorithmsIntro to Data Structure & Algorithms
Intro to Data Structure & Algorithms
 
Algorithms 1
Algorithms 1Algorithms 1
Algorithms 1
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdf
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
CPP18 - String Parsing
CPP18 - String ParsingCPP18 - String Parsing
CPP18 - String Parsing
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
General Programming Concept
General Programming ConceptGeneral Programming Concept
General Programming Concept
 
Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.
 
SearchAlgorithm.pdf
SearchAlgorithm.pdfSearchAlgorithm.pdf
SearchAlgorithm.pdf
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
lec1.ppt
lec1.pptlec1.ppt
lec1.ppt
 
Quick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili SaghafiQuick Sort By Prof Lili Saghafi
Quick Sort By Prof Lili Saghafi
 
Software + Babies
Software + BabiesSoftware + Babies
Software + Babies
 

More from Michael Heron

More from Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Recently uploaded

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

CPP12 - Algorithms

  • 2. Introduction • Yesterday we talked about maintainability. • Today we talk about algorithms. • Algorithms are implementation independent representations of common tasks in programming. • Mostly these get represented in pseudo-code. • We still need to turn them into actual working programs. • All the steps needed are listed for our use.
  • 3. Programming Tasks • Often in programming we need to do certain well defined tasks. • Rather than solve the problem for ourselves, we can look to see where people have made available algorithms to solve the problem for us. • Implementing the algorithm into our own program requires understanding. – We need to know how it all fits together.
  • 4. Common Programming Tasks • Arrays introduce two fertile areas for algorithms. – Searching – Sorting • Arrays let us group together data according to a certain type. – Ints, floats, chars, etc • But the arrays we work with would often be better if we could do certain things easily.
  • 5. Searching • A common task for arrays is searching through to see if a particular value can be found. • Various ways to do this. • Various algorithms exist to provide ways in which this can be done. • Linear search • Binary search • Not all algorithms are created equal…
  • 6. Efficiency • Algorithms give us the process by which we arrive at a result, but they also give us another thing. – A measure of efficiency. • Algorithms are a fertile field of study for software developers. – How much better is algorithm X compared to algorithm Y • Much attention paid to how efficient they are. – Particularly in terms of how they scale.
  • 7. Scaling • Scaling is the term we use to discuss how things change when we deal with bigger values. – A piece of code that works for five values may not work well for ten – A piece of code that works well for ten may not work as well for a million. • We call this ‘scaling up’ – Many algorithms have problems doing this.
  • 8. Sorting • Arrays force no ordering onto elements. – They go in the order we tell them to. • We often require ordering to be applied. – Otherwise we may as well not have an array for large data sets. • Imagine a phone-book that wasn’t in alphabetical order. – How would you use it? • The process of taking an unordered array and making it ordered is called sorting.
  • 9. Sorting • As with searching, many algorithms exist. • Today we’ll talk about one • Bubble sort • Sorting algorithms in particular have scaling problems. • A result of combinatorial explosion • Need a measure of how efficient they are for different sets of data.
  • 10. Cases • Algorithms get assessed on the following: – Best case – Worst case – Average case • Best case is when the circumstances are as amenable to processing as they can be. • Worst case is when the circumstances are as bad for processing as they can be. • Average case is the efficiency in the average case.
  • 11. Searching • Let’s look at an example – the linear search. For each item in the list: Check to see if the item you're looking for matches the item in the list. If it matches. Return the location where you found it (the index). If it does not match. Continue searching until you reach the end of the list. If we get here, we know the item does not exist in the list. Return -1. http://en.wikipedia.org/wiki/Linear_search
  • 12. Linear Search intlinearSearch(int a[], intvalueToFind, int size) { for (inti=0; i<size; i++) { if (valueToFind == a[i]) { return i; } } return -1; } Code scales linearly. For 100 items, it has the following performance: Worst case – 100 comparisons Best case – 1 comparison Average case – 50 comparisons
  • 13. Binary Search • If we had a sorted set of data, we would have a different situation. • We could use a binary search. • This allows us to cut out half of the entire array each time we search. • This scales much better. • We require many fewer comparisons to find the element in which we are interested.
  • 14. Binary Search Start at midpoint. If desired value is at current point Return the current index. If desired value is in the top half of array Discard the bottom half Search again If desired value is in the bottom half of array Discard the top half Search again
  • 15. Binary Search Code Int binarySearch(intsortedArray[], int first, int last, int key) { while (first <= last) { int mid = (first + last) / 2; if (key >sortedArray[mid]) first = mid + 1; else if (key <sortedArray[mid]) last = mid - 1; else return mid; } return -1; } http://www.uow.edu.au/~lukes/TEXTBOOK/notes- cpp/algorithms/searching/binarysearch.html
  • 16. Binary Search Performance • Binary searches scale better than linear searches. – For each element, we half the size of the search space. • For 1024 elements: • Much better performance than associated linear search.
  • 17. Sorting • Binary searches only work on sorted data. • We must ensure the data is sorted before we can apply it. • May not be worth it for small data sets. • Must sort the data before hand. • Multiple ways to do this also. • Again, different algorithms scale differently depending on the number of elements involved.
  • 18. Bubble Sort Start at the beginning of the array Set our current position to the beginning. go through each element of the array from the beginning if our current element is greater than the next element swap them around Set the current position to be the next element Repeat until we reach the end. Won’t look at the code for this sort. Elements ‘bubble up’ into the right position based on the algorithm. Bad efficiency, with combinatorial explosion problem. Does however result in a fully sorted list.
  • 19. Algorithms • Algorithms give us the solution without the code. • That means we don’t need to worry about converting between different languages. • We have a base level description we can use for actual implementation. • They serve as a kind of ‘shorthand jargon’ for people in the business. • When you talk about a ‘bubble sort’, other programmers know what you mean.
  • 20. Efficiency • With the speed of modern processors, is efficiency really a big deal? • Bigger processors have led to more ambitious programs. • For very large sets of data, efficiency can be a real issue. • Programs that take hours and hours to run are not uncommon. • Some applications of programming so processor intensive that modern processors still cannot cope. • Realistic 3D graphics as an example
  • 21. Trade-Offs • Programming is a process of trade-offs • You can have perfect maintainability or perfect efficiency, you can’t have both. • Code that is very efficient usually has very low readability. • Important to decide for yourself where the line lies for a given program. • Safest to go for maintainability first.
  • 22. Optimisation • The process of turning slow running code into fast running code is called optimisation. • Should be done only when there is a need. • There exists in programming an informal guideline called the 80/20 rule • Used in many contexts • In this context: 80% of your CPU’s time is spent in 20% of the code • Hard to tell where critical code is • Don’t optimise first
  • 23. Summary • Efficiency often lies at the other end of the spectrum from maintainability. • Code that runs fast is not often code that is easily maintained. • Algorithms exist as a shorthand for approached to programming problems. • Along with a way of measuring efficiency. • Each program requires a different approach. • No one answer.