SlideShare a Scribd company logo
Algorithms
Reggie Niccolo Santos
UP Information Technology Development Center
(ITDC)
Slideshare: reggieniccolo.santos
Adapted from Aaron Bloomfield’s set of slides
Outline
What is an algorithm?
Algorithm 1: Maximum element
Properties of algorithms
Outline
!
Searching algorithms
Algorithm 2: Linear search
Algorithm 3: Binary search
Outline
Sorting algorithms:
Algorithm 4: Bubble sort
Algorithm 5: Insertion sort
What is an
algorithm?
“a finite set of precise
instructions for performing a
computation or for solving a
problem”
What is an
algorithm?
■
A program is one type of algorithm
All programs are algorithms
Not all algorithms are programs!
What is an
algorithm?
■
Examples:
■
directions to somebody’s house
■
a recipe for cooking a cake
■ the steps to compute the cosine
of 90° is an algorithm
Some algorithms are
harder than others
Some algorithms are easy
■
Finding the largest (or smallest)
value in a list
■
Finding a specific value in a list
Some algorithms are a bit harder
■
Sorting a list
Some algorithms are
harder than others
Some algorithms are very hard
■
Finding the shortest path between
Rizal and Quezon City
Some algorithms are essentially
impossible
■
Factoring large composite numbers
Algorithm 1:
Maximum element
Given a list, how do we find the
maximum element in the list?
!
To express the algorithm, we’ll use
pseudocode
■ Pseudocode is kinda like a
programming language, but not really
Algorithm 1:
Maximum element
Algorithm for finding the maximum element in a
list:
!
procedure max (a1, a2, …, an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
!
{max is the largest element}
Maximum element
running time
How long does this take?
!
If the list has n elements, worst case
scenario is that it takes n “steps”
■
Here, a step is considered a
single step through the list
Properties of
algorithms
Algorithms generally share a set of
properties:
■ Input: what the algorithm takes in as input
■ Output: what the algorithm produces as
output
■ Definiteness: the steps are defined
precisely
■ Correctness: should produce the correct
output
Properties of
algorithms
Algorithms generally share a set of
properties:
■ Finiteness: the steps required should be
finite
■ Effectiveness: each step must be able to
be performed in a finite amount of time
■ Generality: the algorithm should be
applicable to all problems of a
similar form
Searching
algorithms
Given a list, find a specific element in
the list
!
We will see two types
■
Linear search
a.k.a. sequential search
■
Binary search
Algorithm 2: Linear
search
Given a list, find a specific element in the list
■ List does NOT have to be sorted!
!
procedure linear_search (x: integer; a1, a2, …, an:
integers)
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
!
{location is the subscript of the term that equals x, or
it is 0 if x is not found}
Linear search
running time
How long does this take?
!
If the list has n elements, worst case
scenario is that it takes n “steps”
■
Here, a step is considered a
single step through the list
Algorithm 3: Binary
search
Given a list, find a specific element in the list
■ List MUST be sorted!
Each time it iterates through, it cuts the list in half
!
procedure binary_search (x: integer; a1
, a2
, …, an
: increasing integers)
i := 1 { i is left endpoint of search interval }
j := n{ j is right endpoint of search interval }
while i < j
begin
m := (i+j)/2 { m is the point in the middle }
if x > am
then i := m+1
else j := m
end
if x = ai
then location := i
else location := 0
!
{location is the subscript of the term that equals x, or it is 0 if x is
not found}
Algorithm 3: Binary
search
A somewhat alternative view of what
a binary search does…
Binary search
running time
How long does this take (worst case)?
!
If the list has 8 elements
■ It takes 3 steps
If the list has 16 elements
■ It takes 4 steps
If the list has 64 elements
■ It takes 6 steps
!
If the list has n elements
■ It takes log2 n steps
Sorting algorithms
Given a list, put it into some order
■ Numerical, lexicographic, etc.
!
We will see two types
■ Bubble sort
■ Insertion sort
Algorithm 4:
Bubble sort
One of the most simple sorting algorithms
■ Also one of the least efficient
It takes successive elements and “bubbles” them up
the list
!
procedure bubble_sort (a1, a2, …, an)
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
{ a1, …, an are in increasing order }
Algorithm 4:
Bubble sort
An example using physical objects…
Bubble sort running
time
Bubble sort algorithm:
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
!
Outer for loop does n-1 iterations
Inner for loop does
■ n-1 iterations the first time
■ n-2 iterations the second time
■ …
■ 1 iteration the last time
Total: (n-1) + (n-2) + (n-3) + … + 2 + 1 = (n
2
-n)/2
■ We can say that’s “about” n
2
time
Algorithm 5:
Insertion sort
Another simple (and inefficient) algorithm
It starts with a list with one element, and inserts new elements into their proper
place in the sorted part of the list
!
procedure insertion_sort (a1, a2, …, an)
for j := 2 to n
begin
i := 1
while aj > ai
i := i +1
m := aj
for k := 0 to j-i-1
aj-k := aj-k-1
ai := m
end { a1, a2, …, an are sorted }
Insertion sort
running time
Outer for loop runs n-1 times
In the inner for loop:
■
Worst case is when the while keeps i at
1, and the for loop runs lots of times
■
If i is 1, the inner for loop runs 1
time (k goes from 0 to 0) on the first
iteration, 1 time on the second, up to
n-2 times on the last iteration
Total is 1 + 2 + … + n-2 = (n-1)(n-2)/2
■
We can say that’s “about” n2
time
Comparison of
running times
Searches
■ Linear: n steps
■ Binary: log2 n steps
■ Binary search is about as fast as you can get
!
Sorts
■ Bubble: n
2
steps
■ Insertion: n
2
steps
■ There are other, more efficient, sorting techniques
In principle, the fastest are heap sort, quick sort, and merge sort
These each take take n * log2 n steps
In practice, quick sort is the fastest, followed by
merge sort
Summary
What is an algorithm?
Algorithm 1: Maximum element
Properties of algorithms
Summary
!
Searching algorithms
Algorithm 2: Linear search
Algorithm 3: Binary search
Summary
Sorting algorithms:
Algorithm 4: Bubble sort
Algorithm 5: Insertion sort
Thank you!
Questions? :D

More Related Content

What's hot

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
Simplilearn
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Quick Sort
Quick SortQuick Sort
Quick Sort
Shweta Sahu
 
The propositional calculus
The propositional calculusThe propositional calculus
The propositional calculus
Anju Kanjirathingal
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
irdginfo
 
INTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third EditionINTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third Edition
PHI Learning Pvt. Ltd.
 
Array sorting
Array sortingArray sorting
Array sorting
ALI RAZA
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
Prof. Dr. K. Adisesha
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Vicente García Díaz
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
Simplilearn
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
geeortiz
 

What's hot (20)

Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
Scikit-Learn Tutorial | Machine Learning With Scikit-Learn | Sklearn | Python...
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Recursion
RecursionRecursion
Recursion
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
The propositional calculus
The propositional calculusThe propositional calculus
The propositional calculus
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
INTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third EditionINTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third Edition
 
Array sorting
Array sortingArray sorting
Array sorting
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 

Viewers also liked

National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DC
Shivakumar Patil
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Edward Blurock
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
John Lynch
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
American Astronautical Society
 
sPen Data Management
sPen Data ManagementsPen Data Management
sPen Data Management
Uladzimir Slabin
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"John Lynch
 
History of Creationism, Parts II & III
History of Creationism, Parts II & IIIHistory of Creationism, Parts II & III
History of Creationism, Parts II & IIIJohn Lynch
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2mtaft
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
Samsung Open Source Group
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011
Mike Blumenthal
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
Shahi Raz Akhtar
 
The Scientific Revolution
The Scientific RevolutionThe Scientific Revolution
The Scientific RevolutionJohn Lynch
 
One Long Argument
One Long ArgumentOne Long Argument
One Long ArgumentJohn Lynch
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.
Saba SEO
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
Andreas Jaffke
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
John Lynch
 

Viewers also liked (20)

National Air And Space Museum Washington DC
National Air And Space Museum Washington DCNational Air And Space Museum Washington DC
National Air And Space Museum Washington DC
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...How We Got Where We Are: 40 Years of Planning...
How We Got Where We Are: 40 Years of Planning...
 
sPen Data Management
sPen Data ManagementsPen Data Management
sPen Data Management
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
 
History of Creationism, Parts II & III
History of Creationism, Parts II & IIIHistory of Creationism, Parts II & III
History of Creationism, Parts II & III
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
 
History of Google Local from 2004-2011
History of Google Local from 2004-2011 History of Google Local from 2004-2011
History of Google Local from 2004-2011
 
Introduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_aIntroduction to Information Technology ch 02_a
Introduction to Information Technology ch 02_a
 
The Scientific Revolution
The Scientific RevolutionThe Scientific Revolution
The Scientific Revolution
 
One Long Argument
One Long ArgumentOne Long Argument
One Long Argument
 
Google
GoogleGoogle
Google
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
 

Similar to Algorithms - Aaron Bloomfield

21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
ashwinraiyani1
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
Johnny Jean Tigas
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
DaniloMislosAlbay
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
mrizwan38
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Algorithms
AlgorithmsAlgorithms
Algorithms
WaqarzadAa
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
my docoment
my docomentmy docoment
my docoment
NeeshanYonzan
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
L1803016468
L1803016468L1803016468
L1803016468
IOSR Journals
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 

Similar to Algorithms - Aaron Bloomfield (20)

21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
21-algorithms.ppt
21-algorithms.ppt21-algorithms.ppt
21-algorithms.ppt
 
Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++Algorithm, Pseudocode and Flowcharting in C++
Algorithm, Pseudocode and Flowcharting in C++
 
21-algorithms (1).ppt
21-algorithms (1).ppt21-algorithms (1).ppt
21-algorithms (1).ppt
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
my docoment
my docomentmy docoment
my docoment
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
L1803016468
L1803016468L1803016468
L1803016468
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 

More from Reggie Niccolo Santos

Securing PHP Applications
Securing PHP ApplicationsSecuring PHP Applications
Securing PHP Applications
Reggie Niccolo Santos
 
Introduction to Web 2.0
Introduction to Web 2.0Introduction to Web 2.0
Introduction to Web 2.0
Reggie Niccolo Santos
 
UI / UX Engineering for Web Applications
UI / UX Engineering for Web ApplicationsUI / UX Engineering for Web Applications
UI / UX Engineering for Web Applications
Reggie Niccolo Santos
 
Computability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable FunctionComputability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable Function
Reggie Niccolo Santos
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
Reggie Niccolo Santos
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
Reggie Niccolo Santos
 
Computational Thinking and Data Representations
Computational Thinking and Data RepresentationsComputational Thinking and Data Representations
Computational Thinking and Data Representations
Reggie Niccolo Santos
 
Number Systems
Number SystemsNumber Systems
Number Systems
Reggie Niccolo Santos
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game Development
Reggie Niccolo Santos
 
Application Testing
Application TestingApplication Testing
Application Testing
Reggie Niccolo Santos
 
Application Security
Application SecurityApplication Security
Application Security
Reggie Niccolo Santos
 
PHP MVC
PHP MVCPHP MVC
MySQL Transactions
MySQL TransactionsMySQL Transactions
MySQL Transactions
Reggie Niccolo Santos
 
MySQL Cursors
MySQL CursorsMySQL Cursors
MySQL Cursors
Reggie Niccolo Santos
 
MySQL Views
MySQL ViewsMySQL Views

More from Reggie Niccolo Santos (15)

Securing PHP Applications
Securing PHP ApplicationsSecuring PHP Applications
Securing PHP Applications
 
Introduction to Web 2.0
Introduction to Web 2.0Introduction to Web 2.0
Introduction to Web 2.0
 
UI / UX Engineering for Web Applications
UI / UX Engineering for Web ApplicationsUI / UX Engineering for Web Applications
UI / UX Engineering for Web Applications
 
Computability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable FunctionComputability - Tractable, Intractable and Non-computable Function
Computability - Tractable, Intractable and Non-computable Function
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Computational Thinking and Data Representations
Computational Thinking and Data RepresentationsComputational Thinking and Data Representations
Computational Thinking and Data Representations
 
Number Systems
Number SystemsNumber Systems
Number Systems
 
Introduction to Game Development
Introduction to Game DevelopmentIntroduction to Game Development
Introduction to Game Development
 
Application Testing
Application TestingApplication Testing
Application Testing
 
Application Security
Application SecurityApplication Security
Application Security
 
PHP MVC
PHP MVCPHP MVC
PHP MVC
 
MySQL Transactions
MySQL TransactionsMySQL Transactions
MySQL Transactions
 
MySQL Cursors
MySQL CursorsMySQL Cursors
MySQL Cursors
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
 

Recently uploaded

Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 

Recently uploaded (20)

Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 

Algorithms - Aaron Bloomfield

  • 1. Algorithms Reggie Niccolo Santos UP Information Technology Development Center (ITDC) Slideshare: reggieniccolo.santos Adapted from Aaron Bloomfield’s set of slides
  • 2. Outline What is an algorithm? Algorithm 1: Maximum element Properties of algorithms
  • 3. Outline ! Searching algorithms Algorithm 2: Linear search Algorithm 3: Binary search
  • 4. Outline Sorting algorithms: Algorithm 4: Bubble sort Algorithm 5: Insertion sort
  • 5. What is an algorithm? “a finite set of precise instructions for performing a computation or for solving a problem”
  • 6. What is an algorithm? ■ A program is one type of algorithm All programs are algorithms Not all algorithms are programs!
  • 7. What is an algorithm? ■ Examples: ■ directions to somebody’s house ■ a recipe for cooking a cake ■ the steps to compute the cosine of 90° is an algorithm
  • 8. Some algorithms are harder than others Some algorithms are easy ■ Finding the largest (or smallest) value in a list ■ Finding a specific value in a list Some algorithms are a bit harder ■ Sorting a list
  • 9. Some algorithms are harder than others Some algorithms are very hard ■ Finding the shortest path between Rizal and Quezon City Some algorithms are essentially impossible ■ Factoring large composite numbers
  • 10. Algorithm 1: Maximum element Given a list, how do we find the maximum element in the list? ! To express the algorithm, we’ll use pseudocode ■ Pseudocode is kinda like a programming language, but not really
  • 11. Algorithm 1: Maximum element Algorithm for finding the maximum element in a list: ! procedure max (a1, a2, …, an: integers) max := a1 for i := 2 to n if max < ai then max := ai ! {max is the largest element}
  • 12. Maximum element running time How long does this take? ! If the list has n elements, worst case scenario is that it takes n “steps” ■ Here, a step is considered a single step through the list
  • 13. Properties of algorithms Algorithms generally share a set of properties: ■ Input: what the algorithm takes in as input ■ Output: what the algorithm produces as output ■ Definiteness: the steps are defined precisely ■ Correctness: should produce the correct output
  • 14. Properties of algorithms Algorithms generally share a set of properties: ■ Finiteness: the steps required should be finite ■ Effectiveness: each step must be able to be performed in a finite amount of time ■ Generality: the algorithm should be applicable to all problems of a similar form
  • 15. Searching algorithms Given a list, find a specific element in the list ! We will see two types ■ Linear search a.k.a. sequential search ■ Binary search
  • 16. Algorithm 2: Linear search Given a list, find a specific element in the list ■ List does NOT have to be sorted! ! procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 ! {location is the subscript of the term that equals x, or it is 0 if x is not found}
  • 17. Linear search running time How long does this take? ! If the list has n elements, worst case scenario is that it takes n “steps” ■ Here, a step is considered a single step through the list
  • 18. Algorithm 3: Binary search Given a list, find a specific element in the list ■ List MUST be sorted! Each time it iterates through, it cuts the list in half ! procedure binary_search (x: integer; a1 , a2 , …, an : increasing integers) i := 1 { i is left endpoint of search interval } j := n{ j is right endpoint of search interval } while i < j begin m := (i+j)/2 { m is the point in the middle } if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 ! {location is the subscript of the term that equals x, or it is 0 if x is not found}
  • 19. Algorithm 3: Binary search A somewhat alternative view of what a binary search does…
  • 20. Binary search running time How long does this take (worst case)? ! If the list has 8 elements ■ It takes 3 steps If the list has 16 elements ■ It takes 4 steps If the list has 64 elements ■ It takes 6 steps ! If the list has n elements ■ It takes log2 n steps
  • 21. Sorting algorithms Given a list, put it into some order ■ Numerical, lexicographic, etc. ! We will see two types ■ Bubble sort ■ Insertion sort
  • 22. Algorithm 4: Bubble sort One of the most simple sorting algorithms ■ Also one of the least efficient It takes successive elements and “bubbles” them up the list ! procedure bubble_sort (a1, a2, …, an) for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 { a1, …, an are in increasing order }
  • 23. Algorithm 4: Bubble sort An example using physical objects…
  • 24. Bubble sort running time Bubble sort algorithm: for i := 1 to n-1 for j := 1 to n-i if aj > aj+1 then interchange aj and aj+1 ! Outer for loop does n-1 iterations Inner for loop does ■ n-1 iterations the first time ■ n-2 iterations the second time ■ … ■ 1 iteration the last time Total: (n-1) + (n-2) + (n-3) + … + 2 + 1 = (n 2 -n)/2 ■ We can say that’s “about” n 2 time
  • 25. Algorithm 5: Insertion sort Another simple (and inefficient) algorithm It starts with a list with one element, and inserts new elements into their proper place in the sorted part of the list ! procedure insertion_sort (a1, a2, …, an) for j := 2 to n begin i := 1 while aj > ai i := i +1 m := aj for k := 0 to j-i-1 aj-k := aj-k-1 ai := m end { a1, a2, …, an are sorted }
  • 26. Insertion sort running time Outer for loop runs n-1 times In the inner for loop: ■ Worst case is when the while keeps i at 1, and the for loop runs lots of times ■ If i is 1, the inner for loop runs 1 time (k goes from 0 to 0) on the first iteration, 1 time on the second, up to n-2 times on the last iteration Total is 1 + 2 + … + n-2 = (n-1)(n-2)/2 ■ We can say that’s “about” n2 time
  • 27. Comparison of running times Searches ■ Linear: n steps ■ Binary: log2 n steps ■ Binary search is about as fast as you can get ! Sorts ■ Bubble: n 2 steps ■ Insertion: n 2 steps ■ There are other, more efficient, sorting techniques In principle, the fastest are heap sort, quick sort, and merge sort These each take take n * log2 n steps In practice, quick sort is the fastest, followed by merge sort
  • 28. Summary What is an algorithm? Algorithm 1: Maximum element Properties of algorithms
  • 29. Summary ! Searching algorithms Algorithm 2: Linear search Algorithm 3: Binary search
  • 30. Summary Sorting algorithms: Algorithm 4: Bubble sort Algorithm 5: Insertion sort