Upload
tobias-straub
View
4.223
Download
0
Embed Size (px)
Citation preview
tobiasstraub.com
Let's lookat the first two numbers
seqA = 6 | 8 | 10 | 3
6 8
Okay! Now the question isIs the second number (8) smaller thenthe first (6)? Answer: NO, it is not
tobiasstraub.com
Okay,our seqA has not changed.The next step is to perform the samecheck again, but this time we move ourpointers around a number to the right
tobiasstraub.com
A numberto the right
seqA = 6 | 8 | 10 | 3
8 10
Okay! Now the question is againIs the second number (10) smaller thenthe first (8)? Answer: NO, it is not
tobiasstraub.com
A numberto the right
seqA = 6 | 8 | 10 | 3
10 3
Okay! Now the question is againIs the second number (3) smaller thenthe first (10)? Answer: YES, it is
tobiasstraub.com
What have we achieved?The data sequence is now completelypass through, so that the largest elementis at the end of the data sequence
tobiasstraub.com
Now what?Now we pass through the sequence ofdata once again, so that the secondlargest number (8) is on the second lastposition
At the last position of our data sequencewe have already the largest number (10)
tobiasstraub.com
How do we do that?It's easy! - We take our previously sorteddata sequence and complete all the stepsagain
tobiasstraub.com
Let's lookat the first two numbers
seqA = 6 | 8 | 3 | 10
6 8
Okay! Now the question isIs the second number (8) smaller thenthe first (6)? Answer: NO, it is not
tobiasstraub.com
A numberto the right
seqA = 6 | 8 | 3 | 10
8 3
Okay! Now the question is againIs the second number (3) smaller thenthe first (8)? Answer: YES, it is
tobiasstraub.com
Good,the last number we may exclude from thecomparison.
We remember: In the first pass we havealready promoted the largest number tothe last position
tobiasstraub.com
What have we achieved?The data sequence has now been reruncompletely, so that the second largestnumber is at the second last positionof the data sequence
tobiasstraub.com
Correctly!We take our previously sorteddata sequence and complete all the stepsagain
tobiasstraub.com
Let's lookat the first two numbers
seqA = 6 | 3 | 8 | 10
6 3
Okay! Now the question isIs the second number (3) smaller thenthe first (6)? Answer: YES, it is
tobiasstraub.com
Very well,the second last and the last element wemay exclude from the comparison.
We remember: In the first and the secondpass we have already promoted thelargest number to the last position and thesecond last number to the second lastposition
tobiasstraub.com
In the traditionalversion of the algorithm, all comparisonsare made for each run. It does not matterwhether the elements are already in thecorrect position
tobiasstraub.com
Let's talk about complexityLet us consider in terms of complexityat first the traditional algorithm
tobiasstraub.com
worst case O(n²)If we want to bring a number to thedesired position, we need in the worstcase n-1 comparisons
tobiasstraub.com
worst case O(n²)In our example, we had a data sequencewith four elements
seqA = 6 | 8 | 10 | 3
Do you remember?
tobiasstraub.com
worst case O(n²)Okay, in the worst case, we need toperform three comparisons, becauseseqA has four elements 4 – 1 = 3 (n-1)
seqA = 6 | 8 | 10 | 3
1 : Compare 6 with 82 : Compare 8 with 103 : Compare 10 with 3
tobiasstraub.com
worst case O(n²)So, now we have one number on thedesired position
The question we must ask ourselvesnow is
How many times must we repeatthis procedure in the worst case, so thatall our numbers are in the correctposition?
tobiasstraub.com
worst case O(n²)It's logical! - Until all the numbers havereached the correct position
In the worst case, the n-1 passes
tobiasstraub.com
worst case O(n²)The O-notation for the worst case,given by the number of passesmultiplied by the number of comparisons
(n-1) * (n-1) = O(n²)
Thus we have a quadratic term, whichmakes the bubblesort algorithm withincreasing number of data extremelyinefficient
tobiasstraub.com
For exampleWe have the following data sequence
seqB = 3 | 6 | 8 | 10
We pass through the data sequence.At the end of the pass we would noticethat there was no swapping. Thus, thedata sequence is already stored.
tobiasstraub.com
The big OThe O-notation for the best case,given by the number of passesmultiplied by the number of comparisons
1 * (n-1)
tobiasstraub.com
As we know,we can at eatch iteration of the datasequence save one comparison(namely comparison with the alreadysorted number from the last run)
tobiasstraub.com
The number of comparisons..
seqA = 6 | 8 | 10 | 3Pass 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)
tobiasstraub.com
The O-notationfor the optimized algorithm,thus obtained again by the number ofpasses multiplied by the number ofcomparisons
(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
tobiasstraub.com
Bubblesort....is hardly used in practice...has a bad runtime behavior...is very easy to understand.
tobiasstraub.com
Code? Now!You can download the code discussedhere (Java)
Optimized Versionhttp://tobiasstraub.com/bubblesort-ex1
Traditional Versionhttp://tobiasstraub.com/bubblesort-ex2
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