Bubble Sort Demystified | InterviewBit - PowerPoint PPT Presentation

About This Presentation
Title:

Bubble Sort Demystified | InterviewBit

Description:

Also known as Comparison Sort, it is the most simplest sorting algorithm. So how does it work? If your given an unsorted sequence of Integers, Bubble Sort Algorithm will try to bubble up the largest possible number in the whole unsorted sequence, where the max value goes to its best possible position after each iteration. – PowerPoint PPT presentation

Number of Views:194
Slides: 13
Provided by: scaleracademy
Category:

less

Transcript and Presenter's Notes

Title: Bubble Sort Demystified | InterviewBit


1
Bubble
Sort Demystified
2
What is Bubble Sort?
Bubble sort Algorithm , also known as comparison
sort, is the most simplest sorting
algorithm. How Does It Work? It compares adjacent
elements and swaps them if they are in the wrong
order.
3
Bubble Sort Example.
Consider the following array Arr14, 33, 27,
35, 10. We need to sort this array using bubble
sort algorithm.
0 1 2 3 4 14 33 27 35 10
4
First Pass,
We proceed with the first and second element
i.e., Arr0 and Arr1. Check if 14 gt 33 which
is false. So, no swapping happens and the array
remains the same.
0 1 2 3 4 14 33 27 35 10
We proceed with the second and third element
i.e., Arr1 and Arr2. Check if 33 gt 27 which
is true. So, we swap Arr1 and Arr2.
0 1 2 3 4 14 33 27 35 10
5
Thus the array becomes
0 1 2 3 4 14 27 33 35 10
We proceed with the third and fourth element
i.e., Arr2 and Arr3. Check if 33 gt 35 which
is false. So, no swapping happens and the array
remains the same.
0 1 2 3 4 14 27 33 35 10
6
We proceed with the fourth and fifth element
i.e., Arr3 and Arr4. Check if 35 gt 10 which
is true. So, we swap Arr3 and Arr4.
0 1 2 3 4 14 27 33 35 10
Thus, after swapping the array becomes
0 1 2 3 4 14 27 33 10 35
Thus, marks the end of the first pass, where the
Largest element reaches its final(last) position.
7
Second Pass,
We proceed with the first and second element
i.e., Arr0 and Arr1. Check if 14 gt 27 which
is false. So, no swapping happens and the array
remains the same.
0 1 2 3 4 14 27 33 10 35
We now proceed with the second and third element
i.e., Arr1 and Arr2. Check if 27 gt 33 which
is false. So, no swapping happens and the array
remains the same.
8
We now proceed with the third and fourth element
i.e., Arr2 and Arr3. Check if 33 gt 10 which
is true. So, we swap Arr2 and Arr3.
3 4 10 35
0 1 2 14 27 33
Now, the array becomes 0 1 2 14 27 10
3 4 33 35
Thus marks the end of second pass where the
second largest element in the array has occupied
its correct position.
9
Third Pass,
After the third pass, the third largest element
will be at the third last position in the array.
0 1 2 3 4 14 10 27 33 35
Ith Pass, After the ith pass, the ith largest
element will be at the ith last position in the
array.
10
Nth Pass,
After the nth pass, the nth largest
element(smallest element) will be at nth last
position(1st position) in the array, where n
is the size of the array. After doing all the
passes, we can easily see the array will be
sorted. Thus, the sorted array will look like
this
0 1 2 3 4 10 14 27 33 35
11
Application
  • In real life, bubble sort can be visualised when
    people in a queue wanting to be standing in a
    height wise sorted manner swap their positions
    among themselves until everyone is standing
    based on increasing order of heights.

12
us _at_interviewbit
Follow
Write a Comment
User Comments (0)
About PowerShow.com