Write sorting/search methods that can be re-used (Views: 100)


I find that alot of developers that use sorting and search algorithms, taking the Quick Sort algorithm for an example, will reimplement it for every use.


Sorting algorithms rarely depend on actual knowledge what they are sorting, and when we require an algorithm and implement it, why restrict the algorithm to a specific use, as the algorithm itself will never change.

They are only dependent on an index of which they then need to compare and exchange the information that resides at those indexes.

The quick sort algorithm for the example would require only 3 main factors of which could be passed to a quick sort method.

Start and End indexes
Method for Comparing points
Method for Exchanging points

This going to apply for practially all sorting/searching algorithms.

All that is required is that we specify the types that will define the Compare and Exchange methods.

  TIndexCompare = function(const ixA, ixB: integer): integer of object;
  TIndexExchange = procedure(const ixA, ixB: integer) of object;
  //-- Also these methods could be also reused for multiple sort algorythms.
  //-- e.g
  //-- procedure InsertionSortByIndex(ixLo, ixHi: Integer;
  //--                                IndexCompare: TIndexCompare;
  //--                                IndexExchange: TIndexExchange);
  //--  etc....

procedure QuickSortByIndex(ixLo, ixHi: Integer;
  IndexCompare: TIndexCompare;
  IndexExchange: TIndexExchange);

procedure QuickSortByIndex(ixLo, ixHi: Integer;
  IndexCompare: TIndexCompare;
  IndexExchange: TIndexExchange);

  procedure SortIndex(aLo, aHi: Integer);
    I, J, P: Integer;
    tmpInt: Integer;
      I := aLo;
      J := aHi;
      P := (aLo + aHi) shr 1;
        while (I < aHi) and (IndexCompare(I, P) < 0) do
        while (J > aLo) and (IndexCompare(J, P) > 0) do
        if I <= J then
          IndexExchange(I, J);
          if P = I then
            P := J
          else if P = J then
            P := I;
      until I > J;
      if aLo < J then
        SortIndex(aLo, J);
      aLo := I;
    until I >= aHi;

  SortIndex(ixLo, ixHi);

Now to use this..lets say i want to sort a listbox for the example(rather than using  the Listbox standard sorting)

  TMyForm = class(TForm)
    ListBox1: TListBox;
    btnSort: TButton;
    function IndexCompare(const ixA, ixB: integer): integer;
    procedure IndexExchange(const ixA, ixB: integer);


function TMyForm.IndexCompare(const ixA, ixB: integer): integer;
//-- Source to compare items.
  Result := AnsiCompareText(ListBox1.Items[ixA], ListBox1.items[ixB]);

procedure TMyForm.IndexExchange(const ixA, ixB: integer);
// -- Source to exchange items.
  tmpStr: string;
  tmpStr := ListBox1.Items[ixA];
  ListBox1.Items[ixA] := ListBox1.Items[ixB];
  ListBox1.Items[ixB] := tmpStr;

procedure TMyForm.btnSortClick(Sender: TObject);
  with ListBox1.items do
      if UseQuickSort then
        QuickSortByIndex(0, count - 1, IndexCompare, IndexExchange)
        InsertionSortByIndex(0, count - 1, IndexCompare, IndexExchange);


Well hopefully that might of been some use

Later All

<< Back to main page