QSortTest.m
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:2k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. // Swarm library. Copyright (C) 1999, 2000 Swarm Development Group.
  2. // This library is distributed without any warranty; without even the
  3. // implied warranty of merchantability or fitness for a particular purpose.
  4. // See file LICENSE for details and terms of copying.
  5. #import <simtools.h>
  6. #import <objectbase.h>
  7. #import <collections.h>
  8. #include <misc.h>
  9. #include <swarmconfig.h> // PTRINT
  10. int
  11. main (int argc, const char **argv) 
  12. {
  13.   id list;
  14.   id index;
  15.   id member;
  16.   int currMax, currMin;
  17.   initSwarmBatch (argc, argv);
  18.   // first, create a collection to be ordererd....
  19.   list = [List create: globalZone];
  20.   [list addLast: (id) 3];
  21.   [list addLast: (id) 10];
  22.   [list addLast: (id) 1];
  23.   [list addLast: (id) 12];
  24.   [list addLast: (id) 13];
  25.   [list addLast: (id) 389];
  26.   [list addLast: (id) 99];
  27.   // check list in unsorted order
  28.   printf ("unsorted list...n");
  29.   index = [list begin: scratchZone];
  30.   
  31.   while ((member = [index next])) 
  32.     printf (PTRINTFMT ", ", (PTRINT) member);
  33.   [index drop];
  34.   // reverse the list...
  35.   [QSort reverseOrderOf: list];
  36.   // check reversed order
  37.   printf ("nreversed list...n");
  38.   index = [list begin: scratchZone];
  39.   while ((member = [index next])) 
  40.     printf (PTRINTFMT ", ", (PTRINT) member);
  41.   [index drop];
  42.   // sort the list...
  43.   [QSort sortNumbersIn: list];
  44.   // check list in sorted order
  45.   currMax = 0;
  46.   printf ("nsorted (ascending) list...n");
  47.   index = [list begin: scratchZone];
  48.   while ((member = [index next])) 
  49.     {
  50.       if (currMax > (PTRINT) member)
  51.         {
  52.           fprintf(stderr, "list is not sorted in ascending ordern");
  53.           return 1;
  54.         }
  55.       currMax = (PTRINT) member;
  56.       printf (PTRINTFMT ", ", (PTRINT) member);
  57.     }
  58.   [index drop];
  59.   // now reverse list...
  60.   currMin = 999;  
  61.   [QSort reverseOrderOf: list];
  62.   
  63.   printf("nsorted and reversed list (descending)...n");
  64.   index = [list begin: scratchZone];
  65.   while ((member = [index next])) 
  66.     {
  67.       if (currMin < (PTRINT) member)
  68.         {
  69.           fprintf (stderr, "list is not sorted in descending ordern");
  70.           return 1;
  71.         }
  72.       currMin = (PTRINT) member;
  73.       printf (PTRINTFMT ", ", (PTRINT) member);
  74.     }
  75.   [index drop];
  76.   printf ("n");
  77.   return 0;
  78. }