SlideShare a Scribd company logo
Algorithms
Discrete Structures
RIPHAH INTERNATIONAL UNIVERSITY LAHORE
Algorithms
What is an algorithm?
An algorithm is a finite set of precise instructions for performing a
computation or for solving a problem.
Algorithms
 Properties of algorithms:
• Input from a specified set,
• Output from a specified set (solution),
• Definiteness of every step in the computation,
• Correctness of output for every possible input,
• Finiteness of the number of calculation steps,
• Effectiveness of each calculation step and
• Generality for a class of problems.
Pseudocode
 An algorithm can also be described using a computer language.
Understanding an algorithm is complicated and difficult.
 Algorithm can be translated into any programming language. As
many programming languages are in common use, so instead of
using a particular computer language to specify algorithms, a form
of Pseudocode, will be used in this book.
 Pseudocode provides an intermediate step between an English
language description of an algorithm and an implementation of this
algorithm in a programming language.
Pseudocode
 Algorithm 1: Finding the Maximum Element in a Finite Sequence.
 procedure max(a1, a2, . . . , an: integers)
 max := a1
 for i := 2 to n
 if max < ai then max := ai
 return max{max is the largest element}
Pseudocode
 3 , 4 , 8 , 1 , 6 , 7 , 15 , 5 , 8 , 2
i ai max
3
2 (a2) 4 (3<4) 4
3 (a3) 8 (4<8) 8
4 (a4) 1 (8<1) 8
5 (a5) 6 (8<6) 8
6 (a6) 7 (8<7) 8
7 (a7) 15 (8<15) 15
8 (a8) 5 (15<5)15
9 (a9) 8 (15<8)15
10 (a10) 2 (15<2) 15
Linear Search:
 It is also called sequential search. It searches an element
sequentially by comparing the searching element with each
element in the list one by one.
 procedure linear search(x: integer, a1, a2, . . . , an: distinct integers)
 i := 1
 while (i ≤ n and x ≠ ai )
 i := i + 1
 if i ≤ n then location := i
 else location := 0
 return location {location is the subscript of the term that equals x,
or is 0 if x is not found}
Linear Search:
Search 5 from list 2 , 7 , 4 , 10 , 5 , 9
x=5 a1 a2 a3 a4 a5 a6
i=1 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=2 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=3 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=4 2 7 4 10 5 9
x=5 a1 a2 a3 a4 a5 a6
i=5 2 7 4 10 5 9
Element is found at Location=5
Linear Search:
Search 3 from list 2 , 7 , 4 , 10 , 5 , 9
x=3 a1 a2 a3 a4 a5 a6
i=1 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=2 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=3 2 7 4 10 5 9
Linear Search:
x=3 a1 a2 a3 a4 a5 a6
i=4 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=5 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=6 2 7 4 10 5 9
x=3 a1 a2 a3 a4 a5 a6
i=7 2 7 4 10 5 9
Element is not found Location=0
Binary Search:
 Binary Search algorithm is used to search an element from a list of
elements.
 This algorithm can be used when the list has terms occurring in
increasing order.
 It proceeds by comparing the element to be located to the middle
term of the list.
 The list is then split into two smaller sub lists of same size, or one of
these smaller lists has one fewer term than other.
 The search continues by restricting the search to the appropriate
sub lists based on the comparison of the element to be located and
the middle term.
Binary Search:
 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
 m :=
 if x > am then i := m + 1
 else j := m
 if x = ai then location := i
 else location := 0
 return location{location is the subscript i of the term ai equal to x, or 0 if x is not
found}
Binary Search:
Search 18 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 18>8 and i<j so
i m j
2 3 5 8 10 15 18 30
As 18>15 and i<j so
i m j
2 3 5 8 10 15 18 30
Element found at location 7
Binary Search:
Search 3 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 3<8 and i<j so
i m j
2 3 5 8 10 15 18 30
Element found at location 2
Binary Search:
Search 6 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30
i m j
2 3 5 8 10 15 18 30
As 6<8 and i<j so
i m j
2 3 5 8 10 15 18 30
6>3 and i<j so
i m j
2 3 5 8 10 15 18 30
6>5 and i<j
i m j
2 3 5 8 10 15 18 30
As i=j so element not found in the list
Sorting
 Sorting is putting the elements into a list in which the elements are in
