關於TC程設問題..

我想請問一下要怎樣打才能讓他自動(排大小或是找最大最小)

例如:輸入5個數據給他然後他就會排大小或是找最大最小

(我是初學者)

2 Answers

Rating
  • 2 decades ago
    Favorite Answer

    隨便一本程式書都有交排序

    下面是非遞迴的合併排序法

    #include <stdio.h>

    #include <math.h>

    void merge();

    void main()

    {

    int a[100],ARR_NUM,e,count,g,c,i;

    printf("請輸入各數:");

    scanf("%d",&ARR_NUM);

    g=ARR_NUM;

    printf("請輸入數字:");

    for(i=0;i < ARR_NUM;i++)

    scanf("%d",&a[i]);

    printf("\n");

    count=g/2+g%2; /*計算合併次數*/

    c=log(g)/log(2);

    e=pow(2,c+1);

    for(i=ARR_NUM;e>i;i++)

    a[i]=65535;

    merge(a,count,ARR_NUM);

    getch();

    }

    void merge(int a[],int count1,int N)

    {

    int r=2,p=0,m=1,mergeL,mergeR,L[100],R[100];

    int i,j,k,x=2,z=1;

    while(count1>=1)

    {

    while (z<=count1)

    {

    mergeL=m-p;

    mergeR=r-m;

    L[mergeL]=R[mergeR]=65535;

    for(j=0;j<mergeR;j++)

    R[j]=a[m+j];

    for(i=0;i < mergeL;i++)

    L[i]=a[p+i];

    i=j=0;

    for (k=p;k < r;k++)

    {

    if (L[i]<=R[j]) /*合併*/

    {

    a[k]=L[i];

    i++;

    }

    else

    {

    a[k]=R[j];

    j++;

    }

    }

    p=p+x ;

    r=r+x;

    m=m+x;

    z++;

    }

    printf("\n排序後 ( after sorting ):\n");

    for(i=0;i < N;i++)

    printf(" %d",a[i]);

    printf("\n");

    m=x;

    p=0;

    z=1;

    x=x*2;

    if (count1/2!=0)

    count1=count1/2+count1%2;

    else

    count1=count1/2;

    r=x;

    }

    }

  • 2 decades ago

    氣泡排序法

Still have questions? Get your answers by asking now.