shdotcom
shdotcom

Reputation: 157

What I should change in this Delphi code, to get the right result?

In this Delphi code, should be store the minmum value into newA and delete it from A , this repeated until A reached to the size 0

type TDoubleDynArray  = array of Double;

          ..............

            ..........

//Get minimum value:

function MinValue(var minPos : Integer; Data: TDoubleDynArray): Double;
var
  I: Integer;
begin
  ShowMessageUser(IntToStr( Length(data)));
  Result := Data[Low(Data)];
  for I := Low(Data) + 1 to High(Data) do
    if Result > Data[I] then
      minPos := i;

  Result := Data[minPos];
end;

//-------------------------Main function ------------------

function TForm1.Func(X: TDoubleDynArray): Double;
var
A, newA : TDoubleDynArray;
i, minPos : Integer;
begin

   SetLength(A, 55);
   SetLength(newA, 55);
   A := Copy(X,0, 55);


      for i := 0 to 54 do
      begin
        newA[i] := MinValue(MinPos,A);
        Delete(A, MinPos, 1);
      end;

           ..............

            ..........


 end;

When i run the code it stop on size 3 (length of A = 3):

The Result of function MinValue() //ShowMessageUser(IntToStr( Length(data)));

55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 3 3

What I should change to get the right result (length(A) := 0)???

Upvotes: 0

Views: 200

Answers (2)

David Heffernan
David Heffernan

Reputation: 612794

There are numerous problems with your code. Most obviously, MinValue is badly broken. It fails to set minPos when the first value in the array is the lowest. That explains why your code fails.

However, you should probably remove all of the code and use the built in sorting functions. Your hand rolled selection sort doesn't work (yet) and even when fixed will still be O(n2).

Use TArray.Sort to perform the sort.

var
  SortedX: TArray<Double>;
....
SortedX := Copy(X, 0, 55);
TArray.Sort<Double>(SortedX);

This uses Quicksort which has a time complexity of O(n log n) on average.

Even if your version of Delphi predates TArray.Sort, conceptually you should be using a well proven sort algorithm, encapsulated cleanly. Don't roll your own ad-hoc sorting code inline like this.


As a minor aside, it's pointless to create a new array, and then throw it away as you do in this code:

SetLength(A, 55);
A := Copy(X, 0, 55);

The call to SetLength is pointless and wasteful because the newly created array is thrown away when replaced by the new array returned by Copy.

Upvotes: 4

Blurry Sterk
Blurry Sterk

Reputation: 1605

The problem seems to be in the MinValue function..

From what I can see you want to get the lowest value in the array. Currently you are only getting the last element in the array of lower value than the first element in the array irrespective of whether it is the lowest value in the total array. That happens because you keep on comparing to the Result which was set to the first element of the array and it never gets updated.

The code below gives back the lowest value in the array.

function MinValue(var minPos : Integer; Data: TDoubleDynArray): Double;
var
  I: Integer;
begin
  minPos := 0;

  for I := 1 to High(Data) do
    if Data[I] < Data[minPos] then
      minPos := I;

  Result := Data[minPos];
end;

Upvotes: 2

Related Questions