forked from mnhrdt/ccmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC10-sort
453 lines (313 loc) · 15.4 KB
/
C10-sort
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
Chapter 10
SORTS and SEARCHES
Summary
This library segment contains a number of sort
functions, and functions that implement efficient
search capabilities using trees and scatter
storage (hashing). The specific areas covered
are:
o Sort Functions
o Binary Trees
o Balanced (AVL) Binary Trees
o Hashing
Search functions implement the standard operations
of node insertion, node deletion, and a search for
a specified node.
-------------------------------------------------------------------------------
Notes on Contents
o Sort Functions:
Each of the sorting functions is a general purpose algorithm that
operates on an array or list of pointers to the elements to be sorted, and
accepts a user specified comparison function.
msort -------- execute a merged list sort.
qsrt -------- use the "quick-sort" algorithm.
hsort -------- use the "heap-sort" algorithm.
ssort -------- perform a Shell-sort.
Both ordinary binary trees and the balanced (AVL) tree are supported by
library functions.
o Binary Tree Functions:
btins -------- insert a node in a Binary tree.
btdel -------- delete a specified node from a Binary tree.
tsearch ------ search a tree for a node with a specified key.
tsort -------- sort the tree's nodes in ascending order.
prtree ------- print a map of the tree's node structure.
o Balanced (AVL) Trees:
batins ------- insert a node in a balanced tree.
batdel ------- delete a specified node from a balanced tree.
btsearch ----- search a balanced tree for a node with a
specified key.
btsort ------- sort the nodes of a balanced tree in ascending
order.
prbtree ------ print a node map of a balanced tree.
o Hashing:
hashins ------ insert a node into a hashed (scatter storage)
structure.
hashdel ------ delete a specified node from the hashed structure.
hfind -------- find the hashed node with a specified key.
hval --------- perform a simple hash array address computation.
-------------------------------------------------------------------------------
General Technical Comments
The sort techniques implemented include a Shell sort and a heap sort that
provide efficient and simple approaches to the internal sorting problem. In
addition, two high efficiency recursive algorithms, quicksort and merge sort,
are provided. The Shell, heap, and quicksort algorithms all operate on an
array of pointers to the actual list to be sorted, while merge sort sorts a
linked list. Use of the merge sort is advised in applications where the
linked list overhead is acceptable.
All the sort and search functions accept a user defined function for
key comparison. The convention adopted in interpreting the values returned
by this comparison function are those adopted in the standard C library:
1 -> first key > second key;
0 -> first key = second key; and
-1 -> first key < second key.
The binary tree data structure supports high efficiency searches. The use
of balanced trees will ensure a specified bound on the number of nodes that
must be traversed in any search. Therefore, its use is preferred over the
simple binary tree unless storage limits prohibit the balance flag required
in each node of the AVL data structure. Each tree function package includes
functions to insert a node, delete a node, find the node corresponding to a
specified key, and sort the nodes in ascending order of keys.
The hash storage utilities also provide an efficient data structure for
rapid searches. Collision is handled by using linked lists at each hash node.
This approach involves a lower overhead than the tree based structures at the
expense of somewhat less efficient storage utilization when nodes are sparse.
Considerable work has been done on hash key generation, and the user may wish
to replace the simple hval function supplied in the library with another
tailored to his application.
-------------------------------------------------------------------------------
FUNCTION SYNOPSES
-------------------------------------------------------------------------------
Sort Functions:
-------------------------------------------------------------------------------
ssort
Conduct a Shell sort of the array v[] with comparison function comp.
void ssort(void *v,int n,int (*comp)())
v = array of pointers to sort input
(pointers index sorted array at exit)
n = dimension of input array
comp = pointer to comparison function (see numcmp for example)
-----------------------------------------------------------------
hsort
Perform a heap sort with comparison function comp on the array v[].
void hsort(void *v,int n,int (*comp)())
v = array of pointers to sort input
(pointers index sorted array at exit)
n = dimension of input array
comp = pointer to comparison function (see numcmp for example)
-------------------------------------------------------------------
qsrt
Quicksort algorithm for sorting array v[] with comparison function comp.
void qsrt(void *v,int i,int j,int (*comp)())
i = start index of array (normally i=0 at top level)
j = maximum index of array (= dimension-1)
v = array of pointers to sort array
(pointers index sorted array at exit)
comp = pointer to comparison function (see numcmp)
The quicksort function is recursive. It may require an increase
in stack size when a large array is sorted.
---------------------------------------------------------------------
msort
Merge sort a linked list using comparison function comp.
struct llst *msort(struct llst *st,int dim,int (*comp)())
st = pointer to head of linked list
dim = length of list
comp = pointer to comparison function (see numcmp)
return value: ps = pointer to head of sorted list
The msort function generally yields the fastest internal
sorts. The function is recursive (see the note following
qsrt above). The linked list structure, used in msort,
is defined in the header files merge.h (listed below)
and ccmath.h. The list is terminated by a NULL pointer.
merge.h
#ifndef NULL
#define NULL 0L
#endif
struct llst {char *pls; struct llst *pt;};
-------------------------------------------------------------
numcmp
Numcmp contains numeric comparison functions for double or float and
integer variables. (It can easily be extended to other types.)
The return convention is identical to that used by the standard
library function 'strcmp'.
----------------------------------------------------------------
dubcmp
Compare double or float values.
int dubcmp(double *x,double *y)
x = pointer to first value
y = pointer to second value
return values: 0 -> *x = *y
1 -> *x > *y
-1 -> *x < *y
-------------------------------------
intcmp
Compare signed integer variables.
int intcmp(int *x,int *y)
x = pointer to first value
y = pointer to second value
return values: 0 -> *x = *y
1 -> *x > *y
-1 -> *x < *y
unicmp
Compare unsigned integer values.
int unicmp(unsigned *x,unsigned *y)
x = pointer to first value
y = pointer to second value
return values: 0 -> *x = *y
1 -> *x > *y
-1 -> *x < *y
-------------------------------------------------------------------------------
Search Functions:
-------------------------------------------------------------------------------
Trees
The header file tree.h, whose contents are reproduced in ccmath.h, is
used by all the binary tree functions to define the basic node data
structure. A signal parameter BAL is used to distinguish balanced trees from
ordinary binary trees. If BAL is defined, a balanced tree is assumed.
Otherwise the tree structure is a simple binary tree. The header node of a
tree is assumed to contain a dummy key that is lexically less than any key
value in the tree. The search code assumes that this header node is present.
tree.h
#ifndef NULL
#define NULL 0L
#endif
#ifdef BAL
struct tnode {char *key,*rec; int bal; struct tnode *pr,*pl;};
#else
struct tnode {char *key,*rec; struct tnode *pr,*pl;};
#endif
--------------------------------------------------------------------
btins
Insert a node in a binary tree structure.
struct tnode *btins(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: p = pointer to matching tree node
(may be a new node allocated by the function)
----------------------------------------------------------------
btdel
Delete a node with a specified key from a binary tree structure.
int btdel(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: status flag, with
0 -> no matching node in tree
1 -> node found and deleted
------------------------------------------------------
tsearch
Search a binary tree for a node with the specified key.
struct tnode *tsearch(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: p = pointer to matching node of tree
p = NULL -> no such node
---------------------------------------------------------------
tsort
Sort a binary tree's nodes in ascending key order.
void tsort(struct tnode *hd,struct tnode **ar)
hd = pointer to the header node of the tree
ar = pointer to array loaded with sorted node list
----------------------------------------------------------
prtree
Print a map of the node structure of a binary tree segment.
void prtree(struct tnode *hd,int m)
hd = pointer to starting node of tree segment
m = depth to which tree structure is to be printed
(depth is relative to starting node)
This function is useful for displaying the structure of
relatively short tree subsegments in test code.
-------------------------------------------------------------------------------
Balanced (AVL) Trees:
-------------------------------------------------------------------------------
The balanced (AVL) tree functions include a definition of BAL to signal
compilation with the proper node structure. The balance flag (int bal) must
be included in the structure for balanced trees. Thus,
#define BAL 1
must appear before ccmath.h or tree.h is referenced to ensure a correct
definition of the tree structure.
-------------------------------------------------------------------------------
batins
Insert a node with the specified key in a balanced tree.
struct tnode *batins(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: p = pointer to matching tree node
(may be a new node allocated by the function)
----------------------------------------------------------------
batdel
Delete the node matching the specified tree from a balanced tree.
int batdel(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: status flag, with
0 -> no matching node in tree
1 -> node found and deleted
-----------------------------------------------------
btsearch
Search a balanced tree for the node matching a specified key.
struct tnode *btsearch(char *kin,struct tnode *hd)
kin = pointer to input search key string
hd = pointer to head node of tree
return value: p = pointer to matching node of tree
p = NULL -> no such node
-----------------------------------------------------------
btsort
Sort the nodes of a balanced tree in ascending key order.
void btsort(struct tnode *hd,struct tnode **ar)
hd = pointer to the header node of the tree
ar = pointer to array loaded with sorted node list
-------------------------------------------------------------
prbtree
Print the node map of a balanced tree segment.
void prbtree(struct tnode *hd,int m)
hd = pointer to starting node of tree segment
m = depth to which tree structure is to be printed
(depth is relative to starting node)
This function is useful for displaying the structure of
relatively short tree subsegments.
-------------------------------------------------------------------------------
Scatter Storage (Hashing):
-------------------------------------------------------------------------------
The hash table structure used by scatter storage functions is defined
in the header files hash.h listed below, and in ccmath.h.
hash.h
#ifndef NULL
#define NULL 0L
#endif
struct tabl {char *key,*val; struct tabl *pt;};
Either hash.h or ccmath.h must be included when the hash functions are used.
-------------------------------------------------------------------------------
hashins
Insert a node into a scatter storage (hashed) structure.
struct tabl *hashins(char *kin,struct tabl *ha[],int mh)
kin = pointer to key string of insert
ha = hash array of pointers to struct tabl nodes
mh = dimension of hash array ha
return value: p = pointer to the inserted node
(storage for node is allocated by the function)
--------------------------------------------------------------
hashdel
Delete a node from a scatter storage structure.
int hashdel(char *kin,struct tabl *ha[],int mh)
kin = pointer to key string of insert
ha = hash array of pointers to struct tabl nodes
mh = dimension of hash array ha
return value: status flag, with
0 -> no such node
1 -> node found and deleted
--------------------------------------------------------
hfind
Find the hashed node corresponding to a specified key.
struct tabl *hfind(char *kin,struct tabl *ha[],int mh)
kin = pointer to key string to be located
ha = hash array of pointers to struct tabl nodes
mh = dimension of hash array ha
return value: p = pointer to record with key (kin)
p=NULL -> no such record
--------------------------------------------------------------
hval
Compute the hash array address of a key string.
int hval(char *key,int mh)
char *key;
key = pointer to input key string
mh = dimension of hash array
return value: k = harray address of this key (0 <= k < mh)