Académique Documents
Professionnel Documents
Culture Documents
// n is the no of elements
k=0
for i=1 to n-1
if (A[i-1] > A[i])
swap A[i-1],A[i]
k=i
n=k;
WORKING :
The bubble sort working with an example :
A[] = {9,3,6,1,8,4,2}
The length of the array is 7 i.e. let n=7.
9
After 1st iteration we will get a largest element (9) at the last index
of the array . So for the 2nd iteration we need not to include it , because as in
the sorted list , the last element will be the largest one.
For 2nd iteration :
3
Here u can observe the second largest element (8) in the array is in it's
place (at n-2 index).
For 3rd iteration :
3
PERFORMANCE :
Bubble sort has worst-case and average complexity both (n 2),
For references : http://comsciguide.blogspot.com/
where n is the number of items being sorted. There exist many sorting
algorithms with substantially better worst-case or average complexity of
O(nlogn) . Therefore, bubble sort is not a practical sorting algorithm when n is
large.
The positions of the elements in bubble sort will play a large part in
determining its performance . Large elements at the beginning of the list do not
pose a problem , as they are quickly swapped . Small elements towards the end
however, move to the beginning extremely slowly. This has led to these types of
elements being named rabbits and turtles respectively.
It has a special feature , that the largest element( at last index ) gets
sorted first, with smaller elements taking longer to move to their correct
positions.
The only significant advantage that bubble sort has over most other
implementations, even quicksort, but not insertion sort, is that the ability to
detect that the list is sorted in efficient way .When the list is already sorted
(best-case), the complexity of bubble sort is only O(n). By contrast, most other
algorithms, even those with better average-case complexity, perform their
entire sorting process on the set and thus are more complex.
ANALYSIS :
U will be knowing that we do sort for a set of numbers.
Suppose if u have given array of elements which is already sorted and
one element is added to this..U will be having doubt where we have to
For references : http://comsciguide.blogspot.com/
Takealookwithanexample:
Let's take this array which is already sorted.
1
Considerthecaseofaddingthenewnumber(assume5)at
thebeginingofthearray.
The new array becomes
5 1 2 3 4 6 8 9
1 5 2 3 4 6 8 9
1 2 5 3 4 6 8 9
1 2 3 5 4 6 8 9
1 2 3 4 5 6 8 9
// the
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
FOR 2ND ITERATION :
1
1 2 3 4 6 8 9 5
For 1st iteration :
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 9 5
1 2 3 4 6 8 5 9
For 2nd iteration :
1 2 3 4 6 8 5 9
1 2 3 4 6 8 5 9
For references : http://comsciguide.blogspot.com/
1 2 3 4 6 8 5 9
1 2 3 4 6 8 5 9
1 2 3 4 6 8 5 9
1 2 3 4 6 8 5 9
1 2 3 4 6 5 8 9
1 2 3 4 6 5 8 9
For 3rd iteration :
1 2 3 4 6 5 8 9
1 2 3 4 6 5 8 9
1 2 3 4 6 5 8 9
1 2 3 4 6 5 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
// the array is sorted
and k=7 ..so n=7
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9
1 2 3 4 5 6 8 9