Successfully reported this slideshow.
Upcoming SlideShare
×

# Algorithms Lecture 4: Sorting Algorithms I

2,633 views

Published on

Sorting Algorithms, Bubble Sort, Selection Sort, Insertion Sort

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Benha University http://bu.edu.eg/staff/mloey

Are you sure you want to  Yes  No

### Algorithms Lecture 4: Sorting Algorithms I

1. 1. Analysis and Design of Algorithms Sorting Algorithms I
2. 2. Analysis and Design of Algorithms Sorting Algorithms Bubble Sort Selection Sort Insertion Sort
3. 3. 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
4. 4. Analysis and Design of Algorithms Bubble Sort
5. 5. 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.
6. 6. 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
7. 7. Analysis and Design of Algorithms  Example 1 Assume the following Array: 5 1 4 2
8. 8. Analysis and Design of Algorithms  First Iteration:  Compare 5 1 4 2  j  j+1
9. 9. Analysis and Design of Algorithms  First Iteration:  Swap 1 5 4 2  j  j+1
10. 10. Analysis and Design of Algorithms  First Iteration:  Compare 1 5 4 2  j  j+1
11. 11. Analysis and Design of Algorithms  First Iteration:  Swap 1 4 5 2  j  j+1
12. 12. Analysis and Design of Algorithms  First Iteration:  Compare 1 4 5 2  j  j+1
13. 13. Analysis and Design of Algorithms  First Iteration:  Swap 1 4 2 5  j  j+1
14. 14. Analysis and Design of Algorithms 1 4 2 5
15. 15. Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1
16. 16. Analysis and Design of Algorithms  Second Iteration:  Compare 1 4 2 5  j  j+1
17. 17. Analysis and Design of Algorithms  Second Iteration:  Swap 1 2 4 5  j  j+1
18. 18. Analysis and Design of Algorithms 1 2 4 5
19. 19. Analysis and Design of Algorithms  Third Iteration:  Compare 1 2 4 5  j  j+1
20. 20. Analysis and Design of Algorithms 1 2 4 5
21. 21. Analysis and Design of Algorithms  Array is now sorted 1 2 3 4
22. 22. 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:
23. 23. 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
24. 24. 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
25. 25. Analysis and Design of Algorithms  Python Code
26. 26. Analysis and Design of Algorithms
27. 27. Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 5 4 3 2 1
28. 28. Analysis and Design of Algorithms Selection Sort
29. 29. 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.
30. 30. 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
31. 31. Analysis and Design of Algorithms  Example 1 Assume the following Array: 8 12 5 9 2
32. 32. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min
33. 33. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  j  min
34. 34. Analysis and Design of Algorithms  Move 8 12 5 9 2  j  i  min
35. 35. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j
36. 36. Analysis and Design of Algorithms  Compare 8 12 5 9 2  i  min  j
37. 37. Analysis and Design of Algorithms  Move 8 12 5 9 2  i  j  min
38. 38. Analysis and Design of Algorithms  Smallest 8 12 5 9 2  i  min
39. 39. Analysis and Design of Algorithms  Swap 8 12 5 9 2  min  i
40. 40. Analysis and Design of Algorithms  Sorted  Un Sorted 2 12 5 9 8  Sorted  Un Sorted
41. 41. Analysis and Design of Algorithms  Compare 2 12 5 9 8  i  min  j  Sorted
42. 42. Analysis and Design of Algorithms  Move 2 12 5 9 8  i  min  j  Sorted
43. 43. Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min
44. 44. Analysis and Design of Algorithms  Compare 2 12 5 9 8  Sorted  i  j  min
45. 45. Analysis and Design of Algorithms  Smallest 2 12 5 9 8  Sorted  i  min
46. 46. Analysis and Design of Algorithms  Swap 2 12 5 9 8  Sorted  i  min
47. 47. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 12 9 8  Sorted  Un Sorted
48. 48. Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  j  min
49. 49. Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  j  min
50. 50. Analysis and Design of Algorithms  Compare 2 5 12 9 8  Sorted  i  min  j
51. 51. Analysis and Design of Algorithms  Move 2 5 12 9 8  Sorted  i  min  j
52. 52. Analysis and Design of Algorithms  Smallest 2 5 12 9 8  Sorted  i  min
53. 53. Analysis and Design of Algorithms  Swap 2 5 12 9 8  Sorted  i  min
54. 54. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted
55. 55. Analysis and Design of Algorithms  Compare 2 5 8 9 12  Sorted  i  min  j
56. 56. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  Un Sorted
57. 57. Analysis and Design of Algorithms  Sorted  Un Sorted 2 5 8 9 12  Sorted  i  min
58. 58. Analysis and Design of Algorithms  Array is now sorted 2 5 8 9 12  Sorted
59. 59. 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
60. 60. 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
61. 61. 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
62. 62. Analysis and Design of Algorithms  Python Code
63. 63. Analysis and Design of Algorithms
64. 64. Analysis and Design of Algorithms  Time Complexity: O(n2) as there are two nested loops  Example of worst case 2 3 4 5 1
65. 65. Analysis and Design of Algorithms Insertion Sort
66. 66. Analysis and Design of Algorithms Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
67. 67. 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
68. 68. Analysis and Design of Algorithms  Assume the following Array: 5 1 4 2
69. 69. Analysis and Design of Algorithms  Compare  Store= 5 1 4 2  i  j  j+1 1
70. 70. Analysis and Design of Algorithms  Move  Store= 5 4 2  i  j  j+1 1
71. 71. Analysis and Design of Algorithms  Move  Store= 1 5 4 2  i  j+1
72. 72. Analysis and Design of Algorithms  Compare  Store= 1 5 4 2  i  j 4  j+1
73. 73. Analysis and Design of Algorithms  Move  Store= 1 5 2  i  j 4  j+1
74. 74. Analysis and Design of Algorithms  Compare  Store= 1 5 2  i  j 4  j+1
75. 75. Analysis and Design of Algorithms  Move  Store= 1 4 5 2  i  j  j+1
76. 76. Analysis and Design of Algorithms  Compare  Store= 1 4 5 2  i  j 2  j+1
77. 77. Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1
78. 78. Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1
79. 79. Analysis and Design of Algorithms  Move  Store= 1 4 5  i  j 2  j+1
80. 80. Analysis and Design of Algorithms  Compare  Store= 1 4 5  i  j 2  j+1
81. 81. Analysis and Design of Algorithms  Compare  Store= 1 2 4 5  i  j  j+1
82. 82. Analysis and Design of Algorithms  Array is now sorted 1 2 4 5
83. 83. 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
84. 84. 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
85. 85. 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
86. 86. 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
87. 87. 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
88. 88. Analysis and Design of Algorithms  Python Code
89. 89. Analysis and Design of Algorithms
90. 90. Analysis and Design of Algorithms  Time Complexity: O(n2)  Example of worst case 5 4 3 2 1