Recent

Author Topic: How can I make a sorting program for an array of objects in Pascal?  (Read 2742 times)

thomsimbeye

  • Newbie
  • Posts: 2
I have this program I'm making that uses objects and stores them in an array. I wrote this code but it won't compile, I would like some help on how to go about it as well as any alternative that involve the quick sort method.

 Function Partition(memberList:Array Of Member;start,stop:Integer):Integer;
  Var pivot:Member;
  Var pIndex:Integer;
  var temp:Member;
  Begin
      start:=0;
      pivot:=memberList[stop];
      For pIndex:=start To stop-1 Do
          Begin
              If memberList[pIndex].memberNo<=pivot.memberNo Then
                  Begin
                      temp:=memberList[pIndex];
                      memberList[pIndex]:=memberList[pIndex-1];
                      memberList[pIndex-1]:=temp;
                      pIndex:=pIndex+1;
                  End;
          End;
      temp:=memberList[pIndex];
      memberList[pIndex]:=memberList[stop];
      memberList[stop]:=temp;
        Partition:=pIndex;
  End;

Function QuickSort(memberList:Array Of Member;start,stop:Integer):Array Of Member;
    Var less:Array Of Member;
    Var greater:Array Of Member;
    Var pIndex:Integer;
    Begin
        If start<stop Then
            Begin
                pIndex:=Partition(memberList,start,stop);
                less:=QuickSort(memberList,start,pIndex-1);
                greater:=QuickSort(memberList,pIndex+1,stop);
            End;
        QuickSort:=less+greater;
    End;

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: How can I make a sorting program for an array of objects in Pascal?
« Reply #1 on: November 18, 2018, 11:20:33 pm »
I can't read your code well, here is example how sort array of records, for array of objects it's same.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Type
  4.   TItem = record
  5.     ID   : Integer;
  6.     x, y : Integer;
  7.   end;
  8.  
  9. Var
  10.   List : array of TItem;
  11.  
  12. Procedure QuickSort( iLo, iHi: Integer); // By X value
  13. var
  14.   t : TItem;
  15.   lo, hi, mid : Integer;
  16.  
  17. begin
  18.   lo  := iLo;
  19.   hi  := iHi;
  20.   mid := List[ ( lo + hi ) shr 1 ].x;
  21.  
  22.   repeat
  23.     while List[ lo ].x < mid do INC( lo );
  24.     while List[ hi ].x > mid do DEC( hi );
  25.     if lo <= hi then
  26.       begin
  27.         t          := List[ lo ];
  28.         List[ lo ] := List[ hi ];
  29.         List[ hi ] := t;
  30.         INC( lo );
  31.         DEC( hi );
  32.       end;
  33.   until lo > hi;
  34.  
  35.   if hi > iLo Then QuickSort( iLo, hi );
  36.   if lo < iHi Then QuickSort( lo, iHi );
  37.   end;
  38.  
  39. begin
  40.   QuickSort( 0, high(list) );
  41. end.

thomsimbeye

  • Newbie
  • Posts: 2
Re: How can I make a sorting program for an array of objects in Pascal?
« Reply #2 on: November 19, 2018, 09:04:47 am »
I'd like to make a function that returns an array of records/objects.P

Paul_

  • Full Member
  • ***
  • Posts: 143
Re: How can I make a sorting program for an array of objects in Pascal?
« Reply #3 on: November 27, 2018, 06:37:37 pm »
Return sorted array?

You can make copy of original array and sort items in this new one, with same function as above.

 

TinyPortal © 2005-2018