0% found this document useful (0 votes)
15 views

Bubble Sort

Cover complete topic of bubble sort with Example and algorithm.

Uploaded by

Arnav
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)
15 views

Bubble Sort

Cover complete topic of bubble sort with Example and algorithm.

Uploaded by

Arnav
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/ 12

BUBBLE SORT

WHAT IS BUBBLE SORT ?


• Bubble sort is a simple sorting method.
• Sorting: In DSA sorting refer to a process of arranging elements of an array or a list in
a particular order.
• Bubble Sort: This sorting algorithm is comparison based algorithm in which each pair
of adjacent elements is compared and the elements are swapped if they are not in order.
• Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order.
HOW BUBBLE SORTING IS DONE ?
• In Bubble sort algorithm, analysis of list is done in cyclic manner,
analyzing pairs of elements from left to right.
• If the left most element in the pair is less than the rightmost element,
the pair will remain in that order.
• But if the rightmost element is less than the leftmost element, then
the two elements will be switched.
• This cycle repeats from beginning to end until a pass in which no
switch/swapping occurs.
BUBBLE SORT EXAMPLE
Our array is arr[] = {4, 2, 8, 1, 6}.
Pass 1
• 4, 2, 8, 1, 6
• 4 and 2 are switched because 2 is less than 4.
• 2, 4, 8, 1, 6
• List will remain as it is as 4 is less than 8.
• 2, 4, 8, 1, 6
• 1 and 8 are switched because 1 is less than 8.
• 2, 4, 1, 8, 6
• 6 and 8 are switched because 6 is less than 8.
• 2, 4, 1, 6, 8
BUBBLE SORT EXAMPLE
• After pass 1 : 2, 4, 1, 6, 8
2 4 1 6 8

• After pass 2 : 2, 1, 4, 6, 8
2 1 4 6 8

• After pass 3 : 1, 2, 4, 6, 8
1 2 4 6 8 Sorted
Array
ALGORITHM FOR BUBBLE SORT
USING ARRAY
Assuming a list/array of n element.
1. Repeat step 2 and 3 for i = 0 to n-1.
2. Repeat following from j=0 to j < n – 2
• if arr [ j ] > arr [ j + 1 ], then swap places of these elements, else continue to the next iteration.
• Increment j by 1.

3. Increment i by 1.
4. Exit.
BUBBLE SORT PROGRAM IN C
BUBBLE SORT PROGRAM IN C
RABBITS AND TURTLES

• The positions of the elements in bubble sort will play a large part in determining its
performance. Large elements at the top of the list do not pose a problem, as they are
quickly swapped downwards. Small elements at the bottom, however, as mentioned
earlier, move to the top extremely slowly.
• Higher value elements, or element that occur at the end of the sequence, take few passes
and switches to get to the right spots. These are called “rabbits”.
• Lower value elements at last positions move very slowly to their correct places. These
are called :turtles”.
ADVANTAGES OF THE BUBBLE
SORT
• The bubble sort is a very memory-efficient because all of the ordering occurs within the
array or list itself. No new memory is allocated
• No new data structures are necessary, for the same reason.
• The bubble sort is comprised of relatively few line of code.
DISADVANTAGES OF THE BUBBLE
SORT
• The main disadvantage of the bubble sort method is the time it require.
• Presence of turtles can severely slow the sort.
THANK YOU!

You might also like