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.

9781439035665 ppt ch14_bubble_sort

1,203 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

9781439035665 ppt ch14_bubble_sort

  1. 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Bubble Sort
  2. 2. Objectives <ul><li>Explore how to sort an array using bubble sort </li></ul>
  3. 3. Sorting a List <ul><li>Bubble sort </li></ul><ul><ul><li>Suppose list[0...n - 1] is a list of n elements, indexed 0 to n - 1 </li></ul></ul><ul><ul><li>We want to rearrange, that is, sort the elements of list in increasing order </li></ul></ul><ul><ul><li>The bubble sort algorithm works as follows: </li></ul></ul><ul><ul><ul><li>In a series of n - 1 iterations, the successive elements list[index] and list[index + 1] of list are compared </li></ul></ul></ul><ul><ul><ul><li>If list[index] is greater than list[index + 1] , then the elements list[index] and list[index + 1] are swapped, that is, interchanged </li></ul></ul></ul>
  4. 4. Bubble Sort
  5. 5. Bubble Sort (continued)
  6. 6. Bubble Sort (continued)
  7. 7. Bubble Sort (continued)
  8. 8. Bubble Sort (continued)
  9. 9. Bubble Sort (continued)
  10. 10. Bubble Sort (continued) <ul><li>It is known that for a list of length n , on average bubble sort makes n ( n – 1) / 2 key comparisons and about n ( n – 1) / 4 item assignments </li></ul><ul><li>Therefore, if n = 1000, then to sort the list, bubble sort makes about 500,000 key comparisons and about 250,000 item assignments </li></ul>

×