home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume38 / linkedlist / part02 / sorttest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  7.2 KB  |  342 lines

  1. /* Sort torture test */
  2.  
  3. #ifdef UNIX
  4. #include    <malloc.h>
  5. #endif
  6.  
  7. #ifdef    AMIGA
  8. #include    <stdlib.h>
  9. #endif
  10.  
  11. #ifdef MSDOS
  12. #include    <malloc.h>
  13. #endif
  14.  
  15. #include    <stdio.h>
  16. #include    <string.h>
  17. #include    <time.h>
  18. #include    "list.h"
  19.  
  20. typedef struct catalog {
  21.     char        number[35];
  22.     char        title[40];
  23.     char        author[35];
  24.     int        date;
  25. } catalog;
  26.  
  27. #ifdef ANSI
  28. static int    compare_author(catalog *a, catalog *b);
  29. static int    compare_title(catalog *a, catalog *b, int order);
  30. static void    load_it(int id, int num);
  31. static void    print_some(int id, int num);
  32. static time_t    what_time(void);
  33. static void    diff_time(time_t a, time_t b);
  34. static void    prRap(int code, catalog *rpprt);
  35. #else
  36. static int    compare_author();
  37. static int    compare_title();
  38. static void    load_it();
  39. static void    print_some();
  40. static time_t    what_time();
  41. static void    diff_time();
  42. static void    prRap();
  43. #endif
  44.  
  45. static int
  46. compare_author(a, b)
  47. catalog *a, *b;
  48. {
  49.     int    k;
  50.  
  51.     k = strcmp(a->author, b->author);
  52.  
  53.     if (k == 0)
  54.         return(lSAME);
  55.     else if (k > 0)
  56.         return(l2LT1);
  57.     else if (k < 0)
  58.         return(l1LT2);
  59. }
  60.  
  61. static int
  62. compare_title(a, b, order)
  63. catalog *a, *b;
  64. int order;
  65. {
  66.     int    k;
  67.  
  68.     k = strcmp(a->title, b->title);
  69.  
  70.     if (k == 0)
  71.         return(lSAME);
  72.     else if (k > 0)
  73.         return(l2LT1);
  74.     else if (k < 0)
  75.         return(l1LT2);
  76. }
  77.  
  78. static void
  79. load_it(id, num)
  80. int id;
  81. int num;
  82. {
  83.     int    loop, size = sizeof(catalog), code;
  84.     char    c1, c2, c3, c4, c5, c6, c7, c8;
  85.     catalog    rap;
  86.  
  87.     c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';
  88.     c7--;
  89.     for (loop=1; loop<=num; loop++) {
  90.         c7++;
  91.  
  92.         if (c7>'Z') {c7 = 'A'; c6++;}
  93.         if (c6>'Z') {c6 = 'A'; c5++;}
  94.         if (c5>'Z') {c5 = 'A'; c4++;}
  95.         if (c4>'Z') {c4 = 'A'; c3++;}
  96.         if (c3>'Z') {c3 = 'A'; c2++;}
  97.         if (c2>'Z') {c2 = 'A'; c1++;}
  98.         if (c1>'Z') {c1 = 'A'; c1 = c2 = c3 = c4 = c5 = c6 = c7 = 'A';}
  99.  
  100.         sprintf(rap.number,"B-90-%d", loop);
  101.         sprintf(rap.title, "Book %d", loop);
  102.         sprintf(rap.author,"%c%c%c%c%c%c%c%d",
  103.             c1, c2, c3, c4, c5, c6, c7, loop);
  104.         rap.date = 890129;
  105.         code = lInsNode(id, lLAST, &rap, size, loop);
  106.     }
  107.     /* printf("Linked List created\n"); */
  108. }
  109.  
  110. static void
  111. print_some(id, num)
  112. int id;
  113. int num;
  114. {
  115.     int    size = sizeof(catalog), loop;
  116.     int    code;
  117.     catalog    rap;
  118.  
  119.     code = lGetNode(id, lFIRST, &rap, size);
  120.     prRap(code, &rap);
  121.  
  122.     for (loop=2; loop<=num; loop++) {
  123.         code = lGetNode(id, lNEXT, &rap, size);
  124.         prRap(code, &rap);
  125.     }
  126. }
  127.  
  128. static time_t
  129. what_time()
  130. {
  131.     return((time_t)time(NULL));
  132. }
  133.  
  134. static void
  135. diff_time(a, b)
  136. time_t a, b;
  137. {
  138.     struct tm    *tt;
  139.     char        buf1[101], buf2[101];
  140.     int        hour[2], min[2], sec[2];
  141.     time_t        x;
  142.  
  143.     tt = (struct tm *)malloc(sizeof(struct tm *) + 1);
  144.  
  145.     tt = (struct tm *)localtime(&a);
  146.     strftime(buf1, 100, "%H %M %S", tt);
  147.  
  148.     tt = (struct tm *)localtime(&b);
  149.     strftime(buf2, 100, "%H %M %S", tt);
  150.  
  151.     sscanf(buf1,"%d %d %d", &hour[0], &min[0], &sec[0]);
  152.     sscanf(buf2,"%d %d %d", &hour[1], &min[1], &sec[1]);
  153.  
  154.     printf("Hours = %d  Mins = %d  Secs = %d\n",
  155.     (hour[1] - hour[0] > 0)?(hour[1] - hour[0]) : (hour[0] - hour[1]),
  156.     (min[1] - min[0] > 0)?(min[1] - min[0]) : (min[0] - min[1]),
  157.     (sec[1] - sec[0] > 0)?(sec[1] - sec[0]) : (sec[0] - sec[1]));
  158. }
  159.  
  160. int
  161. main()
  162. {
  163.     int    size = sizeof(catalog);
  164.     int    id, code;
  165.     time_t    start, finish;
  166.     catalog    rap;
  167.  
  168.     /* Quick sort */
  169.     /* start testing sorting efficiency */
  170.     id = lDef(lSINGLY, lCHAIN);
  171.     load_it(id, 3000);
  172.  
  173.     printf("Untouched list\n");
  174.     print_some(id, 10);
  175.  
  176.     printf("lQuickSort DESCENDING by TITLE : ");
  177.     start = what_time();
  178.     code = lSort(id, lDESCENDING, lQUICK, compare_title);
  179.     finish = what_time();
  180.     diff_time(start, finish);
  181.     print_some(id, 10);
  182.  
  183.     printf("lQuickSort ASCENDING by TITLE : ");
  184.     start = what_time();
  185.     code = lSort(id, lASCENDING, lQUICK, compare_title);
  186.     finish = what_time();
  187.     diff_time(start, finish);
  188.     print_some(id, 10);
  189.  
  190.     printf("lQuickSort DESCENDING by AUTHOR : ");
  191.     start = what_time();
  192.     code = lSort(id, lDESCENDING, lQUICK, compare_author);
  193.     finish = what_time();
  194.     diff_time(start, finish);
  195.     print_some(id, 10);
  196.  
  197.     printf("lQuickSort ASCENDING by AUTHOR : ");
  198.     start = what_time();
  199.     code = lSort(id, lASCENDING, lQUICK, compare_author);
  200.     finish = what_time();
  201.     diff_time(start, finish);
  202.     print_some(id, 10);
  203.  
  204.     code = lDelAll();
  205.  
  206.     /* Heap sort */
  207.     /* start testing sorting efficiency */
  208.     id = lDef(lSINGLY, lCHAIN);
  209.     load_it(id, 3000);
  210.  
  211.     printf("Untouched list\n");
  212.     print_some(id, 10);
  213.  
  214.     printf("lHeapSort DESCENDING by TITLE : ");
  215.     start = what_time();
  216.     code = lSort(id, lDESCENDING, lHEAP, compare_title);
  217.     finish = what_time();
  218.     diff_time(start, finish);
  219.     print_some(id, 10);
  220.  
  221.     printf("lHeapSort ASCENDING by TITLE : ");
  222.     start = what_time();
  223.     code = lSort(id, lASCENDING, lHEAP, compare_title);
  224.     finish = what_time();
  225.     diff_time(start, finish);
  226.     print_some(id, 10);
  227.  
  228.     printf("lHeapSort ASCENDING by AUTHOR : ");
  229.     start = what_time();
  230.     code = lSort(id, lASCENDING, lHEAP, compare_author);
  231.     finish = what_time();
  232.     diff_time(start, finish);
  233.     print_some(id, 10);
  234.  
  235.     code = lDelAll();
  236.  
  237.     /* Selection sort */
  238.     /* start testing sorting efficiency */
  239.     id = lDef(lSINGLY, lCHAIN);
  240.     load_it(id, 3000);
  241.  
  242.     printf("Untouched list\n");
  243.     print_some(id, 10);
  244.  
  245.     printf("lSelectionSort DESCENDING by TITLE : ");
  246.     start = what_time();
  247.     code = lSort(id, lDESCENDING, lSELECTION, compare_title);
  248.     finish = what_time();
  249.     diff_time(start, finish);
  250.     print_some(id, 10);
  251.  
  252.     printf("lSelectionSort ASCENDING by TITLE : ");
  253.     start = what_time();
  254.     code = lSort(id, lASCENDING, lSELECTION, compare_title);
  255.     finish = what_time();
  256.     diff_time(start, finish);
  257.     print_some(id, 10);
  258.  
  259.     printf("lSelectionSort ASCENDING by AUTHOR : ");
  260.     start = what_time();
  261.     code = lSort(id, lASCENDING, lSELECTION, compare_author);
  262.     finish = what_time();
  263.     diff_time(start, finish);
  264.     print_some(id, 10);
  265.  
  266.     code = lDelAll();
  267.  
  268.     /* insertion sort*/
  269.     /* start testing sorting efficiency */
  270.     id = lDef(lSINGLY, lCHAIN);
  271.     load_it(id, 3000);
  272.  
  273.     printf("Untouched list\n");
  274.     print_some(id, 10);
  275.  
  276.     printf("lInsertSort DESCENDING by TITLE : ");
  277.     start = what_time();
  278.     code = lSort(id, lDESCENDING, lINSERT, compare_title);
  279.     finish = what_time();
  280.     diff_time(start, finish);
  281.     print_some(id, 10);
  282.  
  283.     printf("lInsertSort ASCENDING by TITLE : ");
  284.     start = what_time();
  285.     code = lSort(id, lASCENDING, lINSERT, compare_title);
  286.     finish = what_time();
  287.     diff_time(start, finish);
  288.     print_some(id, 10);
  289.  
  290.     printf("lInsertSort ASCENDING by AUTHOR : ");
  291.     start = what_time();
  292.     code = lSort(id, lASCENDING, lINSERT, compare_author);
  293.     finish = what_time();
  294.     diff_time(start, finish);
  295.     print_some(id, 10);
  296.  
  297.     code = lDelAll();
  298.  
  299.     /* start testing sorting efficiency */
  300.     id = lDef(lSINGLY, lCHAIN);
  301.     load_it(id, 3000);
  302.  
  303.     printf("Untouched list\n");
  304.     print_some(id, 10);
  305.  
  306.     /* Bubble sort */
  307.     printf("lBubbleSort DESCENDING by TITLE : ");
  308.     start = what_time();
  309.     code = lSort(id, lDESCENDING, lBUBBLE, compare_title);
  310.     finish = what_time();
  311.     diff_time(start, finish);
  312.     print_some(id, 10);
  313.  
  314.     printf("lBubbleSort ASCENDING by TITLE : ");
  315.     start = what_time();
  316.     code = lSort(id, lASCENDING, lBUBBLE, compare_title);
  317.     finish = what_time();
  318.     diff_time(start, finish);
  319.     print_some(id, 10);
  320.  
  321.     printf("lBubbleSort ASCENDING by AUTHOR : ");
  322.     start = what_time();
  323.     code = lSort(id, lASCENDING, lBUBBLE, compare_author);
  324.     finish = what_time();
  325.     diff_time(start, finish);
  326.     print_some(id, 10);
  327.  
  328.     code = lDelAll();
  329. }
  330.  
  331. static void
  332. prRap(code, rpprt)
  333. int    code;
  334. catalog    *rpprt;
  335. {
  336.     if (code >= 0)
  337.         printf("catalog :'%s'\t'%s'\t'%s'\t'%d'\n", rpprt->number,
  338.             rpprt->title, rpprt->author, rpprt->date);
  339.     else
  340.         printf("Return code = %d\n", code);
  341. }
  342.