11_3.C
上传用户:wyn840322
上传日期:2007-01-13
资源大小:294k
文件大小:5k
源码类别:

数据结构

开发平台:

C/C++

  1. /* ======================================== */
  2. /*    程式实例: 11_3.c                      */
  3. /*    直接合并排序                          */
  4. /* ======================================== */
  5. #include <stdio.h>
  6. #define LEN  8                    /* 最大元素个数         */
  7. /* ---------------------------------------- */
  8. /*  直接合并排序                            */
  9. /* ---------------------------------------- */
  10. void merge_sort(FILE *merge,FILE *sort1,FILE *sort2,int len)
  11. {
  12.    int s1,s2;                     /* 资料计数             */
  13.    int i,j,k;
  14.    char c,c1,c2;
  15.    for ( i = 1; i < len; i *= 2 )
  16.    {
  17.       /* 分割档案 */
  18.       for ( j = 0; j < len / 2; j++ ) /* 分到档案一       */
  19.       {
  20.          c = getc(merge);
  21.          putc(c,sort1);
  22.       }
  23.       for ( ; j < len; j++ )      /* 分到档案二           */
  24.       {
  25.          c = getc(merge);
  26.          putc(c,sort2);
  27.       }
  28.       rewind(merge);              /* 重设档案指标         */
  29.       rewind(sort1);
  30.       rewind(sort2);
  31.       for ( k = 0; k < len / 2; k += i )  /* 合并回路     */
  32.       {
  33.          c1 = getc(sort1);        /* 读取第一分割档       */
  34.          c2 = getc(sort2);        /* 读取第二分割档       */
  35.          s1 = s2 = 0;             /* 设定计数初值         */
  36.          while ( 1 )
  37.          {
  38.             if ( c1 < c2 )        /* 比较两个值           */
  39.             {
  40.                /* 第一分割档小, 存入主档 */
  41.                putc(c1,merge);
  42.                s1++;              /* 计数加一             */
  43.                if ( s1 < i )      /* 是否未读完           */
  44.                   c1 = getc(sort1);   /* 读取第一分割档   */
  45.                else
  46.                   break;          /* 跳出回路             */
  47.             }
  48.             else
  49.             {
  50.                /* 第二分割档小, 存入主档 */
  51.                putc(c2,merge);
  52.                s2++;              /* 计数加一             */
  53.                if ( s2 < i )      /* 是否未读完           */
  54.                   c2 = getc(sort2);   /* 读取第一分割档   */
  55.                else
  56.                   break;          /* 跳出回路             */
  57.             }
  58.          }
  59.          /* 第一分割档是否是最後一笔 */
  60.          if ( s1 < i )            /* 处理最後一笔         */
  61.          {
  62.             putc(c1,merge);       /* 存入主档             */
  63.             s1++;                 /* 计数加一             */
  64.          }
  65.          /* 第二分割档是否是最後一笔 */
  66.          if ( s2 < i )            /* 处理最後一笔         */
  67.          {
  68.             putc(c2,merge);       /* 存入主档             */
  69.             s2++;                 /* 计数加一             */
  70.          }
  71.          /* 第一分割档 */
  72.          while ( s1 < i )         /* 取出剩下的资料       */
  73.          {
  74.             c = getc(sort1);      /* 读取第一分割档       */
  75.             putc(c,merge);        /* 存入主档             */
  76.             s1++;                 /* 计数加一             */
  77.          }
  78.          /* 第二分割档 */
  79.          while ( s2 < i )         /* 取出剩下的资料       */
  80.          {
  81.             c = getc(sort2);      /* 读取第二分割档       */
  82.             putc(c,merge);        /* 存入主档             */
  83.             s2++;                 /* 计数加一             */
  84.          }
  85.       }
  86.       rewind(merge);              /* 重设档案指标         */
  87.       rewind(sort1);
  88.       rewind(sort2);
  89.    }
  90. }
  91. /* ---------------------------------------- */
  92. /*  主程式: 读取两个档案内容, 接着用直接合  */
  93. /*  并排序法来排序.                         */
  94. /* ---------------------------------------- */
  95. void main()
  96. {
  97.    FILE *data;                    /* 资料档               */
  98.    FILE *fp;                      /* 主档指标             */
  99.    FILE *fp1;                     /* 第一分割档案指标     */
  100.    FILE *fp2;                     /* 第二分割档案指标     */
  101.    char c;
  102.    int j;
  103.    data = fopen("data.txt","r");  /* 开启资料档           */
  104.    if ( data == NULL )
  105.    {
  106.       printf("资料档档开启错误! n");
  107.       exit(0);                    /* 离开                 */
  108.    }
  109.    fp = fopen("result.txt","r+"); /* 开启主档             */
  110.    if ( fp == NULL )
  111.       printf("主档开启错误! n");
  112.    else
  113.    {
  114.       for ( j = 0; j < LEN; j++ ) /* 拷贝资料到主档       */
  115.       {
  116.          c = getc(data);          /* 读取资料             */
  117.          putc(c,fp);              /* 存入资料             */
  118.       }
  119.       fclose(data);               /* 关资料档             */
  120.       rewind(fp);                 /* 重设档案指标         */
  121.       fp1 = fopen("split1.txt","r+"); /* 开启第一分割档案 */
  122.       if ( fp1 == NULL )
  123.          printf("第一分割档开启错误! n");
  124.       else
  125.       {
  126.          fp2 = fopen("split2.txt","r+"); /*开启第二分割档案*/
  127.          if ( fp2 == NULL )
  128.             printf("第二分割档开启错误! n");
  129.          else
  130.          {
  131.             printf("正在处理数据,请稍等. . . n");
  132.             merge_sort(fp,fp1,fp2,LEN);  /* 直接合并排序法 */
  133.             printf("数据处理完成! n");
  134.             fclose(fp);                  /* 关档           */
  135.             fclose(fp1);
  136.             fclose(fp2);
  137.          }
  138.       }
  139.    }
  140. }