1

我需要对记录文件进行排序,但我不太确定该怎么做。我有一个记录文件,我尝试使用简单的冒泡排序对它们进行排序,但我被卡住了,需要一些帮助。有人可以帮我吗?我将在下面列出代码。

HighScoreFile = file of HighScoreRecord;


var
frmEnterDetails: TfrmEnterDetails;
HighScoreMasterFile: HighScoreFile;
HighScore:HighScoreRecord;
FileArray:array[1..20] of HighScoreRecord;
i:integer;

Procedure Sort
var
  i,j,temp:integer;

AssignFile(HighScoreMasterFile, 'HighScores.dat');
Reset(HighScoreMasterFile);
while not eof(HighScoreMasterFile) do
begin    
  i:=i+1;
  read(HighScoreMasterFile, HighScore);
  FileArray[i].Name:=HighScore.Name;
  FileArray[i].Date:=HighScore.Date;
  FileArray[i].FinalScore:=HighScore.FinalScore;

  for i:=0 19 do
    for j:=0 to 18 do
      if FileArray[j].Score > FileArray[j+1].Score then
        begin
          FileArray[temp]:=FileArray[j];
          FileArray[j]:=FileArray[j+1];
          FileArray[j+1]:=FileArray[temp];
       end;
end;

任何帮助都会很棒。

4

1 回答 1

0

有可能 100% 不正确,我没有在 IDE 中测试。您可以使用调试器来逐步查看该算法是如何工作的。这个链接也很好地解释了冒泡排序是如何工作的http://delphi.wikia.com/wiki/Bubble_sort

    HighScoreFile = file of HighScoreRecord;


    var
    frmEnterDetails: TfrmEnterDetails;
    HighScoreMasterFile: HighScoreFile;
    HighScore:HighScoreRecord;
    FileArray:array[1..20] of HighScoreRecord;
    i:integer;

    Procedure Sort
    var
      i,j,temp:integer;
    begin
    AssignFile(HighScoreMasterFile, 'HighScores.dat');
    Reset(HighScoreMasterFile);
    i := 0;
    while not eof(HighScoreMasterFile) do
    begin    
      i:=i+1;
      read(HighScoreMasterFile, HighScore);
      FileArray[i].Name:=HighScore.Name;
      FileArray[i].Date:=HighScore.Date;
      FileArray[i].FinalScore:=HighScore.FinalScore;
    end;

      for i:=1 to 19 do
        for j:=1 to 18 do
          if FileArray[i].Score > FileArray[j+1].Score then
            begin
              temp := i;
              HighScore := FileArray[temp];
              FileArray[temp]:=FileArray[j];
              FileArray[j]:=HighScore;
           end;
  end;
于 2013-05-08T14:28:32.837 回答