sort array 
Author Message
 sort array

can someone send me a procedure that will sort an array of integers.
or could you send me sites that have procedure like this



Mon, 29 Nov 1999 03:00:00 GMT  
 sort array

Quote:

>can someone send me a procedure that will sort an array of integers.
>or could you send me sites that have procedure like this

Program ArraySort;

8000 random numbers 0..999 are loaded into an array. To verify
loading, the array can be inspected manually, using <Esc> to bail out.
An exchange sort is performed. The sorted arrays may also be
inspected. This sort takes more than 10 secs at 66MHz. The same array
of numbers is generated (using the same RandSeed) and Shell sorted.
You will see a dramatic difference in speed. If you change MaxSize to
30000, the exchange sort takes over 3 minutes. The Shell, 3 seconds!
Both sorts are checked for accuracy with the program. On a small
array, such as 100, there is no visible difference in the two methods.

The exchange sort searches the array for the largest number and swaps
it with a[1], if larger. The next largest is swapped with a[2] if
needed, and so on to the end. It is a good one for beginners to use on
SMALL arrays because of the simple logic.

The Shell sort still exchanges contents but more cleverly. By the way,
the Shell sort is typically only about 15% slower than the QuickSort
but easier to understand. The first comparison is a[1] with one
half-way down, say a[nn]. If the contents of a[nn] is larger, the
contents are swapped. Now a[2] is compared to a[nn+1] and swapped if
required...and so on. If any numbers are swapped during this pass,
another identical pass is required to be sure the swaps did not
produce other disorders. When no changes are made, the separation
between the comparisons is halved, in this case, a[1] is compared to
a[nn div 2] for the first comparison..and so on. Since the last
spacing can be adjacent, starting with a[1] compared to a[2], and we
keep doing that spacing until nothing is moved--that guarantees the
array is in order. This last step is the bubble sort but there are FAR
fewer iterations (sometimes none).

For sorting records, an array of pointers is highly recommended. }

Uses CRT;   {ClrScr and ReadKey}

CONST  MaxSize = 8000; {making this 100 gets all 3 arrays on screen}

VAR
a:Array[1..MaxSize] of Integer;   {used inside procedures to conserve
                                   stack space}
Procedure MakeArray;
VAR ct:Integer;
Begin
     RandSeed := 12345;   {fixed to generate identical arrays}
     For ct := 1 to MaxSize Do a[ct] := Random(1000);
End;

Procedure ShowArray;
VAR p:Integer;
   ch:Char;
Begin
     p := 1;
     While p <= MaxSize Do
     Begin
          Write(a[p]:4);
          If p mod 400 = 0 Then
          Begin
               Writeln;
     Writeln('<Esc> to stop manual inspection, any other to
continue.');
               Ch := Readkey;
               If ch = Chr(27) then p := MaxSize ;
          End;
          Inc(p);
     End;
     Writeln;
End;

Procedure CheckErrors;
VAR z, errs:Integer;
Begin
     errs := 0;
     For z := 1 to MaxSize - 1 Do
         If  a[z + 1] > a[z] Then Inc(errs);
     Write(errs, ' Errors. ');
End;

Procedure ExchangeSort;
VAR  tmp, j, k, n : Integer;
Begin
     For j := 1 to MaxSize - 1 Do
     Begin
          tmp := j;
          For k := j + 1 to MaxSize Do
          Begin
               If a[k] > a[tmp] Then tmp := k;
          End;
          If tmp <> j Then
          Begin
               n := a[j];
               a[j] := a[tmp];
               a[tmp] := n;
          End;
     End;
End;

Procedure ShellSort;
VAR buf, jmp, mm, nn:Integer;
    done:Boolean;
Begin
     jmp := MaxSize;
     While jmp > 1 Do
     Begin
          jmp := jmp DIV 2;
          Repeat
                done := True;
                For mm := 1 to MaxSize - jmp Do
                Begin
                     nn := mm + jmp;
                     If a[nn] > a[mm] then  {swap and repeat entirely}
                     Begin
                          buf := a[mm];
                          a[mm] := a[nn];
                          a[nn] := buf;
                          done := False;
                     End;
                End;
          Until Done;
     End;
End;

Begin  {main program with exchange sort and Shell sort}

     MakeArray;
     ClrScr;
     Writeln(MaxSize, ' unsorted random numbers');
     ShowArray;

     Write('Press Enter to start Exchange sort--Takes > 10 secs');
     Readln;
     ExchangeSort;
     ShowArray;
     CheckErrors;

     MakeArray;
     Write('Exact same array made. Press Enter for Shell''s sort: ');
     Readln;
     ShellSort;
     ShowArray;
     CheckErrors;

     Writeln;  Write('Demo completed. Press Enter: ');    Readln;
End.



Tue, 30 Nov 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Shell sort vs. Comb sort (was: sorting arrays)

2. How to randomize a sorted array?

3. Sorting Array

4. sorting arrays

5. Sorting arrays help needed

6. algorithm to calculate the median using a sorted array

7. algorithm to calculate the median for a sorted array

8. Sorting Arrays

9. How to randomize a sorted array?

10. How to randomize a sorted array?

11. How to randomize a sorted array?

12. need helping sorting arrays of records

 

 
Powered by phpBB® Forum Software