Board index » delphi » Sorting Files Direct To Disk

Sorting Files Direct To Disk

Hello everybody!

I have written a program in Pascal that will sort a file direct to
disk, the thing is though, it isn't very efficient, and it will read
through the file mulitple times, I enclose the source below, I'm sorry
if it isn't very straight-forward (I'm a messy programmer ;-)

I would appreciate it if anyone would be able to suggest a new
agorithm, or help me to optimise the one below

Thanks in advance

Jim

P.S. I have done some editing in IE before I posted it so there may be
some errors, sorry!

Program Sort;

uses crt, dos;

type testrec=record
     name:string[11];
     number:integer;
     end;

     timerec=record
     hour:word;
     min:word;
     sec:word;
     hund:word;
     end;

var startpos, lastpos, numitems, i:integer;
    filename:string;
    recfile:file of testrec;
    comparison, storage:testrec;
    starttime, endtime:timerec;

begin
     clrscr;
     Initialisefile;
     numitems:=tempnum;

     with starttime do
     gettime(hour, min, sec, hund);

     for startpos:=1 to numitems do
     begin
          seek(recfile, startpos-1);
          read(recfile, Storage);

          for i:=startpos to numitems do
          begin
          seek(recfile, i-1);
          read(recfile, comparison);
          if comparison.number<=storage.number then
             begin
                  storage:=comparison;
                  lastpos:=i;
             end;
          end;

          seek(recfile, startpos-1);
          read(recfile, comparison);
          seek(recfile, startpos-1);
          write(recfile, storage);
          seek(recfile, lastpos-1);
          write(recfile, comparison);
     end;

     with endtime do
     gettime(hour, min, sec, hund);

with starttime do
writeln('Started at ', hour, ':', min, ':', sec, ':', hund);
with endtime do
writeln('Started at ', hour, ':', min, ':', sec, ':', hund);

readkey;
end.

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:Sorting Files Direct To Disk


In article <7sm05p$3d...@nnrp1.deja.com>,

Quote
Jim  <james_dar...@my-deja.com> wrote:
>Hello everybody!

>I have written a program in Pascal that will sort a file direct to
>disk, the thing is though, it isn't very efficient, and it will read
>through the file mulitple times, I enclose the source below, I'm sorry
>if it isn't very straight-forward (I'm a messy programmer ;-)

Why do you want to sort on the disk?

The standard way to sort on disk is merge sort. First read as many
records as you can on the memory and sort them with an effective method.
Write the sorted runs into files, either a run per file, or in two
files with n runs on each. Then start merging them. Open two files, read
a record on each and write the smaller (or the one sorted first) to
another file. Pick th next record to replace the written one and compare
again. Continue this until the two runs have been merged. Do this
to all of the runs (picking two yet unhandled) until all have been
handled. If you still have more than one runs repeat the merging. Now
you could also merge more than two files at once.

Osmo

Other Threads