Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Large Arrays

Status
Not open for further replies.

AvgMoJoe

Technical User
Nov 7, 2007
26
US
Crystal Reports 2013 SP 2 P 1

I need to figure out how to sort a large array. I am getting an error "A Loop was evaluated more than the maximum number of times allowed."

This happens whether I use a bidirectional bubble sort (which I can understand since the iterations become large)

Code:
    Global numbervar array Arrival_to_Dispo_grpx;
    local numbervar loop1 := 0;
    local numbervar loop2 := 0;
    local numbervar temp := 0;
    //
    for loop2:=1 to ubound(Arrival_to_Dispo_grpx)-1 do(
    for loop1:=1 to ubound(Arrival_to_Dispo_grpx)-loop2 do(
    if Arrival_to_Dispo_grpx[loop1] > Arrival_to_Dispo_grpx[loop1+1] then
    (temp := Arrival_to_Dispo_grpx[loop1];
    Arrival_to_Dispo_grpx[loop1] := Arrival_to_Dispo_grpx[loop1+1];
    Arrival_to_Dispo_grpx[loop1+1] := temp)));

or a gnome sort.

Code:
Global numbervar array Arrival_to_Dispo_grpx;
    Local NumberVar Array Sorting_Array := Arrival_to_Dispo_grpx;
    Local NumberVar i := 2;
    Local NumberVar temp_value;
    while ( i < ( ubound(Sorting_Array) + 1 ))
    do
    (
        if ( Sorting_Array [i - 1] <= Sorting_Array [i] )
        then
            i := i + 1
        else
        (
            temp_value := Sorting_Array [i - 1];
            Sorting_Array [i - 1] := Sorting_Array [i];
            Sorting_Array [i] := temp_value;
            i := i - 1;
            if ( i < 2 ) then i := 2;
        );
    );
    Arrival_to_Dispo_grpx := Sorting_Array;

I saw somewhere that an array size of 455 was the limit for bubble sort, but the solutions referred had dead links.

I'm looking to either use a sorting algorithm that won't produce an error for large arrays, or as I'm building the array, input each new element in a sorted fashion.
 
I ended up using a custom crystal function for sorting (found here) which does not seem to have the same limitation. Now I only need to worry about an array exceeding 1000 which is less of a concern.

Code:
Function ShellSort(Values() As Number)

'exit if array has no elements
If Ubound(Values)=0 Then Exit Function

'account for optional arguments
Dim LastElement As number
LastElement = UBound(Values)

'Crystal Reports’ arrays are 1-based.
Dim FirstElement As Number
FirstElement = 1

Dim Elements As Number
Elements = LastElement - FirstElement + 1

'find the best value for distance
Dim distance As Number
Do
distance = distance * 3 + 1
Loop Until distance > Elements

Dim temp as number
Dim index As Number, index2 As Number

Do

distance = distance \ 3
For index = distance + FirstElement To LastElement
temp = Values(index)
index2 = index

Do While (Values(index2 - distance) > temp)
Values(index2) = Values(index2 - distance)
index2 = index2 - distance
If index2 - distance < FirstElement Then Exit Do
Loop

Values(index2) = temp

Next
Loop Until distance = 1

ShellSort=Values
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top