It's important to remember that the point of the chapter (and the book) was how to deconstruct the problem to find a solution optimized for its constraints, not how to sort arbitrary data. As such, the example using
qsort is a throwaway.
However, it's very poorly written, even from the perspective of the 1970s; I'm guessing the task was handed to one of Bentley's students. Here are a few of the issues:
- Wrong size of the array. This could have been solved by using a constant. Although see below.
- Mixing of concerns: reading, sorting, and writing are three different things, and should be handled by three different functions. OK, this is 21st century dogma, so maybe Bentley or his student can be forgiven. It still hides the core
qsort in a lot of superfluous reading and writing.
- The comparison function will be invalid for combinations of the minimum and maximum integers. A correct comparison must pay attention to the carry bit. You might argue that the text applies a constraint of "positive integers," but in that case the function should be named
posintc (I'm keeping to 7 characters because I recall this as an early *nix linker requirement; a modern programmer should use the much longer but more descriptive
- No bounds-checking: the reader reads until end of file, and if you give it more than 1,000,000 entries will happily overrun the buffer. This is IMO the biggest problem.
That said, you're also injecting your own requirements. Here's the problem statement, from page 7 of the 2nd edition, 20th printing:
If memory were not scarce, how would you implement a sort in a language with libraries for representing and sorting sets?
True, the text talks about a larger file. But that's not explicitly mentioned in the problem. And IMO programmers adding what they think are requirements is a huge problem with our industry.