SlideShare a Scribd company logo
SEARCH AND SORT
TECHNIQUES
FERRY KEMPERMAN
NANJING FOREIGN LANGUAGE SCHOOL
APRIL 2019
ALGORITHM DESIGN: EFFICIENCY
• As a software designer you have to design algorithms that meet the following two basic
requirements:
• 1. Works according to specification (correctness)
• 2. Works in the most efficient manner
• Efficiency of an algorithm is comprised of two main factors:
• - Least possible use of resources (CPU/RAM) during execution
• - Fastest possible execution of your algorithm
When you design an algorithm your design should take efficiency into account, but how?
HOW TO DESIGN AN EFFICIENT ALGORITHM?
A FEW GUIDELINES…..
• Minimize RAM usage during execution
• Every variable / data structure used is stored in RAM during execution.
• Limit the number of variables you use in your algorithm.
• Choose the correct datatypes and data structures to store / retrieve values efficiently.
• Use the right scoping of variables. How long will variables exist in RAM during execution?
• Later more on variable scoping.
• Minimize CPU execution time
• Basic idea: the fewer instructions, the better. It is not that simple, though.
• How many assignments, comparisons etcetera are done during execution?
• What kind of expressions are evaluated and how many times?
• Every assignment / comparison is an I/O (read/write) operation to memory. Expensive!
Algorithm’s efficiency……
Fast execution of your algorithm
This is completely dependent on your algorithm design!
How many loops do you use? How many selections?
Is this really necessary? Or can you do with less?
A major impact on algorithm efficiency is the way you implement how:
- You search for elements in an array (or other data structures)
- You sort elements in an array (or other data structures)
Searching and sorting algorithms for arrays are well know design techniques in
Computer Science.
Before introducing them, let’s use a metaphor to understand the importance
of this.
SUPERMARKET METAPHOR
• You go to the supermarket to get 1 can of coke.
• Strategy 1: Start in the first aisle and look at every product of the shelf to see
if it is the can of coke. If not, proceed to second aisle.
• Strategy 2: Randomly walk around in the supermarket and try to find the can
by looking at the shelves as you pass them.
• Strategy 3: Ask clerk for the right aisle with drinks, proceed to this aisle and
scan shelves in this aisle to find a can of coke.
• Which strategy is the best and why? Which one comes second in terms of
efficiency?
• Explain your answer in terms of instructions?
SEARCHING ALGORITHMS: SEQUENTIAL SEARCH
• Suppose you have an array comprised of 5 integers
• MyArray = [5,3,6,9,10]
• You want to find a certain element, say 9, in this array.
• The array is unsorted.
• You can perform a sequential search by comparing each element in the array to the
element you are looking for.
• First element 5=9? No. Second 3=9? No, 6=9? No. 9-9? Yes. Element found at position 4.
• Basic algorithm, but requires N comparisons, where N is upper bound of array.
• Advantage: can be applied to an unsorted array!
• Sorting is expensive!
• Sequential search is also called linear search.
SEARCHING ALGORITHMS: BINARY SEARCH
• Binary search can only be performed on a sorted array
• MyArray = [4,8,12,18,25,30,34]
• To find an element, say 30, in a sorted array we can use a binary search.
• Instead of starting at the first element, we start in the middle of this array:
• Compare your element with the element in the middle, in this case 18.
• If this middle element is smaller, compare to middle element on the right, if bigger compare to
middle element on the left and so on.
• So 18<30, so smaller. Go right. [25,30,34] is the array to the right. Take middle element again. 30=30!
• We do not have to compare 30 to ALL the elements of the array. This is a huge gain in terms of
efficiency!
• Disadvantage: Only sorted arrays can do a binary search, why?
SORTING ALGORITHMS
• Sorting an array can be done in several ways.
• We will discuss three famous sorting algorithms:.
• Selection Sort
• Bubble Sort
• Insertion Sort
SELECTION SORT
Take an unsorted array.
Put marker on last element N.
Search largest element in array.
Put largest element in the last position.
Move marker to N-1.
Search for largest value.
Put largest element in position N-1.
And so on.
BUBBLE SORT
Start with unsorted array, size N
Compare element 1 and 2.
If 1 bigger than 2, flip them.
Compare element 2 and 3.
If 2 bigger than 3, flip them.
The largest element is now in
Position N.
Repeat process for N-1 elements in
Step 2. Elements N and N-1 are now
In right position.
Repeat this in step 3 for N-2 elements.
Etcetera.
INSERTION SORT
Start with unsorted array.
Take second element and
compare with preceding
elements and insert it at the
right place.
Repeat this for third, fourth
element until list is sorted.

