Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

**Scribd will begin operating the SlideShare business on December 1, 2020**
As of this date, Scribd will manage your SlideShare account and any content you may have on SlideShare, and Scribd's General Terms of Use and Privacy Policy will apply. If you wish to opt out, please close your SlideShare account. Learn more.

Successfully reported this slideshow.

Like this presentation? Why not share!

7,350 views

Published on

No Downloads

Total views

7,350

On SlideShare

0

From Embeds

0

Number of Embeds

422

Shares

0

Downloads

397

Comments

5

Likes

7

No embeds

No notes for slide

- 1. BUBBLEBUBBLE SORTSORT
- 2. tobiasstraub.com Bubble Sort is a very simple sort algorithm
- 3. tobiasstraub.com Ah, okay and how does the process work?
- 4. tobiasstraub.com Let us see we want to sort the following data sequence seqA = 6 | 8 | 10 | 3
- 5. tobiasstraub.com Let's look at the first two numbers seqA = 6 | 8 | 10 | 3 6 8 Okay! Now the question is Is the second number (8) smaller then the first (6)? Answer: NO, it is not
- 6. tobiasstraub.com Perfect, then we need to do nothing in this step
- 7. tobiasstraub.com Okay, our seqA has not changed. The next step is to perform the same check again, but this time we move our pointers around a number to the right
- 8. tobiasstraub.com A number to the right seqA = 6 | 8 | 10 | 3 8 10 Okay! Now the question is again Is the second number (10) smaller then the first (8)? Answer: NO, it is not
- 9. tobiasstraub.com Perfect, then we need to do nothing in this step again
- 10. tobiasstraub.com Good, then let us again move a number to the right
- 11. tobiasstraub.com A number to the right seqA = 6 | 8 | 10 | 3 10 3 Okay! Now the question is again Is the second number (3) smaller then the first (10)? Answer: YES, it is
- 12. tobiasstraub.com Now, we have to swap the numbers seqA = 6 | 8 | 10 | 3 10 3 seqA = 6 | 8 | 3 | 10
- 13. tobiasstraub.com Awesome! So this step is completed
- 14. tobiasstraub.com What have we achieved? The data sequence is now completely pass through, so that the largest element is at the end of the data sequence
- 15. tobiasstraub.com Now what? Now we pass through the sequence of data once again, so that the second largest number (8) is on the second last position At the last position of our data sequence we have already the largest number (10)
- 16. tobiasstraub.com How do we do that? It's easy! - We take our previously sorted data sequence and complete all the steps again
- 17. tobiasstraub.com Let's look at the first two numbers seqA = 6 | 8 | 3 | 10 6 8 Okay! Now the question is Is the second number (8) smaller then the first (6)? Answer: NO, it is not
- 18. tobiasstraub.com Perfect, then we need to do nothing in this step
- 19. tobiasstraub.com Good, then let us move a number to the right
- 20. tobiasstraub.com A number to the right seqA = 6 | 8 | 3 | 10 8 3 Okay! Now the question is again Is the second number (3) smaller then the first (8)? Answer: YES, it is
- 21. tobiasstraub.com Now, we have to swap the numbers seqA = 6 | 8 | 3 | 10 8 3 seqA = 6 | 3 | 8 | 10
- 22. tobiasstraub.com Good, the last number we may exclude from the comparison. We remember: In the first pass we have already promoted the largest number to the last position
- 23. tobiasstraub.com What have we achieved? The data sequence has now been rerun completely, so that the second largest number is at the second last position of the data sequence
- 24. tobiasstraub.com Okay, but what's next? Guess what!
- 25. tobiasstraub.com Correctly! We take our previously sorted data sequence and complete all the steps again
- 26. tobiasstraub.com Let's look at the first two numbers seqA = 6 | 3 | 8 | 10 6 3 Okay! Now the question is Is the second number (3) smaller then the first (6)? Answer: YES, it is
- 27. tobiasstraub.com Now, we have to swap the numbers seqA = 6 | 3 | 8 | 10 6 3 seqA = 3 | 6 | 8 | 10
- 28. tobiasstraub.com Very well, the second last and the last element we may exclude from the comparison. We remember: In the first and the second pass we have already promoted the largest number to the last position and the second last number to the second last position
- 29. tobiasstraub.com Yay! That was all. We have reached the end of the sequence
- 30. tobiasstraub.com You could see, that there is a very simple algorithm for for sorting elements
- 31. tobiasstraub.com And you know what? This is even the optimized version of bubblesort
- 32. tobiasstraub.com In the traditional version of the algorithm, all comparisons are made for each run. It does not matter whether the elements are already in the correct position
- 33. tobiasstraub.com Let's talk about complexity Let us consider in terms of complexity at first the traditional algorithm
- 34. tobiasstraub.com worst case O(n²) If we want to bring a number to the desired position, we need in the worst case n-1 comparisons
- 35. tobiasstraub.com worst case O(n²) In our example, we had a data sequence with four elements seqA = 6 | 8 | 10 | 3 Do you remember?
- 36. tobiasstraub.com worst case O(n²) Okay, in the worst case, we need to perform three comparisons, because seqA has four elements 4 – 1 = 3 (n-1) seqA = 6 | 8 | 10 | 3 1 : Compare 6 with 8 2 : Compare 8 with 10 3 : Compare 10 with 3
- 37. tobiasstraub.com worst case O(n²) So, now we have one number on the desired position The question we must ask ourselves now is How many times must we repeat this procedure in the worst case, so that all our numbers are in the correct position?
- 38. tobiasstraub.com worst case O(n²) It's logical! - Until all the numbers have reached the correct position In the worst case, the n-1 passes
- 39. tobiasstraub.com worst case O(n²) The O-notation for the worst case, given by the number of passes multiplied by the number of comparisons (n-1) * (n-1) = O(n²) Thus we have a quadratic term, which makes the bubblesort algorithm with increasing number of data extremely inefficient
- 40. tobiasstraub.com best case O(n) The best case would be if the data is already completely stored
- 41. tobiasstraub.com For example We have the following data sequence seqB = 3 | 6 | 8 | 10 We pass through the data sequence. At the end of the pass we would notice that there was no swapping. Thus, the data sequence is already stored.
- 42. tobiasstraub.com The big O The O-notation for the best case, given by the number of passes multiplied by the number of comparisons 1 * (n-1)
- 43. tobiasstraub.com Okay, let's see now how the complexity behaves in Bubblesort optimized version
- 44. tobiasstraub.com As we know, we can at eatch iteration of the data sequence save one comparison (namely comparison with the already sorted number from the last run)
- 45. tobiasstraub.com The number of comparisons.. seqA = 6 | 8 | 10 | 3 Pass 1: 6 | 8 | 3 | 10 (3 comparisons: n-1) Pass 2: 6 | 3 | 8 | 10 (2 comparisons: n-2) Pass 3: 3 | 6 | 8 | 10 (1 comparisons: n-3) Pass 4: 3 | 6 | 8 | 10 (0 comparisons: 1) ..can thus be described as follows: (n-1) + (n-2) + … + 1 also n/2 (linear O-notation)
- 46. tobiasstraub.com The number of passes will not change
- 47. tobiasstraub.com The O-notation for the optimized algorithm, thus obtained again by the number of passes multiplied by the number of comparisons (n-1) * (n/2) = O(n²) Thus we have again a quadratic term, but in terms of the linear portion (comparisons) much faster on the run time
- 48. tobiasstraub.com Okay, now a few facts about Bubblesort
- 49. tobiasstraub.com Bubblesort.. ..is hardly used in practice. ..has a bad runtime behavior. ..is very easy to understand.
- 50. tobiasstraub.com Code? Now! You can download the code discussed here (Java) Optimized Version http://tobiasstraub.com/bubblesort-ex1 Traditional Version http://tobiasstraub.com/bubblesort-ex2
- 51. tobiasstraub.com About the Author Tobias Straub is a Web and Mobile Developer from Germany. Write an email to talk with him or follow him on LinkedIn. tobiasstraub.com/linkedin hello@tobiasstraub.com
- 52. This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/. Image sources Page 1: Keith, http://www.flickr.com/photos/outofideas/ License

No public clipboards found for this slide

Login to see the comments