Your SlideShare is downloading.
×

×

Saving this for later?
Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.

Text the download link to your phone

Standard text messaging rates apply

Like this presentation? Why not share!

- Problem Solving with Algorithms and... by Yi-Lung Tsai 2760 views
- Sorting by suebloei 609 views
- [Curs Android] C04 - User Interface... by Vlad Petre 715 views
- sort search in C by faizankhan260690 2338 views
- Cenk Ersoy Reducing the risk and co... by SAP Middle East a... 737 views
- Despre automate programabile by Romica Dilimot 1206 views
- Android User Interface by Fernando Ghisi 67 views
- Sorting Algorithms by zhaokatherine 2394 views
- Lect11 Sorting by ryokollll 1412 views
- Marakana Android User Interface by Marko Gargenta 4501 views
- Sorting bubble-sort by Fajar Zain 348 views
- [Curs Android] C01 - Introducere (I... by Vlad Petre 1912 views

Like this? Share it with your network
Share

893

views

views

Published on

MCAST example . Sorting algorithms

MCAST example . Sorting algorithms

No Downloads

Total Views

893

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

103

Comments

0

Likes

2

No embeds

No notes for slide

- 1. 2 Sorting Algorithms
- 2. Instructions • For the embedded lessons look for the Icon Click on it to present embedded lessons
- 3. Lecture Objectives • • • • • • • Become aware of what is sorting. Become aware of sorting algorithms. Explore the types of sorting algorithms. Learn about Bubble Sort. Learn about Insertion Sort. Learn about Selection Sort. Learn about Merge Sort.
- 4. What is sorting? Ascending or descending order Sorting = ordering. Sorted = ordered based on a particular way. An operation that puts (organizes) elements of a list (a collection of data) into certain order (ascending or descending order). 3162134590 0112334569 9654332110
- 5. Sorting – formal definition Permutation Input: A sequence of n numbers: a1, a2, ..., an Output: A permutation (reordering): ak1, ak2, ..., akn of the input sequence such that f(ak1) ≤ f(ak2) ≤... ≤ f(akn) where f is an ordering function
- 6. Examples of Sorting – Words in a dictionary are sorted. – Files in a directory are often listed in sorted order. – In a newspaper, the calendar of events in a schedule is generally sorted by date. – In a record store musical compact disks are generally sorted by recording artist.
- 7. What is a Sorting Algorithm? An algorithm for sorting a set of elements. 3162134590 0112334569 9654332110
- 8. Types of Sorting Algorithms There are many, many different types of sorting algorithms, but the primary ones are: • • • • • • Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Shell Sort
- 9. Selection Sort One of the simplest algorithms works as follows: Name “selection sort” because it works by repeatedly “selecting“ the smallest remaining element and exchanging it with the i-th element in i-th iteration. sorting – Find the smallest element and exchange it with the element in the first position – Find the second smallest element and exchange it with the element in the second position – Continuing in this way until the entire array is sorted. Meaning of words
- 10. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 11. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 12. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 13. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 14. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 15. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 16. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
- 17. Selection Sort 5 1 3 4 6 Largest Comparison Data Movement Sorted 2
- 18. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 19. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 20. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 21. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 22. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 23. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 24. Selection Sort 5 1 Comparison Data Movement Sorted 3 4 2 6
- 25. Selection Sort 5 1 Largest Comparison Data Movement Sorted 3 4 2 6
- 26. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 27. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 28. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 29. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 30. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 31. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 32. Selection Sort 2 1 3 4 Largest Comparison Data Movement Sorted 5 6
- 33. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 34. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 35. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 36. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 37. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 38. Selection Sort 2 1 3 Largest Comparison Data Movement Sorted 4 5 6
- 39. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 40. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 41. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 42. Selection Sort 2 1 Comparison Data Movement Sorted 3 4 5 6
- 43. Selection Sort 2 1 Largest Comparison Data Movement Sorted 3 4 5 6
- 44. Selection Sort 1 2 Comparison Data Movement Sorted 3 4 5 6
- 45. Selection Sort 1 2 3 DONE! Comparison Data Movement Sorted 4 5 6
- 46. Selection sort start i:=1,n-1 min:=a(i) ind:=i for j:=i+1,n a(j) < min false min:=a(j) ind:=j a(ind):=a(i) a(i):=min end 46
- 47. Code of selection sort public static void selectionSort(int[] { int outer, inner, min; for (outer = 0; outer < a.length { min = outer; for (inner = outer + 1; inner < { if (a[inner] < a[min]) min = a) - 1; outer++) a.length; inner++) inner;} // Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i] // a[min] is least among a[outer]..a[a.length - 1] int temp = a[outer]; a[outer] = a[min]; a[min] = temp; // Invariant: for all i <= outer, if i < j then a[i] <= a[j] } }
- 48. Insertion sort 1. We have two group of items: –sorted group, and –unsorted group 2. Initially, all items in the unsorted group and the sorted group is empty. This method is often used for playing cards (used by people to sort bridge hands). –We assume that items in the unsorted group unsorted. –We have to keep items in the sorted group sorted. 3. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property. 4. Repeat the process until the unsorted group becomes empty.
- 49. Insertion sort Like sorting a hand of playing cards: –Start with an empty left hand and the cards facing down on the table. –Remove one card at a time from the table, and insert it into the correct position in the left hand Compare it with each of the cards already in the hand, from right to left. –The cards held in the left hand are sorted These cards were originally the top cards of the pile on the table.
- 50. Insertion sort 1. To insert 12, we need to make room for it. 6 1 0 2 4 36 12 50
- 51. Insertion sort 10 24 6 2. Moving 36. 36 12 51
- 52. Insertion sort 6 10 3. Moving 24. 24 3 6 12 52
- 53. Insertion sort 0 12 24 36 6 1 4. Place 12 into right position. 53
- 54. Code for insertion sort for(i=1; i<n; i++) for(j=i; (j>0) && (a[j]>a[j-1]); j--) { pom=a[j]; a[j]=a[j-1]; a[j-1]=pom; }
- 55. Insertion sort with Romanian folk dance
- 56. Bubble sort • • • Compare each element (execept the last one) with its neighbor to the right. – If they are out of order, swap them. – This puts the largest element at the very end. – The last element is now in the correct and final place. Compare each element (execept the last two) with its neighbor to the right. – If they are out of order, swap them. – This puts the largest element at the very end. – The last two elements is now in the correct and final place. Compare each element (execept the last three) with its neighbor to the right. – Continue as above until you have no unsorted elements on the left.
- 57. Bubble sort 9, 6, 2, 12, 11, 9, 3, 7 Compares the numbers in pairs from left to right exchanging when Compares the numbers in pairs from left to right exchanging when necessary. The first number is compared to the second and as it is necessary. The first number is compared to the second and as it is larger they are exchanged. larger they are exchanged. 6, 9, 2, 12, 11, 9, 3, 7 The next pair of numbers are compared. 9 is the larger and this pair is The next pair of numbers are compared. 9 is the larger and this pair is also exchanged. also exchanged. 6, 2, 9, 12, 11, 9, 3, 7 In the third comparison, the 9 is not larger than the 12 so no exchange In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to is made. We move on to compare the next pair without any change to the list. the list.
- 58. Bubble sort 6, 2, 9, 12, 11, 9, 3, 7 The 12 is larger than the 11 so they are exchanged. The 12 is larger than the 11 so they are exchanged. 6, 2, 9, 11, 12, 9, 3, 7 The 12 is greater than the 9 so they are exchanged The 12 is greater than the 9 so they are exchanged 6, 2, 9, 11, 9, 12, 3, 7 The 12 is greater than the 3 so they are exchanged. The 12 is greater than the 3 so they are exchanged.
- 59. Bubble sort 6, 2, 9, 11, 9, 3, 12, 7 The 12 is greater than the 7 so they are exchanged. The 12 is greater than the 7 so they are exchanged. The end of the list has been reached so this is the end of the first pass. The twelve The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct at the end of the list must be largest number in the list and so is now in the correct position. We now start aanew pass from left to right. position. We now start new pass from left to right. 6, 2, 9, 11, 9, 3, 7, 12
- 60. Bubble sort First Pass 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 6, 2, 9, 11,11,11,11, 12 9, 3, 7, 7, 9, 3, This time we do not have to compare the last two numbers as we know This time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. the 12 is in position. This pass therefore only requires 6 comparisons.
- 61. Bubble sort 6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 9, 3, 7, 11, 12 3, 9, 9, 7, First Pass This time the 11 and 12 are in position. This pass therefore only This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons. requires 5 comparisons.
- 62. Bubble sort 6, Second Pass 2, Third Pass2, Fourth Pass 2, First Pass 2, 6, 6, 6, 9, 9, 9, 3, 9, 11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 7, 9, 9, 3, 7, 9, 11, 12 12 12 12 Each pass requires fewer comparisons. This time only 4 are needed. Each pass requires fewer comparisons. This time only 4 are needed.
- 63. Bubble sort 6, Second Pass 2, Third Pass2, Fourth Pass 2, Fifth Pass 2, First Pass 2, 6, 6, 6, 3, 6, 9, 9, 9, 3, 6, 3, 11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11, 12 12 12 12 12 The list is now sorted but the algorithm does not know this until it The list is now sorted but the algorithm does not know this until it completes aapass with no exchanges. completes pass with no exchanges.
- 64. Bubble sort 6, Second Pass 2, Third Pass2, Fourth Pass 2, Fifth Pass 2, Sixth Pass 2, First Pass 2, 6, 6, 6, 3, 3, 9, 9, 9, 3, 6, 6, 11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11, 12 12 12 12 12 12 This pass no exchanges are made so the algorithm knows the list is This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work. lists this check could save much more work.
- 65. Bubble sort start i=2,n j=n,i,-1 false a(j-1) > a(j) temp=a(j-1) a(j-1)=a(j) a(j)=temp end 65
- 66. Code for bubble sort public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { // counting down for (inner = 0; inner < outer; inner++) { // bubbling up if (a[inner] > a[inner + 1]) { // if out of order... int temp = a[inner]; // ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }
- 67. Bubble sort with Hungarian ("Csángó") folk dance 67
- 68. Merge sort • Divide array into two halves. • Recursively sort each half. • Merge two halves to make sorted whole. A L G O R I T H M S A L G O R I T H M S divide A G L O R H I M S T sort A G H I L M O R S T merge
- 69. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A smallest L O R H I M S T auxiliary array
- 70. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A smallest L G O R H I M S T auxiliary array
- 71. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G smallest O H R H I M S T auxiliary array
- 72. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G smallest O H R I H I M S T auxiliary array
- 73. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G smallest O H R I H L I M S T auxiliary array
- 74. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G O H smallest R I H L M I M S T auxiliary array
- 75. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G O H smallest R I H L M I O M S T auxiliary array
- 76. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. smallest A G A L G O H smallest R I H L M I O M R S T auxiliary array
- 77. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. first half exhausted A G A L G O H R I smallest H L M I O M R S S T auxiliary array
- 78. Merge sort • Keep track of smallest element in each sorted half. • Insert smallest of two elements into auxiliary array. • Repeat until done. first half exhausted A G A L G O H R I smallest H L M I O M R S S T T auxiliary array
- 79. Merge sort
- 80. We learnt! • • • • • • • What is sorting. Sorting algorithms. Types of sorting algorithms. Bubble Sort. Insertion Sort. Selection Sort. Merge Sort.
- 81. Quiz 1. One of the simplest sorting algorithms is called bubble sort. Do you know why? A It encases each element in a 'bubble' before sorting them. B It's a mystery. Why is anything called what it is, really? The designer hoped more people would use his sort if it had a cute name C D Teacher Guide Quiz Question Smaller elements 'bubble' to the top Teacher Guide Quiz Solution
- 82. Quiz 2. In which case would an insertion sort perform best, assuming it was reading the array to be sorted from beginning to end (as opposed to randomly)? A If the array was sorted in reverse order. B If the array was already sorted. C If the array was in a random order. D These will all perform equally well.
- 83. Homework Sort the following array using each of the four sorting algorithms. 26 a) b) c) d) 48 12 Bubble Sort Selection Sort Insertion Sort Merge Sort 92 28 6 33

Be the first to comment