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

PPT
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
PPTX
Chapter3.pptx
PPTX
Lect-2.pptx
PPT
21-algorithms (1).ppt
PPT
21-algorithms.ppt
PPT
Algorithm, Pseudocode and Flowcharting in C++
PDF
Searching
CHAP 3 ALGORITHM for infomatique ingenieure .ppt
Chapter3.pptx
Lect-2.pptx
21-algorithms (1).ppt
21-algorithms.ppt
Algorithm, Pseudocode and Flowcharting in C++
Searching

Similar to Lecture of algorithms and problem solving (20)

PPTX
Searching and Sorting Algorithms in Data Structures
PPTX
Searching and Sorting algorithms and working
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PDF
UNIT IV -Data Structures.pdf
PPTX
sorting and searching.pptx
PDF
Data structures arrays
DOCX
UNIT V.docx
DOCX
MODULE 5-Searching and-sorting
PPTX
Algorithms
PDF
advanced searching and sorting.pdf
PPT
Introduction to Algorithm for computer engineering students
PPT
Introduction to Algorithm for computer engineering students
PDF
Searching and sorting by B kirron Reddi
PPT
Chapter 11 - Sorting and Searching
PDF
CP PPT_Unit IV computer programming in c.pdf
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
Data structure and problem solving ch03.ppt
PPTX
Data structure using c module 3
Searching and Sorting Algorithms in Data Structures
Searching and Sorting algorithms and working
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
UNIT IV -Data Structures.pdf
sorting and searching.pptx
Data structures arrays
UNIT V.docx
MODULE 5-Searching and-sorting
Algorithms
advanced searching and sorting.pdf
Introduction to Algorithm for computer engineering students
Introduction to Algorithm for computer engineering students
Searching and sorting by B kirron Reddi
Chapter 11 - Sorting and Searching
CP PPT_Unit IV computer programming in c.pdf
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Data structure and problem solving ch03.ppt
Data structure using c module 3
Ad

Recently uploaded (20)

PDF
Cryptography and Network Security-Module-I.pdf
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
PDF
Applications of Equal_Area_Criterion.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
Java Basics-Introduction and program control
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
wireless networks, mobile computing.pptx
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
PPTX
Principal presentation for NAAC (1).pptx
PPTX
mechattonicsand iotwith sensor and actuator
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Cryptography and Network Security-Module-I.pdf
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
Applications of Equal_Area_Criterion.pdf
Module 8- Technological and Communication Skills.pptx
20250617 - IR - Global Guide for HR - 51 pages.pdf
Unit1 - AIML Chapter 1 concept and ethics
Java Basics-Introduction and program control
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
wireless networks, mobile computing.pptx
August -2025_Top10 Read_Articles_ijait.pdf
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
Principal presentation for NAAC (1).pptx
mechattonicsand iotwith sensor and actuator
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
MLpara ingenieira CIVIL, meca Y AMBIENTAL
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