Sie sind auf Seite 1von 13

Mid Term Examination 2020

Data Structure and Algorithm Analysis


Batch:18CS, Semester: Second, Year: Second

Presentation Topic: Passes ,Comparison and Interchanges


in Bubble Sorting.
Presented By: Rafay Saif (18CS08)

Department of Computer Systems, Mehran UET.


Content
• About Bubble Sort.
• Explanation with Example.
• Number of Passes required.
• Number of Comparison required.
• Number of Interchanges required.
• References
• Conclusion.
Bubble Sort

• Bubble sort is an algorithm that compares the adjacent elements and swap their
positions if they are not in the intended order. The order can be ascending or
descending.
• It is known as bubble sort, because with every complete iteration the largest element
in the given array, bubbles up towards the last place or the highest index, just like a
water bubble rises to the water surface.
• To traverse through every element we use Passes.
Explanation with Example

Consider the following example:

42 33 23 74 44

We have to sort the following array in ascending order using bubble sorting.
Explanation with Example

Pass-1: Compare A[N]with A[N+1] and arrange them so that A[N] < A[N+1]
42 33 23 74 44

33 42 23 74 44

33 23 42 74 44

33 23 42 74 44

33 23 42 44 74
Explanation with Example

Pass-2 Repeat Pass 1 with one less comparisons

33 23 42 44 74

23 33 42 44 74

23 33 42 44 74

23 33 42 44 74
Explanation with Example
Pass-3: Repeat Pass 2 with one less comparisons

23 33 42 44 74

23 33 42 44 74

23 33 42 44 74
Explanation with Example

Pass-4: Repeat Pass 3 with one less comparisons

23 33 42 44 74

23 33 42 44 74
Number of Passes required.

The process of sequentially traversing through all or any part of an array is


known as PASS.
As explained in above example ,the Bubble Sort Algorithm requires total “N-1”
Passes.
Where N is the total number of elements.
In the above example we have total 5 element, so the total passes will be
Total Passes = 5-1 = 4.
Number of Comparison required.

The Bubble Sort Algorithm requires  “N-1” Comparisons in first Pass.


The Bubble Sort Algorithm requires  “N-2” Comparisons in second Pass.
Where N is the total number of input elements.
Therefore,
The total number of comparison will be “(N-1)+(N-2)+….(2)+(1) = N(N-1)/2”
In the above example we have total 5 element, so the total comparisons will
be
Total Comparisons = 5(5-1)/2 = 10
Number of Interchanges required.

The number of interchanges depend upon the data as well as on the total
number of elements (N).
The total number of interchanges is always less than or equals to the total
number of comparisons.
The total number of interchanges = Sum of all interchanges in all passes.
In the above example we have total 5 element, so the total interchanges will
be
Total Interchanges = 4 + 1 = 5.
References

• https://www.quora.com/What-is-the-total-number-of-comparisons-in-a-bubbl
e-sort
• Data Structures With C – by Schaum Series
• https://www.programiz.com/dsa/bubble-sort
• Principle of Data Structures Using C and C++ – by Vinu V DAS
Conclusion

THANK YOU! ANY QUESTIONS..?

Das könnte Ihnen auch gefallen