Child: [6c2cfa] (diff)

Download this file

SortLib.def    214 lines (178 with data), 12.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
DEFINITION MODULE SortLib;
(*------------------------------------------------------------------------*)
(* Stellt verschiedene Sortieralgorithmen primaer fuer Felder von Gleit- *)
(* kommazahlen aber auch anderer Datenstrukturen zur Verfuegung. *)
(* Module providing several sorting algorithms mainly for arrays of *)
(* reals but also genereal data structures *)
(*------------------------------------------------------------------------*)
(* Implementation : Michael Riedl *)
(* Licence : GNU Lesser General Public License (LGPL) *)
(*------------------------------------------------------------------------*)
(* $Id: SortLib.def,v 1.1 2018/01/16 09:20:20 mriedl Exp $ *)
FROM Deklera IMPORT VEKTOR,MATRIX,INTVEKTOR;
PROCEDURE InvertIndex(VAR In : ARRAY OF INTEGER;
I0 : ARRAY OF INTEGER;
n : INTEGER);
(*----------------------------------------------------------------*)
(* Reverts the index provided by e.g. QuickSortNR in such a way *)
(* that it can be directly used to print out the underlaying *)
(* array in ascending order *)
(* *)
(* FOR i:=0 TO n-1 DO WrLngReal(A[I[i]],12,4); END; *)
(*----------------------------------------------------------------*)
PROCEDURE QuickSortNR(VAR A : ARRAY OF LONGREAL;
VAR I : ARRAY OF INTEGER;
n : INTEGER);
(*----------------------------------------------------------------*)
(* Non recursive Quicksort routine which returns in I the invers *)
(* sort index but does NOT reorder the original vector A. More *)
(* precisely I[i] indicates where to place A[i] in a *)
(* sorted array B, B sorted in ascending order B[i] <= B[i+1] *)
(* *)
(* FOR i:=0 TO n-1 DO B[I[i]]:=A[i]; END; *)
(*----------------------------------------------------------------*)
PROCEDURE QuickSortRC(VAR A : ARRAY OF LONGREAL;
n : INTEGER);
(*----------------------------------------------------------------*)
(* Recursive Quicksort routine which returns in A the sorted *)
(* vector A in ascending order A[0..n-1] *)
(*----------------------------------------------------------------*)
PROCEDURE Find(VAR A : ARRAY OF LONGREAL;
n,k : INTEGER);
(*----------------------------------------------------------------*)
(* Reorder A such that A[k] is k-th largest *)
(*----------------------------------------------------------------*)
PROCEDURE ShakerSort(VAR A : ARRAY OF LONGREAL;
n : INTEGER);
(*----------------------------------------------------------------*)
(* Shaker Sort returns the array A in ascending order as *)
(* as descibed in Wirths "Algorithmen und Datenstrukturen" *)
(*----------------------------------------------------------------*)
PROCEDURE ShellSort(VAR A : ARRAY OF LONGREAL;
n : INTEGER);
(*----------------------------------------------------------------*)
(* ShellSort returns the array A in ascending order as *)
(* as descibed in Wirths "Algorithmen und Datenstrukturen" *)
(*----------------------------------------------------------------*)
PROCEDURE HeapSort(VAR A : ARRAY OF LONGREAL;
n : INTEGER);
(*----------------------------------------------------------------*)
(* HeapSort returns the array A in ascending order as *)
(* as descibed in Wirths "Algorithmen und Datenstrukturen" *)
(*----------------------------------------------------------------*)
PROCEDURE StraightMerge(VAR A : ARRAY OF INTEGER;
n : INTEGER);
(*----------------------------------------------------------------*)
(* StraightMerge returns the integer array A in ascending order *)
(* as descibed in Wirths "Algorithmen und Datenstrukturen" *)
(* Please not that index range of A is 0 .. 2*n-1 *)
(*----------------------------------------------------------------*)
PROCEDURE MergeSortRC(VAR A : ARRAY OF LONGREAL;
N : INTEGER);
(*----------------------------------------------------------------*)
(* Recursive Version von MergeSort (aufsteigende Ordnung) *)
(*----------------------------------------------------------------*)
PROCEDURE MergeSortNR(VAR A : ARRAY OF LONGREAL;
num : INTEGER);
(*----------------------------------------------------------------*)
(* Nicht recursive Version von MergeSort (aufsteigende Ordnung) *)
(*----------------------------------------------------------------*)
PROCEDURE MergeSort(VAR a : VEKTOR;
VAR p : INTVEKTOR;
low,up : INTEGER);
(*----------------------------------------------------------------*)
(* MergeSort delivers a permutation of indices corresponding to *)
(* sorting the elements of a given vector into non-decreasing *)
(* order. *)
(* The meaning of the formal parameters is: *)
(* a : array low:upp] *)
(* entry : vector to be sorted into nondecreasing order *)
(* exit : the contents of vec1 are left invariant *)
(* p : integer array [low:upp] *)
(* exit : the permutation of indices corresponding to *)
(* sorting the elements of a into nondecreasing *)
(* order *)
(* upp : the upper index of the arrays a and p *)
(*----------------------------------------------------------------*)
PROCEDURE VecPerm(VAR perm : INTVEKTOR;
low,upp : INTEGER;
VAR vector : VEKTOR);
(*----------------------------------------------------------------*)
(* VecPerm permutes the elements of a given vector corresponding *)
(* to a given permutation of indices. *)
(* The meaning of the formal parameters is: *)
(* perm : integer array [low:upp] *)
(* entry: a given permutation (e.g.as produced by *)
(* mergesort) of the numbers in the array vector *)
(* upp : the upper index of the arrays perm and vector *)
(* vector : array [low:upp] *)
(* entry : the real vector to be permuted *)
(* exit : the permuted vector elements. *)
(*----------------------------------------------------------------*)
PROCEDURE CVecPerm(VAR perm : ARRAY OF CARDINAL;
low,upp : CARDINAL;
VAR vector : ARRAY OF LONGCOMPLEX);
(*----------------------------------------------------------------*)
(* Complex Version of VecPerm *)
(*----------------------------------------------------------------*)
PROCEDURE RowPerm(VAR perm : INTVEKTOR;
low : INTEGER;
upp : INTEGER;
i : INTEGER;
VAR mat : MATRIX);
(*----------------------------------------------------------------*)
(* RowPerm permutes the elements of a given row of a matrix *)
(* corresponding to a given permutation of indices. *)
(* The meaning of the formal parameters is: *)
(* perm : integer array perm[low:upp] *)
(* entry: a given permutation as produced by mergesort *)
(* of the numbers in the array vector *)
(* upp : the upper index of the array perm *)
(* i : the row index of the matrix elements *)
(* matrix : array [i:i,low:up] *)
(* entry : matrix [i, low : up] should contain the *)
(* elements to be permuted *)
(* exit : matrix[i, low : up] contains the row of *)
(* permuted elements *)
(*----------------------------------------------------------------*)
TYPE VergleichsProz = PROCEDURE(CARDINAL,CARDINAL) : BOOLEAN;
VertauschProz = PROCEDURE(CARDINAL,CARDINAL);
PROCEDURE HeapSortGeneral(N : CARDINAL;
Vergleich : VergleichsProz;
Vertausche : VertauschProz);
(*---------------------------------------------------------------*)
(* Soriert nach dem Heapsortalgorithmus Daten, die global durch *)
(* die Prozeduren Vergleich und Vertausche manipuliert werden *)
(* k"onnen. *)
(* Dabei vergleicht Vergleich die Reihenfolge zweier beliebiger *)
(* Elemente i und j in der globalen Datenstruktur und Vertausch *)
(* f"uhrt ein Vertauschen dieser Elemente durch. *)
(* *)
(* F"ur ein einfaches Feld von Flie3kommazahlen *)
(* *)
(* VAR X : ARRAY [1..N] OF REAL; *)
(* *)
(* k"onnte Vergleich als *)
(* *)
(* PROCEDURE Kleiner(i,j : CARDINAL) : BOOLEAN; *)
(* *)
(* BEGIN *)
(* RETURN X[i] < X[j]; *)
(* END Kleiner; *)
(* *)
(* und Vertausch als *)
(* *)
(* PROCEDURE Vertausch(i,j : CARDINAL); *)
(* *)
(* VAR z : REAL; *)
(* BEGIN *)
(* z:=X[i]; X[i]:=X[j]; X[j]:=z; *)
(* END Vertausch; *)
(* *)
(* implementiert sein, was zu einer Sortierung der Elemente im *)
(* Feld X nach ihrer Gr"o3e in aufsteigender Reihenfolge f"uhren *)
(* w"urde. Allerdings ist der Algorithmus eher f"ur komplexere *)
(* Datenstrukturen gedacht, da es f"ur obiges Beispiel sicher *)
(* effizientere Algorithmen gibt. *)
(*---------------------------------------------------------------*)
END SortLib.