Page 163 -
P. 163
sort the data
You were think about what would make the program easier to
write and check the box that you think would have the greatest
impact:
If there were no names in the file, only numbers If the data were ordered highest to lowest
If the scores came before the names in the file If you knew exactly how many lines are in the file
An ordered list makes code much simpler
If you had some way of reading the data from the file and then producing an ordered copy
of the data, the program would be a lot simpler to write. Ordering data within a program
is known as “sorting:”
highest
_score The top three scores are just
An ordererd (or the first three values in the
sorted) copy of 9.12 sorted data, extracted to
The unsorted data the same data variables. Easy!
second_
highest
8.65 9.12
9.12 8.65
8.65
8.45 8.45
third_
highest
7.81 8.31
8.05 8.05
7.21 7.81
8.45
8.31 7.21
But how do you order, or sort, your data? What happens to the original data
in the file? Does it remain unsorted or is it sorted, too? Can the data even be
sorted on disk and, if so, does this make things easier, faster, or slower?
Sorting sounds tricky... is there a “best” way?
128 Chapter 4