0% found this document useful (0 votes)
227 views

Bubble Sort PDF

Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the array is fully sorted. It has a time complexity of O(n^2) in all cases due to the double loop required. The number of comparisons is always O(n^2) but the number of swaps can vary from O(1) in the best case to O(n^2) in the worst case. An improvement is to add a check after each pass to see if any swaps were made, and terminate early if not, improving the best case complexity to O(n).

Uploaded by

trevor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
227 views

Bubble Sort PDF

Bubble sort works by repeatedly swapping adjacent elements that are in the wrong order until the array is fully sorted. It has a time complexity of O(n^2) in all cases due to the double loop required. The number of comparisons is always O(n^2) but the number of swaps can vary from O(1) in the best case to O(n^2) in the worst case. An improvement is to add a check after each pass to see if any swaps were made, and terminate early if not, improving the best case complexity to O(n).

Uploaded by

trevor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SCJ2013 Data Structure & Algorithms

Bubble Sort

Nor Bahiah Hj Ahmad & Dayang 
Norhayati A. Jawawi

1
Bubble Sort
Sorting activities for Bubble:
• Go through multiple passes over the array.
• In every pass:
– Compare adjacent elements in the list.
– Exchange the elements if they are out of order.
– Each pass moves the largest (or smallest) elements to the 
end of the array
• Repeating this process in several passes eventually sorts the 
array into ascending (or descending) order.
• Bubble sort  complexity is is O(n2) and only suitable to sort 
array with small size of data.  
2
Bubble Sort Implementation
// Sorts items in an array into ascending order.
void BubbleSort(dataType data[], int listSize)
{   int pass, tempValue;
for ( pass =1;pass < listSize; pass++ ) External for loop is used to control 
{ the number of passes needed. 
// moves the largest element to the 
// end of the array
for (int x = 0; x < listSize ‐ pass; x++)
//compare adjacent elements
Internal for loop is used to compare 
if ( data[x]>data[x+1] )
adjacent elements and swap 
{   // swap elements   
elements if they are not in order. 
tempValue = data[x];
After the internal loop has finished 
execution, the largest element in the 
data[x] = data[x+1];
array will be moved at the top. 
data[x+1] = tempValue;
}
if statement is used to compare the 
}
adjacent elements.
} // end Bubble Sort

3
Sort  [7 8 3 1 6] into Ascending Order
pass = 1 List Size = 5

no swap swap(8,3) swap(8,1) swap (8,6)

4
Sort  [7 8 3 1 6] into Ascending 
Order
pass = 2 listSize = 5

swap(7,3) swap(7,1) swap (7,6)

5
Sort  [7 8 3 1 6] into Ascending Order
pass= 3

swap(3,1) no swap

6
Sort  [7 8 3 1 6] into Ascending 
Order
pass = 4

no swap

7
Bubble Sort Analysis
• The number of comparison between elements and the 
number of exchange between elements determine the 
efficiency of Bubble Sort algorithm. 

• Generally, the number of comparisons between elements 
in Bubble Sort can be stated as follows:

(n-1)+(n-2)+…….+2+1= n(n-1)/2 = O(n2)

8
Bubble Sort Analysis [7 8 3 1 6]

Pass 1 : Comparison (listSize-pass=4) Pass 2 : Comparisons (listSize-pass:(5-2=3))

Pass 4 : Comparisons (5-4=1)


Pass 3 : Comparison (5-3= 2)

(n-1)+(n-2)+……….….+2+1= n(n-1)/2 = O(n2)


The number of comparisons:
(5-1) + (5-2) + (5-3) + (5-4) = 4 + 3 + 2 + 1 = 10.
9
Worse Case Analysis [8 7 6 3 1] 

Pass 1 : Comparison (5-1=4) Pass 2 : Comparisons (5-2=3)

Pass 3 : Comparison (5-3= 2) Pass 4 : Comparisons (5-4=1)

The number of comparisons to sort data in this list:


(5-1) + (5-2) + (5-3) + (5-4) = 4 + 3 + 2 + 1 = 10.
10
Best Case Analysis [1 3 6 7 8]

