歸併排序的遞迴演算法與非遞迴

來源:互聯網
上載者:User

標籤:sort   ret   src   分享   遞迴   歸併   技術   print   scan   

昨天晚上搞了好久,總有那麼幾個資料有點錯誤,原來數組是從1開始存的,沒看清楚...

 1 /* 2 請設計歸併排序演算法函數void mergeSort(int a[],int n),對a[1]..a[n]進行升序排序。 3 並測試在不同資料規模下的排序效率。 4 */ 5 #include "Arrayio.h" 6 #define N 10000     /*N為資料量大小,因data1.txt中只有50萬個數,所以自行設定N值時需讓N<=500000*/ 7  8 /*請將本函數補充完整,並進行測試*/ 9 10 void merge(int a[], int u, int m, int v)11 { /*將有序段a[u..m],a[m+1..v]歸併到a[u..v]*/12     int i, j, k;13     int t[N+1];14     i = u, j = m + 1, k = u;15     while (i <= m && j <= v)16     {17         if (a[i] <= a[j])18         {19             t[k++] = a[i++];20         }21         else22         {23             t[k++] = a[j++];24         }25     }26     while (i <= m)27         t[k++] = a[i++];28     while (j <= v)29         t[k++] = a[j++];30     for (i = u; i <= v; i++)31         a[i] = t[i];32 }33 34 35 /*----一趟歸併------*/36 void mergepass(int a[], int n, int len)37 { /*對a[1..n]進行長度為len的一趟並歸*/38     int i = 1;39     while (i+2*len-1 <= n)40     {41         merge(a,i, i + len - 1, i + 2 * len - 1);42         i = i + 2 * len;43     }44     if (i+len-1<n)45         merge(a,i, i +len-1 , n);46 }47 48 /*----歸併排序------*/49 void mergeSort(int a[], int n)50 {51     int len = 1;52     while (len<=n)53     {54         mergepass(a, n, len);55         len = 2 * len;56     }57 }58 59 /*歸併排序的遞迴實現*/60 void mergeSortdc(int a[], int low, int high)61 {62     int mid;63     if (low<high)64     {65         mid = (low + high) / 2;66         mergeSortdc(a,low, mid);67         mergeSortdc(a,mid + 1, high);68         merge(a, low,mid, high);69     }70 }71 72 int  main()73 {74   int a[N+1],n;                     /*資料存放區在a[1]...a[N]中*/75   printf("資料初始化...\n");76   n=readData(a,N,"data1.txt");      /*從data1.txt中讀入N個整數存入數組a,n為實際讀入的資料個數*/77   printf("%d個資料排序中...\n",n);78   mergeSort(a,n);                   /*mergeSortdc(a,1,n);*/79   saveData(a,n,"D:\\out.txt");          /*排序結果存放在out.txt檔案中*/80   printf("排序結束,排序結果儲存在out.txt檔案中。\n");81   return 0;82 }

 

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define MAX 500000 5  6 /*從檔案中讀入資料存入數組a*/ 7 int readData(int a[], int n,char *f )  /*函數返回成功讀入的資料個數*/ 8 { 9     FILE *fp;10     int i;11     fp=fopen(f,"r");12     if (fp==NULL)   return 0;13     else14     {15         for (i=0;i<n && !feof(fp);i++)16             fscanf(fp,"%d",&a[i]);17         fclose(fp);18         return i;19     }20 }21 22 /*存檔函數*/23 void saveData(int a[],int n, char *f )24 {25     FILE *fp;26     int i;27     fp=fopen(f,"w");28     if (fp==NULL)   printf("檔案建立失敗!");29     else30     {31         for (i=0;i<n;i++)32             {   if (i%10==0) fprintf(fp,"%c",‘\n‘);33                 fprintf(fp,"%8d",a[i]);34             }35         fclose(fp);36     }37 }38 39 40 /*輸出長度為n的整型數組*/41 void output(int a[],int n)42 {  int i;43    printf("\n數組的內容是:\n");44    for (i=0;i<n;i++)45      { if (i%10==0) printf("\n");46        printf("%7d",a[i]);47      }48   printf("\n");49 }

 

歸併排序的遞迴演算法與非遞迴

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.