Professional Documents
Culture Documents
Bubble Sort
Bubble Sort
SORT
(Algorithm)
Group: 06
Zainab
Jens Martensson
Naafey
1053
Syeda
Kiran 1055
Nimra
Shabbir
5002
2
Bubble sort
Bubble Sort is “a sorting algorithm used to sort list items in ascending
order by comparing two adjacent values.”
Jens Martensson
3
Bubble sort
Bubble Sort is “a sorting algorithm used to sort list items in ascending
order by comparing two adjacent values.”
Jens Martensson
It is a comparison based algorithm
4
Bubble sort
Bubble Sort is “a sorting algorithm used to sort list items in ascending
order by comparing two adjacent values.”
Jens Martensson
It is a comparison based algorithm.
5
Bubble sort
Bubble Sort is “a sorting algorithm used to sort list items in ascending
order by comparing two adjacent values.”
Jens Martensson
It is a comparison based algorithm.
6
Bubble sort
It’s name is derived from the fact that smaller data item bubble up..
Jens Martensson
7
Bubble sort
It’s name is derived from the fact that smaller data item bubble up..
Jens Martensson
It’s alternative term is sink down.
8
Bubble sort
It’s name is derived from the fact that smaller data item bubble up..
Jens Martensson
It’s alternative term is sink down.
9
Bubble sort
It’s name is derived from the fact that smaller data item bubble up..
Jens Martensson
It’s alternative term is sink down.
10
Real life Example
Water Bubbles
Jens Martensson
Dictionary.
Tv channels.
11
Working
Steps followed by Bubble sort algorithm are:
Jens Martensson
2) Inner loop will pass (n-i-1) times and j=0.
Jens Martensson
7) Exit inner loop.
9) End main.
13
Example:
If we take an array containing 5 elements.
The array will be sorted in (n-1) times the loop pass i.e. We can get our
Jens Martensson
sorted array in 4 pass.
Here I take an array A[6].
14
1st Loop Pass:
Jens Martensson
15
After (n-1)th pass:
Jens Martensson
16
Another Example
Jens Martensson
17
Time Complexity Analysis of Bubble :
In Bubble Sort, n-1 comparisons will be done in the 1st pass, n-2 in 2nd pass,
n-3 in 3rd pass and so on.
So the total number of comparisons will be, Hence the time complexity of
Bubble Sort is O (n2).
Jens Martensson
Here big-O shows the worst case time complexity. We can change it’s
complexity into O(n) by making it adaptive.
18
Implementation of Bubble sort:
Firstly we had made a display
function to display our array.
Second function we made was
to sort our array.
Function bubble_sort uses two
loops.
Jens Martensson
Outer loop tells us the number
of the path and it iterates (n-
1)times.
Inner loop tells us the numbers
of swap and it iterates (n-i-
1)times.
Inside inner loop we will
satisfy our condition.
If condition is true the values
will be interchanged
otherwise each value will be
compared. 19
Implementation of Bubble sort:
Jens Martensson
function before sorting .
We will call bubble_sort
function.
20
Implementation of Bubble sort:
Output
of the
Jens Martensson
Example
program.
21
Large image Questions:
If we got 2 same element in array it will be sorted in the same order they were
entered it is also the reason it is a stable algorithm.
Jens Martensson
Alphabets are stored in the same way as numbers.
23
Adaptive bubble sort:
Jens Martensson
Best case occurs when array is
already sorted.
24
Thank You