Topic 10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

CSC211 Data Structure & Algorithms

Bubble Sort/Sinking sort

1
Bubble Sort/Sinking sort
• The Bubble Sort, also called Sinking Sort, is a
sorting algorithm that compares each pair of
adjacent elements.
• Bubble sort is not an efficient sorting algorithm
compared to others but it provides useful
insights to beginners on what sorting algorithm
is and how it works behind the scene.
• The basic technique of bubble sort is that the
first element of the list is compared to the
second, the second to the third element, and
so on.
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++)
Internal for loop is used to compare
//compare adjacent elements
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
data[x] = data[x+1]; execution, the largest element in the
data[x+1] = tempValue;
}
array will be moved at the top.
} if statement is used to compare the
} // end Bubble Sort adjacent elements.

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

(4) 8

[:?
)
x
~
4
Sort [7 8 3 1 6] into Ascending
Order
pass = 2 listSize = 5

x= . ...,
-
[4]
c-1
[~]
[ ]
[OJ

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

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

x=

swap(3,1) no swap

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

x= 0

[-'1 8
[3] - i

[1] 6
[l]
[O] I

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]

x= 0 1 2 3 x= 0 1 :?

(4 J 6 6 8 (4 J 8 8
[3J 1 l 6 [2J 3 [3J 6 6
8 1 (:? 1 ~ 6
J
[lJ 3 1 1
[lJ 8 3 3 3
[OJ 3
[OJ .
Pass 1 : Comparison (listSize- Pass 2 : Comparisons (listSize-pass:(5-
pass=4) 2=3)) x=
I
0
x= 0 l.
[-' J 8
[-'] 8 8
(3) (3J '"'!'

[~] 6 6 [l. [~J 6


J J. 3 [OJ [l.J 3 -
3 l. [OJ I 1

(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]
x= 0 1 1 3
x= 0 1 1

[.f 1 1 1
[.f 1 8 8 8
...
[3]
8 [3 1 l
><
x
.) .) ] ~
[1] 3 .. ..
[1]
... 6 8 [l] 6 .)
.
.)
.
1

~
• [O ~ 6 6
3
J
[l] ':
6 6
[OJ 8 . Pass 2 : (5-2=3)
Pass 1 : Comparison

I (5-1=4)
Comparisons

-
x=

-
x= 0 l.
. (-tJ [4 J 8
(3J
8 8
(3J
.
[::! J l. [:? 6
[l.J
_.... J
[l.J l
(OJ (j .. [OJ s l.

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]
x= 0 1 2 3 x= 0 1 2

-
(.t I 8 8 8 [.t 1 8 8 8
(3) i ~
':
i [3 I I

(2) 6 6 () 6 6 I ][2] 6 6 6 6
(1) 3 3 3 3 3 [l] 3 3
[OJ l 1 1
[OJ 1
1
1
Pass 1 : Comparison (5-1=4) Pass 2 : Comparisons (5-2=3)

--
:<. 0 1 x= 0
=
l-' I 8 (-t] 8
(3) . (3)
(::?] 6
(1) 6 6
(1) 3 s 1 [l. ] 3
1 ].
[OJ 11

[OJ 1 1 3

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


2) Comparisons
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
#include<bits/stdc++.h>
#define swap(x,y) { x = x + y; y = x - y; x = x - y; }
using namespace std;

void bubbleSort(int arr[], int n)


{
int i, j;
bool flag;
// Outer pass
for(i = 0; i < n; i++)
{
flag = false; // Set flag as false
for(j = 0; j < n-i-1; j++) {
// Compare values
if( arr[j] > arr[j+1])
{
swap(arr[j],arr[j+1]);
flag = true;
}
}
if(!flag) {
break;
int main(int argv, char* argc[])
{
int arr[] = {1,5,6,8,3,4,7,2,9};
int n = sizeof(arr)/sizeof(int);
cout<<"Unsorted Array :";
for(int i=0;i<n;i++) // Print the Original Array
cout<<arr[i]<<" ";
cout<<endl;
bubbleSort(arr,n); // Call for Bubble Sort Function
cout<<"Sorted Array :";
for(int i=0;i<n;i++) // Print the Sorted Array
cout<<arr[i]<<" ";
return(0);
}
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; added in the algorithm to signal
++pass) whether there is any exchange of
{ sorted = true; // assume elements occur in certain pass.
sorted for (int x = 0; x < n-pass;
++x)
{ if (data[x] > data[x+1])
In external loop, sorted is set
{ // exchange items to true. If there is exchange
temp = data[x];
of data inside the inner loop,
data[x] = data[x+1];
data[x+1] = temp;
sorted is set to false.
sorted = false; // signal exchange Another pass will continue, if
} // end if sorted is false and will stop if
} // end for
sorted is true.
} // end for
} // end bubbleSort

14
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
Difference between Bubble sort and Insertion Sort
Bubble sort is a simple sorting algorithm that repeatedly goes through a list, comparing
adjacent pairs and swapping them if they are in the wrong order. Insertion sort, on the other
hand, is a simple sorting algorithm that builds the final sorted list by transferring one
element at a time.

1) Functionality
While bubble sort checks the neighboring elements and swaps them
accordingly, insertion sort transfers an element at a time to the partially
sorted array.
2) Number of swaps
Also, the number of swaps is an important difference between bubble sort
and insertion sort. Insertion sort has less number of swaps compared to
bubble sort.
3) Speed
Moreover, insertion sort is twice as fast as bubble sort.
4) Complexity
Another difference between bubble sort and insertion sort is that insertion
sort is more complex than bubble sort.

You might also like