0% found this document useful (0 votes)
18 views17 pages

Bubble Sort

Uploaded by

examplesupply
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views17 pages

Bubble Sort

Uploaded by

examplesupply
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Department of BES-1

COMPUTATIONAL THINKING FOR


STRUCTURED DESIGN

Topic

BUBBLE SORT
Session - 25

CREATED BY K. VICTOR BABU


AIM OF THE SESSION
To make student understand the importance of ordered data
and make them well versed in sorting data using bubble sort.

INSTRUCTIONAL OBJECTIVES
1. Define Sorting and its advantages.
2. Understand the importance of sorted data over unsorted.
3. Understand the concept of sorting using the technique of Bubble
sort.
4. Illustrate the time complexity of Bubble sort.
LEARNING OUTCOMES
At the end of this session, you should be able to:
1. Illustrate the importance of sorted data.
2. Sort given array of elements using Bubble sort.
3. State the time complexity of Bubble sort.

CREATED BY K. VICTOR BABU


INTRODUCTION

 In computer computations that deal with processing data stored in


memory, majorly demands data to be available in some order for
efficient processing.

 Ordering data- sorting is a major task for reducing the


computational complexity.

CREATED BY K. VICTOR BABU


Session Description

•Bubble Sort is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order.

•This algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high..

CREATED BY K. VICTOR BABU


Session Description

In Bubble Sort algorithm,


•Traverse from left and compare adjacent elements and the higher one is placed at right
side.

•In this way, the largest element is moved to the rightmost end at first.

•This process is then continued to find the second largest and place it in the second right
most end and so on until the data is sorted.

CREATED BY K. VICTOR BABU


Session Description

How does Bubble Sort Work?


Let us understand the working of bubble sort with the help of the following illustration:
Input: arr[] = {6, 0, 3, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the array.

CREATED BY K. VICTOR BABU


Session Description

Second Pass:
Place the second largest element at correct position

CREATED BY K. VICTOR BABU


Session Description

Third Pass:
Place the remaining two elements at their correct positions.

Total no. of passes: n-1


In Each Passes n-p Comparisons
Total no. of comparisons: n*(n-1)/2
CREATED BY K. VICTOR BABU
Session Description

void bubbleSort(int array[], int size) {


// loop to access each array element
for (int p = 1; p < size; p++) {
// loop to compare array elements
for (int i = 0; i < size - p; i++) {
// compare two adjacent elements ,change > to < to sort in descending order
if (array[i] > array[i + 1]) {
// swapping occurs if elements are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}

CREATED BY K. VICTOR BABU


Session Description
Time Complexity: O(N2)
Auxiliary Space: O(1)

Advantages of Bubble Sort:

•Bubble sort is easy to understand and implement.


•It does not require any additional memory space.
•It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the
sorted output.

Disadvantages of Bubble Sort:

•Bubble sort has a time complexity of O(N2) which makes it very slow for large data sets.
•Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to determine
the relative order of elements in the input data set. It can limit the efficiency of the algorithm in certain cases.

CREATED BY K. VICTOR BABU


ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

Lets raise the question- what is the daily use of the algorithm discussed?—
Well the reply is straight forward and simple. We have been witnessing this from our
school days in our physical educational classes or in sports. The teacher asks to
assemble in line in front of the teacher in ascending order of heights. Now, comes the
well discussed sorting algorithm –The Bubble sort. Here in every pass the teacher
arranges the students slowly by comparing in pairs and interchanging them. The
process finally ends in ordered line of students in increasing order of their heights .

CREATED BY K. VICTOR BABU


EXAMPLES

1.Perform the bubble sort operation on the following


numbers:
92, 45, 89, -2, 58, 55, -5

2. Give the first 3 phases in detail for the following


numbers:

34, 56, 23, 28, 45, 99, 10, 31, 77

CREATED BY K. VICTOR BABU


SUMMARY

• This session explores the importance of sorting


and process involved in sorting the data using
bubble sort.

• The session also adds a clear derivation of the


best and worst case complexities of bubble sort.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Define sorting and mention its advantages.

2. What is the status of the following array after the 4th pass [45,
56,2, 58, 14,9] ?

3. Explain when does bubble sort give worst case complexity?

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

1. Mention the main process involved in bubble sort


technique?

2. Give 3 areas as where sorted data gives better


performance?

3. Mention the advantages of sorted data over unsorted?

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:

1. "The C Programming Language" by Brian Kernighan and Dennis Ritchie - This


is the classic book on C programming and is a great resource for learning about
functions in C.
2. "C Programming: A Modern Approach" by K. N. King - This book is another
excellent resource for learning C programming, including functions.
3. "C Functions" by Michael J. Misamore - This book is focused specifically on
functions in C and can be a great resource for those who want to deepen their
understanding of this topic.
Sites and Web links:
1. https://data-flair.training/blogs/bubble-sort/
2. https://www.geeksforgeeks.org/bubble-sort/

CREATED BY K. VICTOR BABU


THANK YOU

Team – Course Name

CREATED BY K. VICTOR BABU

You might also like