9781439035665 ppt ch14_bubble_sort

1,128 views
1,081 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,128
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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>

×