home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / flogger / part02 < prev    next >
Encoding:
Text File  |  1992-12-17  |  21.8 KB  |  818 lines

  1. Newsgroups: comp.sources.misc
  2. From: mikey@ontek.com (Mike Lee)
  3. Subject:  v34i074:  flogger - Sort Flogger v0.0, Part02/02
  4. Message-ID: <1992Dec18.152304.10018@sparky.imd.sterling.com>
  5. X-Md4-Signature: 16f87fcec79ce1120461338e75f2a1fc
  6. Date: Fri, 18 Dec 1992 15:23:04 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: mikey@ontek.com (Mike Lee)
  10. Posting-number: Volume 34, Issue 74
  11. Archive-name: flogger/part02
  12. Environment: UNIX
  13.  
  14. #! /bin/sh
  15. # This is a shell archive.  Remove anything before this line, then feed it
  16. # into a shell via "sh file" or similar.  To overwrite existing files,
  17. # type "sh file -c".
  18. # Contents:  INSTALLATION Makefile bogo_sort.c bubble_sort.c
  19. #   insert_sort.c shell_sort.c sorting.h
  20. # Wrapped by kent@sparky on Thu Dec 17 13:50:35 1992
  21. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  22. echo If this archive is complete, you will see the following message:
  23. echo '          "shar: End of archive 2 (of 2)."'
  24. if test -f 'INSTALLATION' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'INSTALLATION'\"
  26. else
  27.   echo shar: Extracting \"'INSTALLATION'\" \(1740 characters\)
  28.   sed "s/^X//" >'INSTALLATION' <<'END_OF_FILE'
  29. X
  30. XMakefile is set up for SunOS 4.1.  If you want to use gcc, move 
  31. Xthe # comment characters in the makefile around appropriately.
  32. X
  33. XIf your machine has memmove() comment out the #define in sorting.h
  34. X
  35. XIf you don't have memcpy, comment back in:
  36. X  #define memcpy(to, from, n) bcopy(from, to, n)
  37. Xor better yet upgrade your OS.
  38. X
  39. XType "make" or "make flogger.static"
  40. X
  41. XIf you want to install the sort algorithm library for real
  42. X
  43. X  su root
  44. X  make install
  45. X
  46. X---
  47. X
  48. XPotential portability problems:
  49. X
  50. XShared libraries might work differently, or not at all, on your
  51. Xmachine.  Edit the makefile as necessary.
  52. X
  53. XYou might be missing mallinfo.  Some rewriting of the code will
  54. Xbe in order.  I'll put some ifdef's around this sooner or later.
  55. X
  56. XIf your machine doesn't use a traditional stack, the stack information
  57. Xwill be either plain wrong or it might cause segmentation faults.
  58. XBut if you don't care about the flogger routine-- no big deal.  The
  59. Xsort library itself should be quite portable, even if the test routine
  60. Xisn't.
  61. X
  62. XThe typedefs for chunk4 and chunk8 are thoroughly questionable.  If
  63. Xyou have a more "natural" size on your machine, experiment.
  64. X
  65. XIf you are porting to a machine which doesn't care about alignment
  66. Xvery much, you can remove the extra alignment tests from merge
  67. Xsort and quick sort for a marginal increase in sort speed under some 
  68. Xcircumstances.
  69. X
  70. Xmemcpy() is used--if you have a better block-copy function at your
  71. Xdisposal, by all means plug it in.  Won't help much if you're only
  72. Xsorting 4-byte things like ints or pointers, though, 'cause
  73. Xmerge_sort has special code for these anyway.  
  74. X
  75. Xsend patches, bug fixes, plane tickets to pittsburgh, etc to mikey@ontek.com
  76. X
  77. XI'm especially interested in portability of the merge_sort routine.
  78. X
  79. END_OF_FILE
  80.   if test 1740 -ne `wc -c <'INSTALLATION'`; then
  81.     echo shar: \"'INSTALLATION'\" unpacked with wrong size!
  82.   fi
  83.   # end of 'INSTALLATION'
  84. fi
  85. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  86.   echo shar: Will not clobber existing file \"'Makefile'\"
  87. else
  88.   echo shar: Extracting \"'Makefile'\" \(1541 characters\)
  89.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  90. X
  91. XCC =        cc
  92. XCFLAGS =    -O4 -pic
  93. XSTATICFLAGS =    -Bstatic
  94. X
  95. X# CC = gcc
  96. X# CFLAGS =     -fpic -O2 -ansi -traditional \
  97. X#         -W -Wunused -Wcomment -Wtrigraphs -Wformat -Wuninitialized \
  98. X#         -Wparentheses -Wshadow -Wpointer-arith -Wcast-qual \
  99. X#         -Wconversion -Wswitch -Wid-clash-32
  100. X# STATICFLAGS =    -static
  101. X
  102. X
  103. XLDFLAGS =     -assert pure-text
  104. XLINTFLAGS =     -abhuxz
  105. X
  106. X# don't insert a line break after the -d
  107. XSHARFLAGS =    -v -l40 -M -d__KRILL_ARE_YUMMY_AND_CRUNCHY
  108. X
  109. XVERSION =    0.0
  110. XSHELL =        /bin/sh
  111. X
  112. XOBJS =     merge_sort.o \
  113. X    quick_sort.o \
  114. X    heap_sort.o \
  115. X    shell_sort.o \
  116. X    bubble_sort.o \
  117. X    bogo_sort.o \
  118. X    insert_sort.o
  119. X
  120. Xflogger: flogger.o libsort.so.$(VERSION)
  121. X    $(CC) $(CFLAGS) -o flogger flogger.o -L. -lsort
  122. X
  123. Xflogger.static: flogger.o libsort.a
  124. X    $(CC) $(CFLAGS) $(STATICFLAGS) -o flogger.static flogger.o -L. -lsort
  125. X
  126. Xlibsort.a: $(OBJS)
  127. X    ar ru libsort.a $(OBJS)
  128. X    ranlib libsort.a
  129. X
  130. Xlibsort.so.$(VERSION): $(OBJS)
  131. X    ld $(LDFLAGS) $(OBJS) -o libsort.so.$(VERSION)
  132. X
  133. X$(OBJS): sorting.h
  134. X
  135. Xinstall: libsort.so.$(VERSION) libsort.a
  136. X    cp libsort.a /usr/lib
  137. X    chmod 644 /usr/lib/libsort.a
  138. X    cp libsort.so.$(VERSION) /usr/lib
  139. X    chmod 755 /usr/lib/libsort.so.$(VERSION)
  140. X    /usr/etc/ldconfig
  141. X
  142. Xlint:    lint.out
  143. X
  144. Xlint.out: *.c sorting.h
  145. X    lint $(LINTFLAGS) *.c > lint.out 2>&1
  146. X
  147. Xclean:
  148. X    -rm -f core shar0? $(OBJS) \
  149. X    libsort.so.$(VERSION) libsort.a flogger.o \
  150. X    flogger flogger.static lint.out
  151. X
  152. Xshar:    
  153. X    shar $(SHARFLAGS) -o shar \
  154. X    README INSTALLATION TODO OPTIMIZING Makefile bubble_sort.c \
  155. X    heap_sort.c merge_sort.c quick_sort.c shell_sort.c insert_sort.c \
  156. X    sorting.h flogger.c bogo_sort.c
  157. X
  158. END_OF_FILE
  159.   if test 1541 -ne `wc -c <'Makefile'`; then
  160.     echo shar: \"'Makefile'\" unpacked with wrong size!
  161.   fi
  162.   # end of 'Makefile'
  163. fi
  164. if test -f 'bogo_sort.c' -a "${1}" != "-c" ; then 
  165.   echo shar: Will not clobber existing file \"'bogo_sort.c'\"
  166. else
  167.   echo shar: Extracting \"'bogo_sort.c'\" \(4211 characters\)
  168.   sed "s/^X//" >'bogo_sort.c' <<'END_OF_FILE'
  169. X
  170. X#undef TEST
  171. X#define EXIT_SUCCESS 0
  172. X#define EXIT_FAILURE 1
  173. X#define RAND_MAX 32767
  174. X
  175. X/*
  176. X
  177. XIn alt.folklore.computers, there has been a discussion of the worst
  178. Xpossible sorting algorithm, which has been called "bogosort".
  179. X
  180. XMy interpretation of the algorithm is this: Exchange two randomly
  181. Xchosen elements of the array to be sorted.  Check to see if the array
  182. Xis in order.  Iterate until done.
  183. X
  184. XFrom what I've read, theoretical analysis of this algorithm gives it a
  185. Xperformance of O(n!), which means that the time to sort is
  186. Xproportional to the factorial of the number of elements.  And since
  187. Xthe algorithm is random in nature, it could range from instantaneous
  188. X(only two entries out of order, and it happens to exchange them first)
  189. Xto to infinite (it might never succeed).
  190. X
  191. XSo, for kicks I coded up a bogosort routine.
  192. X
  193. XIn my testing, I discovered that the mean time to sort an array of ten
  194. Xintegers was 75 seconds (25MHz 486, Unix, gcc 2.1, "optimized").
  195. X
  196. XExtrapolating from this, assuming O(n!), gives 312 days to sort 15
  197. Xintegers and 1,593,378 years to sort 20 integers.  Someone with a much
  198. Xfaster machine than mine will have to verify these figures.
  199. X
  200. X-- 
  201. XRichard Krehbiel                                 richk@grebyn.com
  202. XOS/2 2.0 will do for me until AmigaDOS for the 386 comes along...
  203. X
  204. X
  205. X===== cut here ====== bogosort.c =====================================
  206. X
  207. X*/
  208. X
  209. X#include <stdio.h>
  210. X#include <stdlib.h>
  211. X#include <time.h>
  212. X#include <assert.h>
  213. X
  214. X#define TRUE 1
  215. X#define FALSE 0
  216. X
  217. X#if 0
  218. X#define range_rand(range) ((long)rand() * (long)range / (RAND_MAX + 1))
  219. X
  220. X#if ! defined(range_rand)
  221. Xint range_rand(range) int range;
  222. X{
  223. X    return (long)rand() * (long)range / (RAND_MAX + 1);
  224. X}
  225. X#endif
  226. X
  227. X#endif
  228. X
  229. X/* bogo sort is sensitive to rand()'s repeating-lower-digits feature 
  230. X   plus the above macro and function gave me different results with
  231. X   and without -O, so I replaced them with this slow but serviceable
  232. X   function. */
  233. X
  234. Xint range_rand(range) 
  235. X  int range;
  236. X{
  237. X  int result;
  238. X  extern long random();
  239. X
  240. X  do { 
  241. X    result = (int) random(); 
  242. X  } 
  243. X  while (result < 0);
  244. X  
  245. X  result = result % range;
  246. X
  247. X  return result;
  248. X}
  249. X
  250. X
  251. Xvoid swap_elem(elem1, elem2, elem_size) 
  252. X               register char *elem1;
  253. X               register char *elem2;
  254. X               register size_t elem_size;
  255. X
  256. X{
  257. X    /***
  258. X    int temp;
  259. X
  260. X    temp = *(int*) elem1;
  261. X    *(int*) elem1 = *(int*) elem2;
  262. X    *(int*) elem2 = temp;
  263. X    ***/
  264. X
  265. X    while(elem_size-- > 0)
  266. X    {
  267. X        char temp;
  268. X        temp = *elem1;
  269. X        *elem1++ = *elem2;
  270. X        *elem2++ = temp;
  271. X    }
  272. X
  273. X}
  274. X
  275. Xint in_order(base, n_elem, elem_size, compare)
  276. X   register char *base;
  277. X   register int n_elem;
  278. X   register size_t elem_size;
  279. X   int (*compare)();
  280. X{
  281. X    while(--n_elem > 0)
  282. X    {
  283. X        if(compare(base, base+elem_size) > 0)
  284. X            return FALSE;
  285. X        base += elem_size;
  286. X    }
  287. X    return TRUE;
  288. X}
  289. X
  290. X/*
  291. X  The bogo() function:
  292. X
  293. X  This function is called with the same arguments as qsort.  When it returns,
  294. X  the elements of the given array are in order.
  295. X
  296. X  You may wish to call srand() before using bogosort.
  297. X*/
  298. X
  299. Xint bogo_sort(base, n_elem, elem_size, compare)
  300. X          char *base;
  301. X          int n_elem;
  302. X          size_t elem_size;
  303. X          int (*compare)();
  304. X{
  305. X    assert(n_elem <= RAND_MAX);
  306. X
  307. X    while(!in_order(base, n_elem, elem_size, compare))
  308. X    {
  309. X        register char *elem1, *elem2;
  310. X
  311. X        elem1 = base + (range_rand(n_elem) * elem_size);
  312. X        elem2 = base + (range_rand(n_elem) * elem_size);
  313. X
  314. X        swap_elem(elem1, elem2, elem_size);
  315. X    }
  316. X}
  317. X
  318. X#ifdef TEST
  319. X
  320. Xint array[100];        /* Up to 100 elements - no further */
  321. X
  322. Xint int_compare(i1, i2)
  323. X  char *i1; char *i2;
  324. X{
  325. X    return *(int *)i1 - *(int *)i2;
  326. X}
  327. X
  328. Xmain(argc, argv)
  329. X  int argc; char *argv[];
  330. X{
  331. X    time_t now;
  332. X    int n_elem;
  333. X    int i;
  334. X
  335. X    if(argc < 2)
  336. X    {
  337. X        fprintf(stderr, "useage: %s <number of elements>\n",
  338. X                argv[0]);
  339. X        exit(EXIT_FAILURE);
  340. X    }
  341. X
  342. X    n_elem = atoi(argv[1]);
  343. X    if(n_elem > 100)
  344. X    {
  345. X        fprintf(stderr, 
  346. X  "No more than 100 elements, please (as if your life is that long...)\n");
  347. X        exit(EXIT_FAILURE);
  348. X    }
  349. X
  350. X    now = time((time_t *)NULL);
  351. X    srandom(now);
  352. X
  353. X    fputs("Starting array:\n", stdout);
  354. X
  355. X    for(i = 0; i < n_elem; i++)
  356. X    {
  357. X        array[i] = random();
  358. X        printf("%d ", array[i]);
  359. X    }
  360. X    fputs("\n", stdout);
  361. X
  362. X    bogo_sort((char *)array, n_elem, sizeof(int), int_compare);
  363. X
  364. X    fputs("Ending array:\n", stdout);
  365. X    for(i = 0; i < n_elem; i++)
  366. X    {
  367. X        printf("%d ", array[i]);
  368. X    }
  369. X    fputs("\n", stdout);
  370. X}
  371. X#endif
  372. X
  373. END_OF_FILE
  374.   if test 4211 -ne `wc -c <'bogo_sort.c'`; then
  375.     echo shar: \"'bogo_sort.c'\" unpacked with wrong size!
  376.   fi
  377.   # end of 'bogo_sort.c'
  378. fi
  379. if test -f 'bubble_sort.c' -a "${1}" != "-c" ; then 
  380.   echo shar: Will not clobber existing file \"'bubble_sort.c'\"
  381. else
  382.   echo shar: Extracting \"'bubble_sort.c'\" \(3389 characters\)
  383.   sed "s/^X//" >'bubble_sort.c' <<'END_OF_FILE'
  384. X
  385. X/* 
  386. X
  387. XNAME
  388. X  bubble sort
  389. X
  390. XDESCRIPTION
  391. X
  392. X  Traipse up and down the records until the entire array is in order
  393. X  exchanging records with their neighbors if the adjacent records are
  394. X  out of order with respect to each other.  A flag keeps track of
  395. X  whether exchanges were done; when an entire pass is made and no
  396. X  exchanges were performed, the sort is complete.  Also, after each
  397. X  pass, the element at the end of the pass is guaranteed to be in it's
  398. X  final place so the next pass excludes it.
  399. X
  400. X  This algorithm reverses direction on each pass, so that a single item
  401. X  out of order won't force worst-case behavior.  Knuth refers to this
  402. X  as the "cocktail shaker sort."
  403. X
  404. XAUTHORSHIP
  405. X
  406. X  Unknown to me.
  407. X
  408. XREFERENCES
  409. X
  410. X  Knuth Vol 3, page 106-111
  411. X
  412. XCOMPLEXITY
  413. X
  414. X  Best case O(n)
  415. X  Worst case O(n^2)
  416. X  Iterative.
  417. X
  418. XCOPYRIGHT
  419. X
  420. X  Copyright 1992 Michael Lee.
  421. X
  422. X  (1) Permission to use, copy, distribute, and make changes is granted
  423. X  providing that (a) that you do so without charging anyone a fee and
  424. X  (b) this copyright notice must be included verbatim in all copies and 
  425. X  distributions.  
  426. X
  427. X  (2) There is no warranty for this program, to the extent permitted by
  428. X  applicable law.  This program is provided "AS IS" without warranty of
  429. X  any kind, either expressed or implied, including, but not limited to,
  430. X  the implied warranties of merchantability and fitness for a particular 
  431. X  purpose.  The entire risk as to the quality and performance of this 
  432. X  program is with the user.  Should this program prove defective, the 
  433. X  user assumes the cost of all necessary servicing, repair or correction.
  434. X
  435. X  (3) In no event unless required by applicable law will the copyright
  436. X  holder be liable to the user for damages, including any general,
  437. X  special, incidental or consequential damages arising out of the use
  438. X  or inability to use this program (including but not limited to loss of
  439. X  data or data being rendered inaccurate or losses sustained by the
  440. X  user or third parties or a failure of this program to operate with any
  441. X  other programs), even if such holder or third party has been advised
  442. X  of the possibility of such damages.
  443. X
  444. X  (4) Object code produced by a compiler from this code may be 
  445. X  incorporated into commercial products without being subject to 
  446. X  restrictions (1)(a) or (1)(b).
  447. X
  448. X*/
  449. X
  450. X#include <stdio.h>
  451. X#include <malloc.h>
  452. X#include <memory.h>
  453. X#include <string.h>
  454. X#include <assert.h>
  455. X
  456. X#include "sorting.h"
  457. X
  458. Xint bubble_sort(base, nelem, width, cmpr_func)
  459. X  char * base;
  460. X  int nelem;
  461. X  int width;
  462. X  int (*cmpr_func)();
  463. X{
  464. X  int top = nelem - 1;
  465. X  int bottom = 0;
  466. X  int i, did_swap = 1;
  467. X  char * temp;
  468. X
  469. X  temp = malloc(width);
  470. X  assert(temp != NULL);
  471. X
  472. X  while (top > bottom)
  473. X  {
  474. X    did_swap = 0;
  475. X    for (i = bottom; i < top; i++)
  476. X      if ((*cmpr_func)(base+i*width, base+(i+1)*width) > 0)
  477. X      {
  478. X        memcpy(temp, base+i*width, width);
  479. X        memcpy(base+i*width, base+(i+1)*width, width);
  480. X        memcpy(base+(i+1)*width, temp, width);
  481. X        did_swap = 1;
  482. X      }
  483. X
  484. X    if (!did_swap) break;
  485. X    top--;
  486. X
  487. X    did_swap = 0;
  488. X    for (i = top - 1; i >= bottom; i--)
  489. X      if ((*cmpr_func)(base+i*width, base+(i+1)*width) > 0)
  490. X      {
  491. X        memcpy(temp, base+i*width, width);
  492. X        memcpy(base+i*width, base+(i+1)*width, width);
  493. X        memcpy(base+(i+1)*width, temp, width);
  494. X        did_swap = 1;
  495. X      }
  496. X
  497. X    if (!did_swap) break;
  498. X    bottom ++;
  499. X  }
  500. X
  501. X  free(temp);
  502. X  return 0;
  503. X
  504. X}
  505. END_OF_FILE
  506.   if test 3389 -ne `wc -c <'bubble_sort.c'`; then
  507.     echo shar: \"'bubble_sort.c'\" unpacked with wrong size!
  508.   fi
  509.   # end of 'bubble_sort.c'
  510. fi
  511. if test -f 'insert_sort.c' -a "${1}" != "-c" ; then 
  512.   echo shar: Will not clobber existing file \"'insert_sort.c'\"
  513. else
  514.   echo shar: Extracting \"'insert_sort.c'\" \(3298 characters\)
  515.   sed "s/^X//" >'insert_sort.c' <<'END_OF_FILE'
  516. X
  517. X/* 
  518. X
  519. XNAME
  520. X  insertion sort 
  521. X
  522. XDESCRIPTION
  523. X
  524. X  Sorts by inserting each successive record into its proper place in
  525. X  the preceeding, already sorted records.   Perform a binary search on
  526. X  the preceeding records to find where to insert the current record,
  527. X  then shift all the records over to make room in the right place.
  528. X
  529. XAUTHORSHIP
  530. X
  531. X  This is properly called the binary insertion sort and credit is
  532. X  given for first publication to John Mauchly, 1946.
  533. X
  534. XREFERENCES
  535. X
  536. X  Knuth, Art of Computer Programming Vol 3, page 83.
  537. X
  538. XCOMPLEXITY
  539. X
  540. X  O(n log n) comparisons 
  541. X  O(0.25 * n^2) memory operations
  542. X  Iterative.
  543. X
  544. XCOPYRIGHT
  545. X
  546. X  Copyright 1992 Michael Lee.
  547. X
  548. X  (1) Permission to use, copy, distribute, and make changes is granted
  549. X  providing that (a) that you do so without charging anyone a fee and
  550. X  (b) this copyright notice must be included verbatim in all copies and 
  551. X  distributions.  
  552. X
  553. X  (2) There is no warranty for this program, to the extent permitted by
  554. X  applicable law.  This program is provided "AS IS" without warranty of
  555. X  any kind, either expressed or implied, including, but not limited to,
  556. X  the implied warranties of merchantability and fitness for a particular 
  557. X  purpose.  The entire risk as to the quality and performance of this 
  558. X  program is with the user.  Should this program prove defective, the 
  559. X  user assumes the cost of all necessary servicing, repair or correction.
  560. X
  561. X  (3) In no event unless required by applicable law will the copyright
  562. X  holder be liable to the user for damages, including any general,
  563. X  special, incidental or consequential damages arising out of the use
  564. X  or inability to use this program (including but not limited to loss of
  565. X  data or data being rendered inaccurate or losses sustained by the
  566. X  user or third parties or a failure of this program to operate with any
  567. X  other programs), even if such holder or third party has been advised
  568. X  of the possibility of such damages.
  569. X
  570. X  (4) Object code produced by a compiler from this code may be 
  571. X  incorporated into commercial products without being subject to 
  572. X  restrictions (1)(a) or (1)(b).
  573. X
  574. X*/
  575. X
  576. X#include <stdio.h>
  577. X#include <malloc.h>
  578. X#include <memory.h>
  579. X#include <string.h>
  580. X#include <assert.h>
  581. X
  582. X#include "sorting.h"
  583. X
  584. Xint insertion_sort(base, nelem, width, cmpr_func)
  585. X  char * base;
  586. X  int nelem;
  587. X  int width;
  588. X  int (*cmpr_func)();
  589. X{
  590. X  int i, top, middle, bottom;
  591. X  int found, test;
  592. X  char * temp;
  593. X
  594. X  temp = malloc(width);
  595. X  assert(temp != NULL);
  596. X
  597. X  for (i = 1; i < nelem; i++)
  598. X  {
  599. X    /* binary search looking for place between base[0] and base[i-1] where
  600. X       you can insert base[i] into in order. */
  601. X
  602. X    bottom = 0;
  603. X    top = i-1;
  604. X    middle = 0;
  605. X    found = 0;
  606. X
  607. X    while (top >= bottom && ! found)
  608. X    {
  609. X      middle = (top+bottom) / 2;
  610. X      test = (*cmpr_func)(base+middle*width, base+i*width);
  611. X
  612. X      if (test > 0)
  613. X        top = middle - 1;
  614. X      else if (test < 0)
  615. X      {
  616. X        middle ++;
  617. X        bottom = middle;
  618. X      }
  619. X      else
  620. X      {
  621. X        middle ++;
  622. X        found = 1;
  623. X      }
  624. X    }
  625. X
  626. X    /* make room at base[middle] for base[i] */
  627. X
  628. X    if (i != middle)
  629. X    {
  630. X      memcpy(temp, base+i*width, width);
  631. X      memmove(base+middle*width+width, base+middle*width, (i-middle)*width);
  632. X      memcpy(base+middle*width, temp, width);
  633. X    }
  634. X  }
  635. X
  636. X  if (temp != NULL) free(temp);
  637. X
  638. X  return 0;
  639. X
  640. X}
  641. END_OF_FILE
  642.   if test 3298 -ne `wc -c <'insert_sort.c'`; then
  643.     echo shar: \"'insert_sort.c'\" unpacked with wrong size!
  644.   fi
  645.   # end of 'insert_sort.c'
  646. fi
  647. if test -f 'shell_sort.c' -a "${1}" != "-c" ; then 
  648.   echo shar: Will not clobber existing file \"'shell_sort.c'\"
  649. else
  650.   echo shar: Extracting \"'shell_sort.c'\" \(2668 characters\)
  651.   sed "s/^X//" >'shell_sort.c' <<'END_OF_FILE'
  652. X
  653. X/*
  654. X
  655. XNAME
  656. X  shell sort 
  657. X
  658. XDESCRIPTION
  659. X
  660. X  An exhange sort algorithm which exchanges over larger distances
  661. X  than bubble sort, thus reducing the number of exchanges needed.
  662. X
  663. XAUTHORSHIP
  664. X
  665. X  D. L. Shell
  666. X
  667. XREFERENCES
  668. X
  669. X  D. L. Shell, CACM 2, July 1959
  670. X  Kernighan & Ritchie, C Programming Language, Second Edition, pg 62
  671. X  Knuth, Art of Computer Programming Vol 3, pg 84-95
  672. X
  673. XCOMPLEXITY
  674. X
  675. X  I'm not exactly sure, but I think it's O(N^1.5) 
  676. X  Iterative.
  677. X
  678. XCOPYRIGHT
  679. X
  680. X  Copyright 1992 Michael Lee.
  681. X
  682. X  (1) Permission to use, copy, distribute, and make changes is granted
  683. X  providing that (a) that you do so without charging anyone a fee and
  684. X  (b) this copyright notice must be included verbatim in all copies and 
  685. X  distributions.  
  686. X
  687. X  (2) There is no warranty for this program, to the extent permitted by
  688. X  applicable law.  This program is provided "AS IS" without warranty of
  689. X  any kind, either expressed or implied, including, but not limited to,
  690. X  the implied warranties of merchantability and fitness for a particular 
  691. X  purpose.  The entire risk as to the quality and performance of this 
  692. X  program is with the user.  Should this program prove defective, the 
  693. X  user assumes the cost of all necessary servicing, repair or correction.
  694. X
  695. X  (3) In no event unless required by applicable law will the copyright
  696. X  holder be liable to the user for damages, including any general,
  697. X  special, incidental or consequential damages arising out of the use
  698. X  or inability to use this program (including but not limited to loss of
  699. X  data or data being rendered inaccurate or losses sustained by the
  700. X  user or third parties or a failure of this program to operate with any
  701. X  other programs), even if such holder or third party has been advised
  702. X  of the possibility of such damages.
  703. X
  704. X  (4) Object code produced by a compiler from this code may be 
  705. X  incorporated into commercial products without being subject to 
  706. X  restrictions (1)(a) or (1)(b).
  707. X
  708. X*/
  709. X
  710. X#include <stdio.h>
  711. X#include <malloc.h>
  712. X#include <memory.h>
  713. X#include <string.h>
  714. X#include <assert.h>
  715. X
  716. X#include "sorting.h"
  717. X
  718. Xint shell_sort(base, nelem, width, cmpr_func)
  719. X  char * base;
  720. X  int nelem;
  721. X  int width;
  722. X  int (*cmpr_func)();
  723. X{
  724. X  int gap, i, j;
  725. X  char * temp;
  726. X
  727. X  temp = malloc(width);
  728. X  assert(temp != NULL);
  729. X
  730. X  for (gap = nelem / 2; gap > 0; gap /= 2)
  731. X  {
  732. X    for (i = gap; i < nelem; i++)
  733. X    {
  734. X      for (j = i-gap; 
  735. X           j >=0 && (*cmpr_func)(base+j*width, base+(j+gap)*width) > 0;
  736. X           j -= gap)
  737. X      {
  738. X        memcpy(temp, base+j*width, width);
  739. X        memcpy(base+j*width, base+(j+gap)*width, width);
  740. X        memcpy(base+(j+gap)*width, temp, width);
  741. X      }
  742. X    }
  743. X  }
  744. X
  745. X  if (temp != NULL) free(temp);
  746. X
  747. X  return 0;
  748. X
  749. X}
  750. END_OF_FILE
  751.   if test 2668 -ne `wc -c <'shell_sort.c'`; then
  752.     echo shar: \"'shell_sort.c'\" unpacked with wrong size!
  753.   fi
  754.   # end of 'shell_sort.c'
  755. fi
  756. if test -f 'sorting.h' -a "${1}" != "-c" ; then 
  757.   echo shar: Will not clobber existing file \"'sorting.h'\"
  758. else
  759.   echo shar: Extracting \"'sorting.h'\" \(797 characters\)
  760.   sed "s/^X//" >'sorting.h' <<'END_OF_FILE'
  761. X
  762. X/* use the next line if your C library does not have memmove */
  763. X
  764. X#define memmove(a, b, n) bcopy(b, a, n)
  765. X
  766. X/* use the next line if your C library does not have memcpy */
  767. X
  768. X/* #define memcpy(to, from, n) bcopy(from, to, n)  */
  769. X
  770. X
  771. X#define MAYBE_THRESHOLD 8
  772. Xextern int _maybe_sorted;
  773. X
  774. Xextern void exit();
  775. Xextern int qsort();
  776. X
  777. Xextern int merge_sort();
  778. Xextern int heap_sort();
  779. Xextern int bubble_sort();
  780. Xextern int quick_sort();
  781. Xextern int shell_sort();
  782. Xextern int insertion_sort();
  783. Xextern int bogo_sort();
  784. X
  785. X/* It's more important that these represent common sizes on your
  786. X   machine than that they represent a certain number of bytes. */
  787. X
  788. Xtypedef short               chunk2;
  789. Xtypedef long                chunk4;
  790. Xtypedef double              chunk8;
  791. X
  792. X#define FLOGGER_VERSION 0
  793. X#define FLOGGER_PATCHLEVEL 0
  794. X
  795. END_OF_FILE
  796.   if test 797 -ne `wc -c <'sorting.h'`; then
  797.     echo shar: \"'sorting.h'\" unpacked with wrong size!
  798.   fi
  799.   # end of 'sorting.h'
  800. fi
  801. echo shar: End of archive 2 \(of 2\).
  802. cp /dev/null ark2isdone
  803. MISSING=""
  804. for I in 1 2 ; do
  805.     if test ! -f ark${I}isdone ; then
  806.     MISSING="${MISSING} ${I}"
  807.     fi
  808. done
  809. if test "${MISSING}" = "" ; then
  810.     echo You have unpacked both archives.
  811.     rm -f ark[1-9]isdone
  812. else
  813.     echo You still must unpack the following archives:
  814.     echo "        " ${MISSING}
  815. fi
  816. exit 0
  817. exit 0 # Just in case...
  818.