Pass 1 : Comparison (5-1=4) Pass 2 : Comparisons (5-2=3)

Pass 3 : Comparison (5-3= 2) Pass 4 : Comparisons (5-4=1)

The number of comparisons to sort data in this list:


(5-1) + (5-2) + (5-3) + (5-4) = 4 + 3 + 2 + 1 = 10.
11
Bubble Sort Analysis
In any cases, (worse case, best case or average case) 
to sort the list in ascending order the number of 
comparisons between elements is the same. 
– Worse Case  [8 7 6 3 1] 
– Average Case [7 8 3 1 6] 
– Best Case [1 3 6 7 8]
All lists with 5 elements need 10 comparisons to sort 
all the data. 

12
Bubble Sort Analysis
• In the example given, it can be seen that the number of 
comparison for worse case and best case is the same ‐ with 10 
comparisons. 
• The difference can be seen in the number of swapping 
elements.  Worse case has maximum number of swapping: 
10, while best case has no swapping since all data is already in 
the right position. 
• For best case, starting with pass one, there is no exchange of 
data occur.  
• From the example, it can be concluded that in any pass, if 
there is no exchange of data occur, the list is already sorted.  
The next pass shouldn't be continued and the  sorting process 
should stop. 
13
Improved Bubble Sort
// Improved Bubble Sort To improve the efficiency of Bubble
// Sorts items in an array into ascending order. Sort, a condition that check whether
the list is sorted should be add at the
void bubbleSort(DataType data[], int n)
external loop
{ int temp;
bool sorted = false; // false when swaps occur A Boolean variable, sorted is
for (int pass = 1; (pass < n) && !sorted; ++pass) added in the algorithm to signal
{ sorted = true; // assume sorted
whether there is any exchange
for (int x = 0; x < n-pass; ++x)
{ if (data[x] > data[x+1]) of elements occur in certain
{ // exchange items pass.
temp = data[x];
data[x] = data[x+1]; In external loop, sorted is set
data[x+1] = temp; to true. If there is exchange
sorted = false; // signal exchange of data inside the inner loop,
} // end if sorted is set to false.
} // end for
} // end for Another pass will continue, if
} // end bubbleSort sorted is false and will stop if
sorted is true.

14
Improved Bubble Sort : Best Case[1 3 6 7 8]

sorted = T T T T
pass = 1

In pass 1, there is no exchange of data occur and variable sorted is


always True.
Therefore, condition statement in external loop will become false and the
loop will stop execution. In this example, pass 2 will not be continued.

Analysis - For best case, the number of comparison between


elements is 4, (n-1) which is O(n).
15
Improved Bubble Sort : 
Average Case [1 3 7 6 8]
For Average Case:           
• [1 3 7 6 8] we have to go 
through 2 passes only.  The 
subsequent passes is not 
continued since the array is 
sorted = T T F F
already sorted.  
pass = 1
• Conclusion – For improved 
Bubble Sort, the sorting time 
and the number of 
comparisons between data in 
average case and best case 
can be minimized. 

sorted = T T T
pass = 2
16
Bubble Sort – Algorithm Complexity
• Complexity is measured based on time consuming 
operations to compare and swap elements.
• Number of comparisons
– a for loop embedded inside a while loop
– Worst Case (n‐1)+(n‐2)+(n‐3) …+1 , or O(n2)
– Best Case – (n‐1) or O(n)
• Number of Swaps
– inside a conditional ‐> #swaps data dependent !!
– Best Case 0, or O(1)
– Worst Case (n‐1)+(n‐2)+(n‐3) …+1 , or O(n2)

17
Summary and Conclusion
Bubble Sort takes several passes to sort elements in
an array. Every pass need to do comparisons between
elements and exchange the data if the elements are
not in the right order. However the complexity of
Bubble sort is the same for best case and worse case.
Bubble Comparisons Swaps
Sort
Best Case O(n2) 0
Average O(n2) O(n2)
Case
Worst Case O(n2) O(n2)
18

You might also like