Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

BUBBLE

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.

Bubble sorting is the simplest sorting technique.

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.

Bubble sorting is the simplest sorting technique.

It is easy to analyze, implement and understand.

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.

Bubble sort is a stable algorithm.

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.

Bubble sort is a stable algorithm.

Time complexity of bubble sort is O().

10
Real life Example
Water Bubbles

Jens Martensson
Dictionary.

Contact list of your phone.

Tv channels.

11
Working
Steps followed by Bubble sort algorithm are:

1) Outer loop passes (n-1) times and i=0.

Jens Martensson
2) Inner loop will pass (n-i-1) times and j=0.

3) Now compare the two adjacent values A[j] and


A[j+1].

4) If values are not sorted swap them A[j] with


A[j+1].

5) Repeat step 3 for all elements in array. 12


Working
6) Repeat step 1,2,3 until we reaches the sorted list
of array.

Jens Martensson
7) Exit inner loop.

8)Exit outer 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:

Now array A[6] is sorted.


After each pass one largest unsorted number is aligned to its position.

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:

 Inside the main function, we


will get the size and element of
array.

 Then we will call display

Jens Martensson
function before sorting .
 We will call bubble_sort
function.

 Display function is called to


display the sorted array.

20
Implementation of Bubble sort:

Output
of the

Jens Martensson
Example
program.

21
Large image Questions:

What if we got 2 same


elements in array?

How we sort alphabets are


sorted ?

Is bubble sort adaptive?


Answers:

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.

Bubble sort is not adaptive by default but we can make it adaptive.

23
Adaptive bubble sort:

After using another variable to


make bubble sort adaptive .

Time complexity of bubble will


be O(n) and it is the best case.

Jens Martensson
Best case occurs when array is
already sorted.

24
Thank You

You might also like