Back to Challenge Set 20

Code Challenge: Bubble Sort

Extra CreditOptional. Not required for any homework drop.

Difficulty  

divider

The Brief

What they asked for
Somebody told me there is more than one way to sort a list. Show
me a different one from the one you already showed me, and tell
me honestly which is better.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Bubble sort compares neighbors and swaps them if they are out of order. It repeats until a full pass makes no swaps at all.
  • That last part is the interesting one. Selection sort could not tell it was finished; this one can.
  • Print the array after every pass and count the comparisons.
  • Then run it on an array that is already sorted, and compare that count with the scrambled one, and both with selection sort's 15.
  • Answer the client's question. Give a real answer, not “it depends.”

Sample Output

Sample Output
After pass 1: 17 42 8 55 31 93
After pass 2: 17 8 42 31 55 93
After pass 3: 8 17 31 42 55 93
After pass 4: 8 17 31 42 55 93
Comparisons: 20, passes: 4 (selection sort: 15 every time)
The same list again, already sorted:
After pass 1: 8 17 31 42 55 93
Comparisons: 5, passes: 1 (selection sort: 15 every time)

This is the deck's array from Activity 2.12. Bubble sort does more work on it than selection sort, and far less on a sorted one.


Submit

Your requirements list and your program file, named cc-bubble-sort.

Commence Challenge