Bubble sort
Upcoming SlideShare
Loading in...5
×
 

Bubble sort

on

  • 864 views

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

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

Statistics

Views

Total Views
864
Views on SlideShare
864
Embed Views
0

Actions

Likes
1
Downloads
36
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Bubble sort Bubble sort Presentation Transcript

  • 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
  • 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.
  • "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
  • "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
  • "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
  • "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
  • "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
  • "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
  • "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
  • 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
  • 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
  • No, Swap isn’t built in.Procedure Swap(a, b isoftype in/out Num)t isoftype Numt <- aa <- bb <- tendprocedure // SwapLB
  • 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
  • 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.
  • “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
  • 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
  • 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
  • Putting It All Together
  • 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
  • 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
  • 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
  • Thank You