Re:Sorting of records in binary file..
Quote
crone wrote:
> G'day,
> I've a bit of a problem in regard to a progam I have to
> write.. The program prompts the user to enter relevant details which
> are
> then stored into a record.. One of these records is an integer value..
> Once the record has been completely entered, it is written to a binary
> file.. What the program then has to do, is sort the records according
> to
> the integer value field.. As I understand, I could put the records
> into
> an array and then sort them in that.. End product, I have to have a
> binary file with the records sorted in it. Can anyone help me in doing
> this? Or maybe even suggest another way of doing it if that would be
> better..
Suppose I hand you a shuffled deck of cards and ask you to give me back
the deck
sorted by suit and rank. There are, however, the following rules: 1)
Start with the deck
face-down. At each step, you can only turn over the top card of each
face-down pile.
2) You can make up to 5 "piles" of face-up cards, putting each
turned-up top card of
a face-down pile in whichever pile you wish. 3) When you run out of a
face-down
pile, you can turn over any (or all) face-up pile and continue the
process.
Try taking a deck of cards and seeing how you'd do this. I'd suggest
using two face-up
piles at any one time.
Here's an example of how this might work. Suppose the deck is
completely sorted, except for a single mis-placed card. You start
turning over cards one at a time. You
start "Pile 1" with the first turned-over card. You notice that the
second turned-over card
comes "after" the card in Pile 1, so you again put it in Pile 1 (which
is now accumulating
a run of sorted cards). Keep going until you get the "out-of-order"
card, which you put in
"Pile 2". Keep going with the rest of the deck, which can all go on
Pile 1. Now you are
out of face-down cards, but have two (sorted!) face-up piles. Turn them
both face down. Now turn over the top card of each pile. Looking at
only those two cards, start
building a new face-up pile, taking from either "Down Pile 1" or "Down
Pile 2" as needed
to maintain a sorted order (i.e. choose the "smaller" of the face-up
cards). When you are
done with this pass, you should have a sorted deck.
Now implement the algorithm in Pascal.
Bob Schor
Pascal Enthusiast