home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1993-11-04 | 3.2 KB | 101 lines |
- (******************************************************************************
-
- :Program. Sorting.mod
- :Contents. procedure for sorting
- :Author. Markus Uhlendahl
- :Address. Vorm Burgtor 16, D-4408 Dülmen
- :Phone. 02594/81540
- :Language. Modula-2
- :Translator. M2Amiga 4.0d
- :Copyright. Public Domain
- :History. 15.11.91 --- 1.0 --- first release
-
- ******************************************************************************)
- IMPLEMENTATION MODULE Sorting;
-
-
- PROCEDURE InternalLower (a,b : LONGINT;
- Lower : Comparison;
- ascending : BOOLEAN) : BOOLEAN;
- (*
- * FUNCTION compare two elements
- * If ascending is TRUE it returns element a lower than element b
- * else it returns element a greater than element b
- * INPUTS a = index of an element in the array
- * b = index of an element in the array
- * Lower = PROCEDURE which compares two elements of an array
- * returns TRUE if the first element of the comparison
- * is really lower (a<b) than the second element
- * ascending = if ascending is TRUE the result will be a<b else
- * the result will be b<a
- * RESULTS if ascending than a<b else b<a
- *
- *)
-
- BEGIN
- IF ascending THEN
- RETURN (Lower(a,b));
- ELSE
- RETURN (Lower(b,a));
- END;
- END InternalLower;
-
-
- PROCEDURE QuickSort (first,last : LONGINT;
- Lower : Comparison;
- Swap : SwapProcedure;
- ascending : BOOLEAN);
- (*
- * FUNCTION sort an array
- * This implementation of the quicksort-algorythm is very
- * flexible. It sorts arrays of every type and the user can
- * choose if the array is sorted ascending or not. To make this
- * possible the user has to write two procedures. One that
- * compares two elements of the array and one that swaps two
- * elements.
- * INPUTS first = first index of the array
- * last = last index of the array
- * Lower = PROCEDURE which compares two elements of the array
- * returns TRUE if the first element of the comparison
- * is really lower (a<b) than the second element
- * Swap = PROCEDURE which swaps two elements of the array
- * ascending = if ascending is TRUE the first element of the
- * array will be the smallest and the last element
- * the greatest
- * if ascending is FALSE it will be vice versa
- * BUGS none known
- * AUTHOR Markus Uhlendahl
- *
- *)
-
- VAR
- i,j,x : LONGINT;
-
- BEGIN
- i:=first;
- j:=last;
- x:=(first+last) DIV 2;
- REPEAT
- WHILE InternalLower(i,x,Lower,ascending) DO
- INC (i);
- END;
- WHILE InternalLower(x,j,Lower,ascending) DO
- DEC (j);
- END;
- IF i<=j THEN
- Swap (i,j);
- INC (i);
- DEC (j);
- END;
- UNTIL i>j;
- IF first<j THEN
- QuickSort (first,j,Lower,Swap,ascending);
- END;
- IF i<last THEN
- QuickSort (i,last,Lower,Swap,ascending);
- END;
- END QuickSort;
-
-
- END Sorting.
-