More Related Content

Similar to Search and Sort algorithms. Bubble, Insertion, Selection.

cs702 ppt.ppt
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
JavedIqbal398171
 
Excel tips
Excel tipsExcel tips
Excel tips
laxmiraj01
 
Excel Tips 101
Excel Tips 101Excel Tips 101
Excel Tips 101
Andre Pereira
 
Sorting
SortingSorting
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
Excel Tips.pptx
Excel Tips.pptxExcel Tips.pptx
Excel Tips.pptx
SumitPaul80
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
Krish_ver2
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
Excel tips
Excel tipsExcel tips
Excel tips
Preet Sandhu
 
Algorithms
AlgorithmsAlgorithms
Algorithms
cachs_computing
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
Vivek487417
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
NagendraK18
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Sorting
SortingSorting
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
DipayanSadhu1
 
Lecture 1 computing and algorithms
Lecture 1  computing and algorithmsLecture 1  computing and algorithms
Lecture 1 computing and algorithms
alvin567
 
Alogorithm ppr slideshow
Alogorithm ppr slideshowAlogorithm ppr slideshow
Alogorithm ppr slideshow
KartikAgarwal82
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan
 
Algorithms 1
Algorithms 1Algorithms 1
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 

Similar to Search and Sort algorithms. Bubble, Insertion, Selection. (20)

cs702 ppt.ppt
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel Tips 101
Excel Tips 101Excel Tips 101
Excel Tips 101
 
Sorting
SortingSorting
Sorting
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Excel Tips.pptx
Excel Tips.pptxExcel Tips.pptx
Excel Tips.pptx
 
4.1 sequentioal search
4.1 sequentioal search4.1 sequentioal search
4.1 sequentioal search
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast exampleDS PPT - ( 1 )SORTING lgoritham techniques with bast example
DS PPT - ( 1 )SORTING lgoritham techniques with bast example
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Sorting
SortingSorting
Sorting
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Lecture 1 computing and algorithms
Lecture 1  computing and algorithmsLecture 1  computing and algorithms
Lecture 1 computing and algorithms
 
Alogorithm ppr slideshow
Alogorithm ppr slideshowAlogorithm ppr slideshow
Alogorithm ppr slideshow
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Algorithms 1
Algorithms 1Algorithms 1
Algorithms 1
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 

More from FerryKemperman

Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
FerryKemperman
 
Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)
FerryKemperman
 
Top_down_programming..............................
Top_down_programming..............................Top_down_programming..............................
Top_down_programming..............................
FerryKemperman
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
FerryKemperman
 
Structured Query Language introduction..
Structured Query Language introduction..Structured Query Language introduction..
Structured Query Language introduction..
FerryKemperman
 
SDT introduction as given at MKIS, KL, 2023
SDT introduction as given at MKIS, KL, 2023SDT introduction as given at MKIS, KL, 2023
SDT introduction as given at MKIS, KL, 2023
FerryKemperman
 
Computer Architecture Machine Cycle (1).pdf
Computer Architecture Machine Cycle (1).pdfComputer Architecture Machine Cycle (1).pdf
Computer Architecture Machine Cycle (1).pdf
FerryKemperman
 
Referential integrity in databases.pptx
Referential integrity in databases.pptxReferential integrity in databases.pptx
Referential integrity in databases.pptx
FerryKemperman
 

More from FerryKemperman (8)

Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)Working with files (concepts/pseudocode/python)
Working with files (concepts/pseudocode/python)
 
Top_down_programming..............................
Top_down_programming..............................Top_down_programming..............................
Top_down_programming..............................
 
OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........OOP in Python, a beginners guide..........
OOP in Python, a beginners guide..........
 
Structured Query Language introduction..
Structured Query Language introduction..Structured Query Language introduction..
Structured Query Language introduction..
 
SDT introduction as given at MKIS, KL, 2023
SDT introduction as given at MKIS, KL, 2023SDT introduction as given at MKIS, KL, 2023
SDT introduction as given at MKIS, KL, 2023
 
Computer Architecture Machine Cycle (1).pdf
Computer Architecture Machine Cycle (1).pdfComputer Architecture Machine Cycle (1).pdf
Computer Architecture Machine Cycle (1).pdf
 
Referential integrity in databases.pptx
Referential integrity in databases.pptxReferential integrity in databases.pptx
Referential integrity in databases.pptx
 

Recently uploaded

All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
widenerjobeyrl638
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
michniczscribd
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
confluent
 
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
Luigi Fugaro
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
Luigi Fugaro
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 

Recently uploaded (20)

All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
 
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
Voxxed Days Trieste 2024 - Unleashing the Power of Vector Search and Semantic...
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 

