Slide 1

Slide 1 text

Analysis and Design of Algorithms Sorting Algorithms I

Slide 2

Slide 2 text

Analysis and Design of Algorithms Sorting Algorithms Bubble Sort Selection Sort Insertion Sort

Slide 3

Slide 3 text

Analysis and Design of Algorithms  Sorting Algorithm is an algorithm made up of a series of instructions that takes an array as input, and outputs a sorted array.  There are many sorting algorithms, such as:  Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Heap Sort, QuickSort, Radix Sort, Counting Sort, Bucket Sort, ShellSort, Comb Sort, Pigeonhole Sort, Cycle Sort

Slide 4

Slide 4 text

Analysis and Design of Algorithms Bubble Sort

Slide 5

Slide 5 text

Analysis and Design of Algorithms Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Slide 6

Slide 6 text

Analysis and Design of Algorithms Algorithm:  Step1: Compare each pair of adjacent elements in the list  Step2: Swap two element if necessary  Step3: Repeat this process for all the elements until the entire array is sorted

Slide 7

Slide 7 text

Analysis and Design of Algorithms  Example 1 Assume the following Array: 5 1 4 2

Slide 8

Slide 8 text

Analysis and Design of Algorithms  First Iteration:  Compare 5 1 4 2  j  j+1

Slide 9

Slide 9 text

Analysis and Design of Algorithms  First Iteration:  Swap 1 5 4 2  j  j+1

Slide 10

Slide 10 text

Analysis and Design of Algorithms  First Iteration:  Compare 1 5 4 2  j  j+1

Slide 11

Slide 11 text

Analysis and Design of Algorithms  First Iteration:  Swap 1 4 5 2  j  j+1

Slide 12

Slide 12 text

Analysis and Design of Algorithms  First Iteration:  Compare 1 4 5 2  j  j+1

Slide 13

Slide 13 text

Analysis and Design of Algorithms  First Iteration:  Swap 1 4 2 5  j  j+1

Slide 14

Slide 14 text

Analysis and Design of Algorithms 1 4 2 5

Slide 15

Slide 15 text

Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1

Slide 16

Slide 16 text

Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1

Slide 17

Slide 17 text

Analysis and Design of Algorithms  Second Iteration:  Swap 1 2 4 5  j  j+1

Slide 18

Slide 18 text

Analysis and Design of Algorithms 1 2 4 5

Slide 19

Slide 19 text

Analysis and Design of Algorithms  Third Iteration:  Compare 1 2 4 5  j  j+1

Slide 20

Slide 20 text

Analysis and Design of Algorithms 1 2 4 5

Slide 21

Slide 21 text

Analysis and Design of Algorithms  Array is now sorted 1 2 3 4

Slide 22

Slide 22 text

Analysis and Design of Algorithms 5 4 2 1 3 4 5 2 1 3 4 2 5 1 3 4 2 1 5 3 4 2 1 3 5 4 2 1 3 5 2 4 1 3 5 2 1 4 3 5 2 1 3 4 5 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5  Example 2:

Slide 23

Slide 23 text

Analysis and Design of Algorithms  What is the output of bubble sort after the 1st iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63

Slide 24

Slide 24 text

Analysis and Design of Algorithms  What is the output of bubble sort after the 1st iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63

Slide 25

Slide 25 text

Analysis and Design of Algorithms  Python Code

Slide 26

Slide 26 text

Analysis and Design of Algorithms

Slide 27

Slide 27 text

Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 5 4 3 2 1

Slide 28

Slide 28 text

Analysis and Design of Algorithms Selection Sort

Slide 29

Slide 29 text

Analysis and Design of Algorithms The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.

Slide 30

Slide 30 text

Analysis and Design of Algorithms Algorithm:  Step1: Find the minimum value in the list  Step2: Swap it with the value in the current position  Step3: Repeat this process for all the elements until the entire array is sorted

Slide 31

Slide 31 text

Analysis and Design of Algorithms  Example 1 Assume the following Array: 8 12 5 9 2

Slide 32

Slide 32 text

Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min

Slide 33

Slide 33 text

Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min

Slide 34

Slide 34 text

Analysis and Design of Algorithms  Move 8 12 5 9 2  j  i  min

Slide 35

Slide 35 text

Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j

Slide 36

Slide 36 text

Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j

Slide 37

Slide 37 text

Analysis and Design of Algorithms  Move 8 12 5 9 2  i  j  min

Slide 38

Slide 38 text

Analysis and Design of Algorithms  Smallest 8 12 5 9 2  i  min

Slide 39

Slide 39 text

Analysis and Design of Algorithms  Swap 8 12 5 9 2  min  i

Slide 40

Slide 40 text

Analysis and Design of Algorithms  Sorted  Un Sorted 2 12 5 9 8  Sorted  Un Sorted

Slide 41

Slide 41 text

Analysis and Design of Algorithms  Compare 2 12 5 9 8  i  min  j  Sorted

Slide 42

Slide 42 text

Analysis and Design of Algorithms  Move 2 12 5 9 8  i  min  j  Sorted

Slide 43

Slide 43 text

Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min

Slide 44

Slide 44 text

Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min

Slide 45

Slide 45 text

Analysis and Design of Algorithms  Smallest 2 12 5 9 8  Sorted  i  min

Slide 46

Slide 46 text

Analysis and Design of Algorithms  Swap 2 12 5 9 8  Sorted  i  min

Slide 47

Slide 47 text

Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 12 9 8  Sorted  Un Sorted

Slide 48

Slide 48 text

Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  j  min

Slide 49

Slide 49 text

Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  j  min