increasing order.
Bubble Sort:
 The bubble sort is one of the simplest sorting algorithms, but not one
of the most efficient.
 It puts a list into increasing order by successively comparing
adjacent elements, interchanging them if they are in the wrong
order.
 To carry out the bubble sort, we perform the basic operation that is,
interchanging a larger element with a smaller one following it,
starting at the beginning of the list, for full pass.
 We iterate this procedure until the sort is complete.
Bubble Sort:
 procedure bubblesort(a1, . . . , an : real numbers with n ≥ 2)
 for i := 1 to n − 1
 for j := 1 to n − i
 if aj > aj+1 then interchange aj and aj+1
 {a1, . . . , an is in increasing order}
Bubble Sort:
Sort the list 4 , 7 , 3 , 5 , 2 using Bubble Sort
Pass 1:
4 7 3 5 2
4 7 3 5 2
4 7 3 5 2
4 3 7 5 2
4 3 5 7 2
4 3 5 2 7
Bubble Sort:
Pass2:
4 3 5 2 7
4 3 5 2 7
3 4 5 2 7
3 4 5 2 7
3 4 2 5 7
Pass3:
3 4 2 5 7
3 4 2 5 7
3 4 2 5 7
3 2 4 5 7
Bubble Sort:
Pass4:
3 2 4 5 7
3 2 4 5 7
2 3 4 5 7
Sorted list is
2 3 4 5 7
Insertion Sort:
 Insertion Sort is a simple sorting algorithm, but it is usually not the most
efficient.
 To sort a list with n elements, the insertion sort begins with the second
element.
 This second element is compared with the first element and inserted
before the first element if it is smaller than the first element and after
the first element if it is greater than the first element.
 At this point, the first two elements are in correct order.
 The third element is then compared with the first element, and if it is
larger than the first element, it is compared with the second element,
it is inserted into the correct position among the first three elements
and so on.
Insertion Sort:
 procedure insertion sort(a1, a2, . . . , an: real numbers with n ≥ 2)
 for j := 2 to n
 i := 1
 while aj > ai
 i := i + 1
 m := aj
 for k := 0 to j − i − 1
 aj−k := aj−k−1
 ai := m
 {a1, . . . , an is in increasing order}
Insertion Sort:
Sort the list 4 , 7 , 3 , 5 , 2 using Insertion Sort.
Pass 1:
4 7 3 5 2
4 7 3 5 2
4 7 3 5 2
Pass 2:
4 7 3 5 2
4 7 3 5 2
3 4 7 5 2
Insertion Sort:
Pass 3:
3 4 7 5 2
3 4 7 5 2
3 4 5 7 2
Pass 4:
3 4 5 7 2
3 4 5 5 2
2 3 4 5 7
Sorted list is
2 3 4 5 7
Ad

More Related Content

Similar to Lecture of algorithms and problem solving (20)

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
 
Searching and Sorting algorithms and working
Searching and Sorting algorithms and workingSearching and Sorting algorithms and working
Searching and Sorting algorithms and working
RitikaLohiya2
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjkAJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Algorithms
AlgorithmsAlgorithms
Algorithms
WaqarzadAa
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptxUNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptxUNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
Searching
Searching Searching
Searching
CGC Technical campus,Mohali
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
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
 
Searching and Sorting algorithms and working
Searching and Sorting algorithms and workingSearching and Sorting algorithms and working
Searching and Sorting algorithms and working
RitikaLohiya2
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjkAJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering studentsIntroduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
geraldjamesignacio2
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptxUNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptxUNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
smruti sarangi
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 

