Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

1,736 views

Published on

this is a simple ppt of bubble sorting in c language...

No Downloads

Total views

1,736

On SlideShare

0

From Embeds

0

Number of Embeds

3

Shares

0

Downloads

92

Comments

0

Likes

2

No embeds

No notes for slide

- 1. Sorting• Sorting takes an unordered collection andmakes it an ordered one.512354277 1011 2 3 4 5 65 12 35 42 77 1011 2 3 4 5 6
- 2. Exchange/Bubble Sort:It uses simple algorithm. It sorts by comparing each pairof adjacent items and swapping them in the order. Thiswill be repeated until no swaps are needed. Thealgorithm got its name from the way smaller elements"bubble" to the top of the list.It is not that much efficient, when a list is having morethan a few elements. Among simple sorting algorithms,algorithms like insertion sort are usually considered asmore efficient.Bubble sort is little slower compared to other sortingtechniques but it is easy because it deals with only twoelements at a time.
- 3. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping512354277 1011 2 3 4 5 6
- 4. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping512354277 1011 2 3 4 5 6Swap42 77
- 5. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping512357742 1011 2 3 4 5 6Swap35 77
- 6. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping512773542 1011 2 3 4 5 6Swap12 77
- 7. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping577123542 1011 2 3 4 5 6No need to swap
- 8. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping577123542 1011 2 3 4 5 6Swap5 101
- 9. "Bubbling Up" the Largest Element• Traverse a collection of elements– Move from the front to the end– “Bubble” the largest value to the end usingpair-wise comparisons and swapping77123542 51 2 3 4 5 6101Largest value correctly placed
- 10. Bubble Sort1. In each pass, we compare adjacent elements and swap them if they areout of order until the end of the list. By doing so, the 1stpass ends up“bubbling up” the largest element to the last position on the list2. The 2nd pass bubbles up the 2ndlargest, and so on until, after N-1passes, the list is sorted.Example:Pass 1 89 | 45 68 90 29 34 17 Pass 2 45 | 68 89 29 34 1745 89 | 68 90 29 34 17 45 68 | 89 29 34 1768 89 | 90 29 34 17 68 89 | 29 34 1789 90 | 29 34 17 29 89 | 34 1729 90 | 34 17 34 89 | 1734 90 | 17 17 8917 9045 68 89 29 34 17 90 45 68 29 34 17 89largest 2ndlargest
- 11. The “Bubble Up” Algorithmindex <- 1last_compare_at <- n – 1loopexitif(index > last_compare_at)if(A[index] > A[index + 1]) thenSwap(A[index], A[index + 1])endifindex <- index + 1endloop
- 12. No, Swap isn’t built in.Procedure Swap(a, b isoftype in/out Num)t isoftype Numt <- aa <- bb <- tendprocedure // SwapLB
- 13. Items of Interest• Notice that only the largest value iscorrectly placed• All other values are still out of order• So we need to repeat this process77123542 51 2 3 4 5 6101Largest value correctly placed
- 14. Repeat “Bubble Up” How Many Times?• If we have N elements…• And if each time we bubble an element,we place it in its correct location…• Then we repeat the “bubble up”process N – 1 times.• This guarantees we’ll correctlyplace all N elements.
- 15. “Bubbling” All the Elements77123542 51 2 3 4 5 61015421235 771 2 3 4 5 61014253512 771 2 3 4 5 61014235512 771 2 3 4 5 61014235125 771 2 3 4 5 6101N-1
- 16. Reducing the Number of Comparisons12354277 1011 2 3 4 5 6577123542 51 2 3 4 5 61015421235 771 2 3 4 5 61014253512 771 2 3 4 5 61014235512 771 2 3 4 5 6101
- 17. Reducing the Number of Comparisons• On the Nth“bubble up”, we only need todo MAX-N comparisons.• For example:– This is the 4th“bubble up”– MAX is 6– Thus we have 2 comparisons to do4253512 771 2 3 4 5 6101
- 18. Putting It All Together
- 19. N is … // Size of ArrayArr_Type definesa Array[1..N] of NumProcedure Swap(n1, n2 isoftype in/out Num)temp isoftype Numtemp <- n1n1 <- n2n2 <- tempendprocedure // Swap
- 20. procedure Bubblesort(A isoftype in/out Arr_Type)to_do, index isoftype Numto_do <- N – 1loopexitif(to_do = 0)index <- 1loopexitif(index > to_do)if(A[index] > A[index + 1]) thenSwap(A[index], A[index + 1])endifindex <- index + 1endloopto_do <- to_do - 1endloopendprocedure // BubblesortInnerloopOuterloop
- 21. Summary• “Bubble Up” algorithm will move largestvalue to its correct location (to the right)• Repeat “Bubble Up” until all elements arecorrectly placed:– Maximum of N-1 times• We reduce the number of elements wecompare each time one is correctly placed
- 22. Thank You

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment