Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Bubble Sort
 Compares adjacent array elements
– Exchanges their values if they are out of order
 Smaller values bubble u...
Bubble Sort (cont.)
 Example:
512354277 101
0 1 2 3 4 5
Swap42 77
Bubble Sort (cont.)
 Example:
512357742 101Swap35 77
0 1 2 3 4 5
Bubble Sort (cont.)
 Example:
512773542 101Swap12 77
0 1 2 3 4 5
Bubble Sort (cont.)
 Example:
577123542 101
No need to swap
0 1 2 3 4 5
Bubble Sort (cont.)
 Example:
577123542 101 Swap5 101
0 1 2 3 4 5
Bubble Sort (cont.)
 Example:
77123542 5 101
Largest value correctly placed
0 1 2 3 4 5
Bubble Sort (cont.)
• Notice that only the largest value is correctly
placed
• All other values are still out of order
• S...
Bubble Sort (cont.)
Repeat “Bubble Up” How Many Times?
 If we have N elements…
 And if each time we bubble an element, w...
Bubble Sort (cont.)
77123542 5
0 1 2 3 4 5
101
5421235 77
0 1 2 3 4 5
101
4253512 77
0 1 2 3 4 5
101
4235512 77
0 1 2 3 4 ...
Bubble Sort (cont.)
 What if the array was already sorted?
 What if only a few elements were out of place and after a co...
Bubble Sort (cont.)
Using a Boolean “Flag”
 We can use a boolean variable to determine if any swapping occurred during th...
public static void BubbleSort( int [ ] num )
{
int j;
boolean flag = true; // set flag to true to begin first pass
int tem...
Bubble Sort (cont.)
 Excellent performance in some cases
– But very poor performance in others!
 Works best when array i...
Upcoming SlideShare
Loading in …5
×

8 elementary sorts-bubble

484 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

8 elementary sorts-bubble

  1. 1. Bubble Sort  Compares adjacent array elements – Exchanges their values if they are out of order  Smaller values bubble up to the top of the array – Larger values sink to the bottom
  2. 2. Bubble Sort (cont.)  Example: 512354277 101 0 1 2 3 4 5 Swap42 77
  3. 3. Bubble Sort (cont.)  Example: 512357742 101Swap35 77 0 1 2 3 4 5
  4. 4. Bubble Sort (cont.)  Example: 512773542 101Swap12 77 0 1 2 3 4 5
  5. 5. Bubble Sort (cont.)  Example: 577123542 101 No need to swap 0 1 2 3 4 5
  6. 6. Bubble Sort (cont.)  Example: 577123542 101 Swap5 101 0 1 2 3 4 5
  7. 7. Bubble Sort (cont.)  Example: 77123542 5 101 Largest value correctly placed 0 1 2 3 4 5
  8. 8. Bubble Sort (cont.) • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 77123542 5 101 Largest value correctly placed 0 1 2 3 4 5
  9. 9. Bubble Sort (cont.) 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 correctly place all N elements.
  10. 10. Bubble Sort (cont.) 77123542 5 0 1 2 3 4 5 101 5421235 77 0 1 2 3 4 5 101 4253512 77 0 1 2 3 4 5 101 4235512 77 0 1 2 3 4 5 101 4235125 77 0 1 2 3 4 5 101 N-1
  11. 11. Bubble Sort (cont.)  What if the array was already sorted?  What if only a few elements were out of place and after a couple of “bubble ups,” the array was sorted?  We want to be able to detect this and “stop early”!
  12. 12. Bubble Sort (cont.) Using a Boolean “Flag”  We can use a boolean variable to determine if any swapping occurred during the “bubble up.”  If no swapping occurred, then we know that the collection is already sorted!  This boolean “flag” needs to be reset after each “bubble up.”
  13. 13. public static void BubbleSort( int [ ] num ) { int j; boolean flag = true; // set flag to true to begin first pass int temp; //holding variable while ( flag ) { flag= false; //set flag to false awaiting a possible swap for( j=0; j > num.length -1; j++ ) if ( num[ j ] > num[j+1] ) // change to > for ascending sort { temp = num[ j ]; //swap elements num[ j ] = num[ j+1 ]; num[ j+1 ] = temp; flag = true; //shows a swap occurred } } } }
  14. 14. Bubble Sort (cont.)  Excellent performance in some cases – But very poor performance in others!  Works best when array is nearly sorted to begin with  Worst case number of comparisons: n2  Worst case number of exchanges: n2  Best case occurs when the array is already sorted: – n comparisons – 0 exchanges

×