Recently uploaded (20)

Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdfGROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
kemimafe11
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFTDeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
Kyohei Ito
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx698642933-DdocfordownloadEEP-FAKE-PPT.pptx
698642933-DdocfordownloadEEP-FAKE-PPT.pptx
speedcomcyber25
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdfGROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
GROUP 2 - MANUFACTURE OF LIME, GYPSUM AND CEMENT.pdf
kemimafe11
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Understand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panelUnderstand water laser communication using Arduino laser and solar panel
Understand water laser communication using Arduino laser and solar panel
NaveenBotsa
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFTDeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
DeFAIMint | 🤖Mint to DeFAI. Vibe Trading as NFT
Kyohei Ito
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptxUnleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
Unleashing the Power of Salesforce Flows &amp_ Slack Integration!.pptx
SanjeetMishra29
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
860556374-10280271.pptx PETROLEUM COKE CALCINATION PLANT
Pierre Celestin Eyock
 
Ad

Lecture of algorithms and problem solving

  • 2. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a problem.
  • 3. Algorithms  Properties of algorithms: • Input from a specified set, • Output from a specified set (solution), • Definiteness of every step in the computation, • Correctness of output for every possible input, • Finiteness of the number of calculation steps, • Effectiveness of each calculation step and • Generality for a class of problems.
  • 4. Pseudocode  An algorithm can also be described using a computer language. Understanding an algorithm is complicated and difficult.  Algorithm can be translated into any programming language. As many programming languages are in common use, so instead of using a particular computer language to specify algorithms, a form of Pseudocode, will be used in this book.  Pseudocode provides an intermediate step between an English language description of an algorithm and an implementation of this algorithm in a programming language.
  • 5. Pseudocode  Algorithm 1: Finding the Maximum Element in a Finite Sequence.  procedure max(a1, a2, . . . , an: integers)  max := a1  for i := 2 to n  if max < ai then max := ai  return max{max is the largest element}
  • 6. Pseudocode  3 , 4 , 8 , 1 , 6 , 7 , 15 , 5 , 8 , 2 i ai max 3 2 (a2) 4 (3<4) 4 3 (a3) 8 (4<8) 8 4 (a4) 1 (8<1) 8 5 (a5) 6 (8<6) 8 6 (a6) 7 (8<7) 8 7 (a7) 15 (8<15) 15 8 (a8) 5 (15<5)15 9 (a9) 8 (15<8)15 10 (a10) 2 (15<2) 15
  • 7. Linear Search:  It is also called sequential search. It searches an element sequentially by comparing the searching element with each element in the list one by one.  procedure linear search(x: integer, a1, a2, . . . , an: distinct integers)  i := 1  while (i ≤ n and x ≠ ai )  i := i + 1  if i ≤ n then location := i  else location := 0  return location {location is the subscript of the term that equals x, or is 0 if x is not found}
  • 8. Linear Search: Search 5 from list 2 , 7 , 4 , 10 , 5 , 9 x=5 a1 a2 a3 a4 a5 a6 i=1 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=2 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=3 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=4 2 7 4 10 5 9 x=5 a1 a2 a3 a4 a5 a6 i=5 2 7 4 10 5 9 Element is found at Location=5
  • 9. Linear Search: Search 3 from list 2 , 7 , 4 , 10 , 5 , 9 x=3 a1 a2 a3 a4 a5 a6 i=1 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=2 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=3 2 7 4 10 5 9
  • 10. Linear Search: x=3 a1 a2 a3 a4 a5 a6 i=4 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=5 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=6 2 7 4 10 5 9 x=3 a1 a2 a3 a4 a5 a6 i=7 2 7 4 10 5 9 Element is not found Location=0
  • 11. Binary Search:  Binary Search algorithm is used to search an element from a list of elements.  This algorithm can be used when the list has terms occurring in increasing order.  It proceeds by comparing the element to be located to the middle term of the list.  The list is then split into two smaller sub lists of same size, or one of these smaller lists has one fewer term than other.  The search continues by restricting the search to the appropriate sub lists based on the comparison of the element to be located and the middle term.
  • 12. Binary Search:  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  m :=  if x > am then i := m + 1  else j := m  if x = ai then location := i  else location := 0  return location{location is the subscript i of the term ai equal to x, or 0 if x is not found}
  • 13. Binary Search: Search 18 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 18>8 and i<j so i m j 2 3 5 8 10 15 18 30 As 18>15 and i<j so i m j 2 3 5 8 10 15 18 30 Element found at location 7
  • 14. Binary Search: Search 3 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 3<8 and i<j so i m j 2 3 5 8 10 15 18 30 Element found at location 2
  • 15. Binary Search: Search 6 from sequence 2 , 3 , 5 , 8 , 10 , 15 , 18 , 30 i m j 2 3 5 8 10 15 18 30 As 6<8 and i<j so i m j 2 3 5 8 10 15 18 30 6>3 and i<j so i m j 2 3 5 8 10 15 18 30 6>5 and i<j i m j 2 3 5 8 10 15 18 30 As i=j so element not found in the list
  • 16. Sorting  Sorting is putting the elements into a list in which the elements are in increasing order.
  • 17. Bubble Sort:  The bubble sort is one of the simplest sorting algorithms, but not one of the most efficient.  It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order.  To carry out the bubble sort, we perform the basic operation that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for full pass.  We iterate this procedure until the sort is complete.
  • 18. Bubble Sort:  procedure bubblesort(a1, . . . , an : real numbers with n ≥ 2)  for i := 1 to n − 1  for j := 1 to n − i  if aj > aj+1 then interchange aj and aj+1  {a1, . . . , an is in increasing order}
  • 19. Bubble Sort: Sort the list 4 , 7 , 3 , 5 , 2 using Bubble Sort Pass 1: 4 7 3 5 2 4 7 3 5 2 4 7 3 5 2 4 3 7 5 2 4 3 5 7 2 4 3 5 2 7
  • 20. Bubble Sort: Pass2: 4 3 5 2 7 4 3 5 2 7 3 4 5 2 7 3 4 5 2 7 3 4 2 5 7 Pass3: 3 4 2 5 7 3 4 2 5 7 3 4 2 5 7 3 2 4 5 7
  • 21. Bubble Sort: Pass4: 3 2 4 5 7 3 2 4 5 7 2 3 4 5 7 Sorted list is 2 3 4 5 7
  • 22. Insertion Sort:  Insertion Sort is a simple sorting algorithm, but it is usually not the most efficient.  To sort a list with n elements, the insertion sort begins with the second element.  This second element is compared with the first element and inserted before the first element if it is smaller than the first element and after the first element if it is greater than the first element.  At this point, the first two elements are in correct order.  The third element is then compared with the first element, and if it is larger than the first element, it is compared with the second element, it is inserted into the correct position among the first three elements and so on.
  • 23. Insertion Sort:  procedure insertion sort(a1, a2, . . . , an: real numbers with n ≥ 2)  for j := 2 to n  i := 1  while aj > ai  i := i + 1  m := aj  for k := 0 to j − i − 1  aj−k := aj−k−1  ai := m  {a1, . . . , an is in increasing order}
  • 24. Insertion Sort: Sort the list 4 , 7 , 3 , 5 , 2 using Insertion Sort. Pass 1: 4 7 3 5 2 4 7 3 5 2 4 7 3 5 2 Pass 2: 4 7 3 5 2 4 7 3 5 2 3 4 7 5 2
  • 25. Insertion Sort: Pass 3: 3 4 7 5 2 3 4 7 5 2 3 4 5 7 2 Pass 4: 3 4 5 7 2 3 4 5 5 2 2 3 4 5 7 Sorted list is 2 3 4 5 7
  翻译: