Bubble Sort PDF
Bubble Sort PDF
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
4
Sort [7 8 3 1 6] into Ascending
Order
pass = 2 listSize = 5
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:
8
Bubble Sort Analysis [7 8 3 1 6]
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
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