Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Prochain SlideShare
Chargement dans…5
×

# Bubble sort

5 143 vues

Publié le

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

Publié dans : Technologie, Sports
• Full Name
Comment goes here.

Are you sure you want to Yes No
• I have always found it hard to meet the requirements of being a student. Ever since my years of high school, I really have no idea what professors are looking for to give good grades. After some google searching, I found this service ⇒ www.HelpWriting.net ⇐ who helped me write my research paper.

Voulez-vous vraiment ?  Oui  Non
Votre message apparaîtra ici
• Did you try ⇒ www.HelpWriting.net ⇐?. They know how to do an amazing essay, research papers or dissertations.

Voulez-vous vraiment ?  Oui  Non
Votre message apparaîtra ici
• Dating direct: ❶❶❶ http://bit.ly/369VOVb ❶❶❶

Voulez-vous vraiment ?  Oui  Non
Votre message apparaîtra ici

Voulez-vous vraiment ?  Oui  Non
Votre message apparaîtra ici

Voulez-vous vraiment ?  Oui  Non
Votre message apparaîtra ici

### Bubble sort

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