home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lect14b
sorting
with searching as a helper
-
We have seen code dealing with an array-of-Songs
(we called it Song[] album).
-
Review: Write a method that find the longest song on an album.
(What is the signature for this?
Clearly, a loop is in order;
What is your index variable?
What is your “soFar” (or “accumulator”) variable?)
-
Recall the max3, before we had loops!
But… what about finding the median-length song?
That (apparently1) requires sorting.
-
What would the signature for sort be?
We will sort “in place” — that is,
we will modify the contents of the array we are given to sort
(as opposed to leaving the original array as-is,
and returning a whole new different array with the same values, but sorted).
-
Discuss: algorithm for sorting, using longestSong as a helper.
Btw, this approach is called “insertion-sort”.
It is not the only (or necessarily the best) way to sort, but it has its strengths
(including how it is implemented w/ a helper-function which is handy in its own right).
-
We will complete the code in lecture.
Next topic: inheritance.
1Actually, some clever computer scientists
have figured out how to find the median element without fully sorting;
it's a beautiful algorithm. You can take Data Structures and Algorithms,
and/or see Wikipedia. ↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs