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

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 Structures
poongothai11
 
Searching and Sorting algorithms and working
Searching and Sorting algorithms and working
RitikaLohiya2
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
sorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
Data structures arrays
Data structures arrays
maamir farooq
 
UNIT V.docx
UNIT V.docx
Revathiparamanathan
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Algorithms
Algorithms
WaqarzadAa
 
advanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Introduction 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 students
geraldjamesignacio2
 
Searching 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 Searching
Eduardo Bergavera
 
CP 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].pptx
kncetaruna
 
UNIT 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 3
smruti sarangi
 
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...
Sitamarhi Institute of Technology
 
Searching 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 working
RitikaLohiya2
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
sorting and searching.pptx
sorting and searching.pptx
ParagAhir1
 
Data structures arrays
Data structures arrays
maamir farooq
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
advanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Introduction 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 students
geraldjamesignacio2
 
Searching 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 Searching
Eduardo Bergavera
 
CP 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].pptx
kncetaruna
 
UNIT 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 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...
Sitamarhi Institute of Technology
 

Recently uploaded (20)

Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
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