The SDL Component Suite is an industry leading collection of components supporting scientific and engineering computing. Please visit the SDL Web site for more information....



SortArray


Unit:SDL_math1
Class:None
Declaration: [1] procedure SortArray (ArrayAdr: pointer; LengArray: longint; TypArray: TVarKind; Ascending: boolean);
[2] procedure SortArray (SArray: array of integer; Ascending: boolean);
[3] procedure SortArray (SArray: array of single; Ascending: boolean);
[4] procedure SortArray (SArray: array of double; Ascending: boolean); {Pascal}
[2] void __fastcall SortArray(double * SArray, const int SArray_Size, bool Ascending);
[3] void __fastcall SortArray(int * SArray, const int SArray_Size, bool Ascending);
[4] void __fastcall SortArray(float * SArray, const int SArray_Size, bool Ascending); {C++}

The procedure SortArray sorts a one-dimensional numeric array either in ascending or descending order. This routine uses a modified bubble sort ('CompSort') algorithm, which is superior to 'QuickSort' in many cases. SortArray is available in four overloaded versions. Version [1] is only available for the Win32 environment, but not for .NET.

The parameter ArrayAdr holds a pointer to the array (use the operator '@'). The parameter LengArray defines the length of this array (i.e. the maximum number of elements of this array). The parameter LengArray may be defined smaller than the actual size of the array. In this case only the first LengArray elements are processed by SortArray.

The parameter TypArray defines the type of the array elements. The users should be aware that a wrong designation of the type of the array elements leads to severe errors and can cause a system crash. The following types are valid: inum (integer), lnum (longint), snum (single), dnum (double), and rnum (real); the types strg (string) and bool (boolean) are not supported.

As an alternative to version [1] you may use the simpler versions [2], [3], and [4] for integer, single, or double precision arrays, respectively.

The parameter Ascending specifies whether the array should be sorted by ascending (TRUE) or descending (FALSE) values.

SortArray increments the global variable ProcStat and calls the feedback routine MathFeedBackProc in order to allow feedback to the user during time consuming calculations.

Hint 1: If only part of the array is to be sorted the user can pass the address of the first element to be included in the parameter ArrayAdr and set the parameter LengArray to the number of elements to be included.

Hint 2: If your are using dynamic arrays you must not use the @ operator since dynamic array variables are pointers. For details, please see the program "sortinto", which is available for download from the SDL examples pages: http://www.lohninger.com/examples.html

Hint 3: The declaration of the versions [2]..[4] of SortArray in C++ slightly differs from the Pascal declaration (note the extra parameter SArray_Size which specifies the highest index of the SArray array).

Hint 4: Due to a compiler bug, overloading with variable dynamic arrays does not function correctly under Delphi 5. In order to cope with this situation, the four overloaded versions have been assigned individual names under Delphi 5: [1] = SortArray, [2] = SortArrayInteger, [3] = SortArraySingle, [4] = SortArrayDouble.

Example: The statement SortArray (@rfeld, 100, rnum, true); sorts the first 100 floating point numbers with the array rfeld by ascending values.

Example: This procedure is used in the following example program (see http://www.lohninger.com/examples.html for downloading the code): sortarray



Last Update: 2012-Oct-20