Slide 50

Slide 50 text

Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  min  j

Slide 51

Slide 51 text

Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  min  j

Slide 52

Slide 52 text

Analysis and Design of Algorithms  Smallest 2 5 12 9 8  Sorted  i  min

Slide 53

Slide 53 text

Analysis and Design of Algorithms  Swap 2 5 12 9 8  Sorted  i  min

Slide 54

Slide 54 text

Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted

Slide 55

Slide 55 text

Analysis and Design of Algorithms  Compare 2 5 8 9 12  Sorted  i  min  j

Slide 56

Slide 56 text

Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted

Slide 57

Slide 57 text

Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  i  min

Slide 58

Slide 58 text

Analysis and Design of Algorithms  Array is now sorted 2 5 8 9 12  Sorted

Slide 59

Slide 59 text

Analysis and Design of Algorithms 12 10 16 11 9 7  Example 2: 7 10 16 11 9 12 7 9 16 11 10 12 7 9 10 11 16 12 7 9 10 11 16 12 7 9 10 11 12 16 12 10 16 11 9 7

Slide 60

Slide 60 text

Analysis and Design of Algorithms  What is the output of selection sort after the 2nd iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63

Slide 61

Slide 61 text

Analysis and Design of Algorithms  What is the output of selection sort after the 2nd iteration given the following sequence of numbers: 13 2 9 4 18 45 37 63 a) 2 4 9 13 18 37 45 63 b) 2 9 4 13 18 37 45 63 c) 13 2 4 9 18 45 37 63 d) 2 4 9 13 18 45 37 63

Slide 62

Slide 62 text

Analysis and Design of Algorithms  Python Code

Slide 63

Slide 63 text

Analysis and Design of Algorithms

Slide 64

Slide 64 text

Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 2 3 4 5 1

Slide 65

Slide 65 text

Analysis and Design of Algorithms Insertion Sort

Slide 66

Slide 66 text

Analysis and Design of Algorithms Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.

Slide 67

Slide 67 text

Analysis and Design of Algorithms  Algorithm:  Step1: Compare each pair of adjacent elements in the list  Step2: Insert element into the sorted list, until it occupies correct position.  Step3: Swap two element if necessary  Step4: Repeat this process for all the elements until the entire array is sorted

Slide 68

Slide 68 text

Analysis and Design of Algorithms  Assume the following Array: 5 1 4 2

Slide 69

Slide 69 text

Analysis and Design of Algorithms  Compare  Store= 5 1 4 2  i  j  j+1 1

Slide 70

Slide 70 text

Analysis and Design of Algorithms  Move  Store= 5 4 2  i  j  j+1 1

Slide 71

Slide 71 text

Analysis and Design of Algorithms  Move  Store= 1 5 4 2  i  j+1

Slide 72

Slide 72 text

Analysis and Design of Algorithms  Compare  Store= 1 5 4 2  i  j 4  j+1

Slide 73

Slide 73 text

Analysis and Design of Algorithms  Move  Store= 1 5 2  i  j 4  j+1

Slide 74

Slide 74 text

Analysis and Design of Algorithms  Compare  Store= 1 5 2  i  j 4  j+1

Slide 75

Slide 75 text

Analysis and Design of Algorithms  Move  Store= 1 4 5 2  i  j  j+1

Slide 76

Slide 76 text

Analysis and Design of Algorithms  Compare  Store= 1 4 5 2  i  j 2  j+1

Slide 77

Slide 77 text

Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1

Slide 78

Slide 78 text

Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1

Slide 79

Slide 79 text

Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1

Slide 80

Slide 80 text

Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1

Slide 81

Slide 81 text

Analysis and Design of Algorithms  Compare  Store= 1 2 4 5  i  j  j+1

Slide 82

Slide 82 text

Analysis and Design of Algorithms  Array is now sorted 1 2 4 5

Slide 83

Slide 83 text

Analysis and Design of Algorithms 5 1 8 3 9 2  Example 2: 1 5 8 3 9 2 1 5 8 3 9 2 1 3 5 8 9 2 1 3 5 8 9 2 1 2 3 5 8 9 5 1 8 3 9 2

Slide 84

Slide 84 text

Analysis and Design of Algorithms  What is the output of insertion sort after the 1st iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 7 5 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9

Slide 85

Slide 85 text

Analysis and Design of Algorithms  What is the output of insertion sort after the 1st iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 7 5 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9

Slide 86

Slide 86 text

Analysis and Design of Algorithms  What is the output of insertion sort after the 2nd iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 5 7 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9

Slide 87

Slide 87 text

Analysis and Design of Algorithms  What is the output of insertion sort after the 2nd iteration given the following sequence of numbers: 7 3 5 1 9 8 4 6 a) 3 5 7 1 9 8 4 6 b) 1 3 7 5 9 8 4 6 c) 3 4 1 5 6 8 7 9 d) 1 3 4 5 6 7 8 9

Slide 88

Slide 88 text

Analysis and Design of Algorithms  Python Code

Slide 89

Slide 89 text

Analysis and Design of Algorithms

Slide 90

Slide 90 text

Analysis and Design of Algorithms  Time Complexity: O(n2)  Example of worst case 5 4 3 2 1

Slide 91

Slide 91 text

Analysis and Design of Algorithms facebook.com/mloey [email protected] twitter.com/mloey linkedin.com/in/mloey [email protected] mloey.github.io

Slide 92

Slide 92 text

Analysis and Design of Algorithms www.YourCompany.com © 2020 Companyname PowerPoint Business Theme. All Rights Reserved. THANKS FOR YOUR TIME