SlideShare a Scribd company logo
1 of 26
1
Algorithms
2
What is an algorithm?
An algorithm is “a finite set of precise
instructions for performing a computation or for
solving a problem”
 A program is one type of algorithm
All programs are algorithms
Not all algorithms are programs!
 Directions to somebody’s house is an algorithm
 A recipe for cooking a cake is an algorithm
 The steps to compute the cosine of 90° is an
algorithm
3
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 very hard
 Finding the shortest path between Miami and Seattle
Some algorithms are essentially impossible
 Factoring large composite numbers
In section 2.2, we’ll see how to rate how “hard”
algorithms are
4
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
5
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}
6
procedure max (a1, a2, …, an: integers)
max := a1
for i := 2 to n
if max < ai then max := ai
max := a1
for i := 2 to n
if max < ai then max := ai
Algorithm 1: Maximum element
4 1 7 0 5 2 9 3 6 8
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
max
i 2
3
4
5
6
7
8
9
10
4
7
9
7
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
8
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
 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
9
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
10
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}
11
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
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
Algorithm 2: Linear search, take 1
4 1 7 0 5 2 9 3 6 8
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
i 2
3
4
5
6
7
8
1
x 3
location 8
12
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
i := 1
while ( i ≤ n and x ≠ ai )
i := i + 1
if i ≤ n then location := i
else location := 0
Algorithm 2: Linear search, take 2
4 1 7 0 5 2 9 3 6 8
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
i 2
3
4
5
6
7
8
9
10
1
x 11
location 0
11
13
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
14
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}
15
Algorithm 3: Binary search, take 1
2 4 6 8 10 12 14 16 18 20
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
i j
m
i := 1
j := n
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
while i < j
begin
m := (i+j)/2
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
i := 1
j := n
while i < j
begin
m := (i+j)/2
if x > am then i := m+1
else j := m
end
if x = ai then location := i
1
x 14
10
5
6 8 8
7 7
6
7
location 7
16
Algorithm 3: Binary search, take 2
2 4 6 8 10 12 14 16 18 20
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
i j
m
i := 1
j := n
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
while i < j
begin
m := (i+j)/2
if x > am then i := m+1
else j := m
end
if x = ai then location := i
else location := 0
i := 1
j := n
while i < j
begin
m := (i+j)/2
if x > am then i := m+1
else j := m
end
if x = ai then location := I
else location := 0
1
x 15
10
5
6 8 8
7
location 0
8
17
Algorithm 3: Binary search
A somewhat alternative view of what a
binary search does…
18
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
19
Sorting algorithms
Given a list, put it into some order
 Numerical, lexicographic, etc.
We will see two types
 Bubble sort
 Insertion sort
20
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 }
22
Algorithm 4: Bubble sort
An example using physical objects…
23
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 = (n2-n)/2
 We can say that’s “about” n2 time
24
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 }
take successive elements in the list
find where that element should be
in the sorted portion of the list
move all elements in the sorted
portion of the list that are greater
than the current element up by one
put the current element into it’s proper place in the sorted portion of the list
25
Insertion sort running time
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 }
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
26
Comparison of running times
Searches
 Linear: n steps
 Binary: log2 n steps
 Binary search is about as fast as you can get
Sorts
 Bubble: n2 steps
 Insertion: n2 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
https://www.projectpro.io/projects/data-
science-projects/machine-learning-
projects-in-
python?utm_source=blog397&utm_mediu
m=projectpagebutton&utm_campaign=mo
bstac
27

More Related Content

Similar to 21-algorithms.ppt

In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfarpitaeron555
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 

Similar to 21-algorithms.ppt (20)

Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
Searching
Searching Searching
Searching
 
Searching
SearchingSearching
Searching
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Ada notes
Ada notesAda notes
Ada notes
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
L1803016468
L1803016468L1803016468
L1803016468
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
Sorting
SortingSorting
Sorting
 
Selection sort
Selection sortSelection sort
Selection sort
 

Recently uploaded

Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknowmakika9823
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 

Recently uploaded (20)

Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 

21-algorithms.ppt

  • 2. 2 What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem”  A program is one type of algorithm All programs are algorithms Not all algorithms are programs!  Directions to somebody’s house is an algorithm  A recipe for cooking a cake is an algorithm  The steps to compute the cosine of 90° is an algorithm
  • 3. 3 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 very hard  Finding the shortest path between Miami and Seattle Some algorithms are essentially impossible  Factoring large composite numbers In section 2.2, we’ll see how to rate how “hard” algorithms are
  • 4. 4 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
  • 5. 5 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}
  • 6. 6 procedure max (a1, a2, …, an: integers) max := a1 for i := 2 to n if max < ai then max := ai max := a1 for i := 2 to n if max < ai then max := ai Algorithm 1: Maximum element 4 1 7 0 5 2 9 3 6 8 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 max i 2 3 4 5 6 7 8 9 10 4 7 9
  • 7. 7 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
  • 8. 8 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  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
  • 9. 9 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
  • 10. 10 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}
  • 11. 11 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 i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 Algorithm 2: Linear search, take 1 4 1 7 0 5 2 9 3 6 8 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 i 2 3 4 5 6 7 8 1 x 3 location 8
  • 12. 12 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 i := 1 while ( i ≤ n and x ≠ ai ) i := i + 1 if i ≤ n then location := i else location := 0 Algorithm 2: Linear search, take 2 4 1 7 0 5 2 9 3 6 8 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 i 2 3 4 5 6 7 8 9 10 1 x 11 location 0 11
  • 13. 13 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
  • 14. 14 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}
  • 15. 15 Algorithm 3: Binary search, take 1 2 4 6 8 10 12 14 16 18 20 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 i j m i := 1 j := n procedure binary_search (x: integer; a1, a2, …, an: increasing integers) while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 i := 1 j := n while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i 1 x 14 10 5 6 8 8 7 7 6 7 location 7
  • 16. 16 Algorithm 3: Binary search, take 2 2 4 6 8 10 12 14 16 18 20 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 i j m i := 1 j := n procedure binary_search (x: integer; a1, a2, …, an: increasing integers) while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := i else location := 0 i := 1 j := n while i < j begin m := (i+j)/2 if x > am then i := m+1 else j := m end if x = ai then location := I else location := 0 1 x 15 10 5 6 8 8 7 location 0 8
  • 17. 17 Algorithm 3: Binary search A somewhat alternative view of what a binary search does…
  • 18. 18 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
  • 19. 19 Sorting algorithms Given a list, put it into some order  Numerical, lexicographic, etc. We will see two types  Bubble sort  Insertion sort
  • 20. 20 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 }
  • 21. 22 Algorithm 4: Bubble sort An example using physical objects…
  • 22. 23 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 = (n2-n)/2  We can say that’s “about” n2 time
  • 23. 24 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 } take successive elements in the list find where that element should be in the sorted portion of the list move all elements in the sorted portion of the list that are greater than the current element up by one put the current element into it’s proper place in the sorted portion of the list
  • 24. 25 Insertion sort running time 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 } 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
  • 25. 26 Comparison of running times Searches  Linear: n steps  Binary: log2 n steps  Binary search is about as fast as you can get Sorts  Bubble: n2 steps  Insertion: n2 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