Board index » delphi » Sorting of records in binary file..

Sorting of records in binary file..

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..

Thanks

Michael Crone
cr...@it.net.au

 

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

Re:Sorting of records in binary file..


Quote
>           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..

   In terms of overall effort, yes, it's best to do the sorting in memory
(can you fit all the file records in memory?) and then rewrite the file
from the sorted memory array.  File sorts and the I/o they consume should
be the last option, and using a pointer array (which can store records in
about 430K of available Heap memory) to load and process large files
(10,000+ records, for example) is easy enough.

Re:Sorting of records in binary file..


Go to DSP or Torry's site and get Martin Waldenburg's mRecSort unit
(mRecSort.zip).  For fixed records, it's the fastest sorter I have seen .
Also, implements file and in-memory sorting.

BTW, in the future don't cross-post to so many NGs; pick one or else you'll
get flamed.

Fred Hovey
FredH@eathling_dot_net(replace _Dot_ with period to reply)

Quote
crone wrote in message <35D43575.CA197...@it.net.au>...
>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..

Re:Sorting of records in binary file..


hey!
very interesting algorythm.. seems to be fast.
Whats its name (dont laugh at me if it is Quicksort for I dont know
how that one works :)
<Marc II>

Re:Sorting of records in binary file..


hey!
very interesting algorythm.. seems to be fast.
Whats its name (dont laugh at me if it is Quicksort for I dont know
how that one works :)
<Marc II>

Other Threads