written 8.4 years ago by | • modified 8.4 years ago |
- The bubble sort is also known as the ripple sort.
- The bubble sort is probably the first, reasonably complex module that any beginning programmer has to write.
- It is a very simple construct which introduces the student to the fundamentals of how sorting works.
- A bubble sort makes use of an array and some sort of "swapping" mechanism. Most programming languages have a built-in function to swap elements of an array.
- Even if a swapping function does not exist, only a couple of extra lines of code are required to store one array element in a temporary field in order to swap a second element into its place.
- Then the first element is moved out of the temporary field and back into the array at the second element's position.
- Here is a simple example of how a bubble sort works: Suppose you have a row of children's toy blocks with letters on them.
- They are in random order and you wish to arrange them in alphabetical order from left to right.
- Example:
Step 1. Begin with the first block. In this case, the letter G. (Fig. 1.)
Step 2. Look at the block just to the right of it.
Step 3. If the block to the right should come before the block on the left, swap them so that they are in order (Fig. 2.)
If you were doing this by hand, you might just pick the blocks to be moved with one in each hand and cross your arms to swap them. Or you might move the first one out of its position temporarily, move the second one in its place, then move the first one to the now empty position (this is the difference between having a single function to do the swap, or writing some code to do it).
Step 4. Compare the next block in line with the first, and repeat step 3. Do this until you run out of blocks. Then begin step one again with the second block. (Fig. 3,4,5,6)
The bubble sort gets its name because elements tend to move up into the correct order like bubbles rising to the surface.
Deriving the time complexity of the bubble sort algorithm as follows:
- According to above algorithm our time complexity equation formed is:
Worst case:
T(n) = C1 * n + C2 * (n-1)(n-i+1) + C3* (n-1)(n-i) + C4 * (n-1)(n-i)
$\hspace{0.8cm}$ i is 1,2,3,4,5...........,n i.e i iterator is increasing
$\hspace{0.8cm}$ = C1* n + C2 * (n(n-i+1)-(n-i+1)) +C3* (n(n-i)-(n-i)) + C4* (n(n-i)-(n-i))
$\hspace{0.8cm}$ = C1* n + C2* (nn-in+n - n -i+1)+ C3(nn-ni -n-i) +C4 *(nn-n*i-n-i)
$\hspace{0.8cm}$ = C1n + C2(nn- in -i+1)+C3(nn-ni -n-i)+C4 *(nn-n*i-n-i)
$\hspace{0.8cm}$ = C1n + C2(nn)-C2 (in)-C2i+C2+C3(nn)-C3(ni)-C3n-C3i+C4(nn)- C4(ni)-C4n-C4*i
$\hspace{0.8cm}$ = n(C1-C3-C4) + nn(C2+C3+C4)+ ni(-C2-C3-C4)+i*(-C2-C3-C4)+C2
$\hspace{0.8cm}$ =O(n*n)
$\hspace{0.8cm}$ =O(n^2)
Best case:
For sorted array 3rd line in bubble sort algorithm is not satisfied thats why of course 4rth line will also not execute.
T (n) = C1 n + C2 *((n-1) (n-i+1)) +C3 ((n-1) (n-i)) +C4* ((n-1) (n-i))
Now as you know algorithm is working till 3rd line because the elements in array is already sorted.
So that’s why iterating one time that is till n times putting n in place of i in the equation we get:
T(n) = C1n + C2 ((n-1)(n-n+1)) +C3*((n-1)(n-n)) +C4 *0
$\hspace{0.6cm}$ = C1n + C2 (n-1) +C30+C30+C4*0
$\hspace{0.6cm}$ = C1n + C2n - C2
$\hspace{0.6cm}$ = n*(C1+C2) - C2
$\hspace{0.6cm}$ = O(n)