Search and Sort algorithms. Bubble, Insertion, Selection.

  • 1. SEARCH AND SORT TECHNIQUES FERRY KEMPERMAN NANJING FOREIGN LANGUAGE SCHOOL APRIL 2019
  • 2. ALGORITHM DESIGN: EFFICIENCY • As a software designer you have to design algorithms that meet the following two basic requirements: • 1. Works according to specification (correctness) • 2. Works in the most efficient manner • Efficiency of an algorithm is comprised of two main factors: • - Least possible use of resources (CPU/RAM) during execution • - Fastest possible execution of your algorithm When you design an algorithm your design should take efficiency into account, but how?
  • 3. HOW TO DESIGN AN EFFICIENT ALGORITHM? A FEW GUIDELINES….. • Minimize RAM usage during execution • Every variable / data structure used is stored in RAM during execution. • Limit the number of variables you use in your algorithm. • Choose the correct datatypes and data structures to store / retrieve values efficiently. • Use the right scoping of variables. How long will variables exist in RAM during execution? • Later more on variable scoping. • Minimize CPU execution time • Basic idea: the fewer instructions, the better. It is not that simple, though. • How many assignments, comparisons etcetera are done during execution? • What kind of expressions are evaluated and how many times? • Every assignment / comparison is an I/O (read/write) operation to memory. Expensive!
  • 4. Algorithm’s efficiency…… Fast execution of your algorithm This is completely dependent on your algorithm design! How many loops do you use? How many selections? Is this really necessary? Or can you do with less? A major impact on algorithm efficiency is the way you implement how: - You search for elements in an array (or other data structures) - You sort elements in an array (or other data structures) Searching and sorting algorithms for arrays are well know design techniques in Computer Science. Before introducing them, let’s use a metaphor to understand the importance of this.
  • 5. SUPERMARKET METAPHOR • You go to the supermarket to get 1 can of coke. • Strategy 1: Start in the first aisle and look at every product of the shelf to see if it is the can of coke. If not, proceed to second aisle. • Strategy 2: Randomly walk around in the supermarket and try to find the can by looking at the shelves as you pass them. • Strategy 3: Ask clerk for the right aisle with drinks, proceed to this aisle and scan shelves in this aisle to find a can of coke. • Which strategy is the best and why? Which one comes second in terms of efficiency? • Explain your answer in terms of instructions?
  • 6. SEARCHING ALGORITHMS: SEQUENTIAL SEARCH • Suppose you have an array comprised of 5 integers • MyArray = [5,3,6,9,10] • You want to find a certain element, say 9, in this array. • The array is unsorted. • You can perform a sequential search by comparing each element in the array to the element you are looking for. • First element 5=9? No. Second 3=9? No, 6=9? No. 9-9? Yes. Element found at position 4. • Basic algorithm, but requires N comparisons, where N is upper bound of array. • Advantage: can be applied to an unsorted array! • Sorting is expensive! • Sequential search is also called linear search.
  • 7. SEARCHING ALGORITHMS: BINARY SEARCH • Binary search can only be performed on a sorted array • MyArray = [4,8,12,18,25,30,34] • To find an element, say 30, in a sorted array we can use a binary search. • Instead of starting at the first element, we start in the middle of this array: • Compare your element with the element in the middle, in this case 18. • If this middle element is smaller, compare to middle element on the right, if bigger compare to middle element on the left and so on. • So 18<30, so smaller. Go right. [25,30,34] is the array to the right. Take middle element again. 30=30! • We do not have to compare 30 to ALL the elements of the array. This is a huge gain in terms of efficiency! • Disadvantage: Only sorted arrays can do a binary search, why?
  • 8.
  • 9. SORTING ALGORITHMS • Sorting an array can be done in several ways. • We will discuss three famous sorting algorithms:. • Selection Sort • Bubble Sort • Insertion Sort
  • 10. SELECTION SORT Take an unsorted array. Put marker on last element N. Search largest element in array. Put largest element in the last position. Move marker to N-1. Search for largest value. Put largest element in position N-1. And so on.
  • 11. BUBBLE SORT Start with unsorted array, size N Compare element 1 and 2. If 1 bigger than 2, flip them. Compare element 2 and 3. If 2 bigger than 3, flip them. The largest element is now in Position N. Repeat process for N-1 elements in Step 2. Elements N and N-1 are now In right position. Repeat this in step 3 for N-2 elements. Etcetera.
  • 12. INSERTION SORT Start with unsorted array. Take second element and compare with preceding elements and insert it at the right place. Repeat this for third, fourth element until list is sorted.