顺序查找,折半查找(递归与非递归)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39360985/article/details/78795900

说明:

编译器:Xcode

语言:C

data2.txt为:

      1       2       3       4       5       6       7       8       9      10
      11      12      13      14      15      16      17      18      19      20
      21      22      23      24      25      26      27      28      29      30
      31      32      33      34      35      36      37      38      39      40
      41      42      43      44      45      46      47      48      49      50
      51      52      53      54      55      56      57      58      59      60
      61      62      63      64      65      66      67      68      69      70
      71      72      73      74      75      76      77      78      79      80
      81      82      83      84      85      86      87      88      89      90
      91      92      93      94      95      96      97      98      99     100
     101     102     103     104     105     106     107     108     109     110
     111     112     113     114     115     116     117     118     119     120
     121     122     123     124     125     126     127     128     129     130
     131     132     133     134     135     136     137     138     139     140
     141     142     143     144     145     146     147     148     149     150
     151     152     153     154     155     156     157     158     159     160
     161     162     163     164     165     166     167     168     169     170
     171     172     173     174     175     176     177     178     179     180
     181     182     183     184     185     186     187     188     189     190
     191     192     193     194     195     196     197     198     199     200
     201     202     203     204     205     206     207     208     209     210
     211     212     213     214     215     216     217     218     219     220
     221     222     223     224     225     226     227     228     229     230
     231     232     233     234     235     236     237     238     239     240
     241     242     243     244     245     246     247     248     249     250
     251     252     253     254     255     256     257     258     259     260
     261     262     263     264     265     266     267     268     269     270
     271     272     273     274     275     276     277     278     279     280
     281     282     283     284     285     286     287     288     289     290
     291     292     293     294     295     296     297     298     299     300



#include <stdio.h>

#include <stdlib.h>

#define N 10000


//从文件中读入数据存入数组

int readData(int a[],int n,char *f ) //函数返回成功读入的数据个数

{

    FILE *fp;

    int i;

    fp=fopen(f,"r");

    if (fp==NULL)

        return 0;

    else

    {

        for (i=0;i<n && !feof(fp);i++)

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

        

        fclose(fp);

        return i;

    }

}


int shunSearch(int a[],int n,int key)//顺序查找

{

    

    for(int i=0;i<n-1;i++)

    {

        if(a[i]==key)

            return i;

    }

    return 0;

}



int zheFeidiSearch(int a[],int n,int key)//折半查找非递归

{

    

    int low=0;

    int high=n-1;

    int mid;

    while(low<high)

    {

        mid=(low+high)/2;

        if(a[mid]==key)

            return mid;

        if(a[mid]>key)

            high=mid-1;

        if(a[mid]<key)

            low=mid+1;

    }

    return -1;

}



int zheDiSearch(int a[],int low,int high,int key)//折半查找递归

{

    

    int mid;

    if(low>high)

        return -1;

    else

    {

        mid=(low+high)/2;

        if(a[mid]==key)

            return mid;

        if(a[mid]>key)

            return zheDiSearch(a,low,mid-1,key);

        if(a[mid]<key)

            return zheDiSearch(a,mid+1,high,key);

    }

    return 0;

}


int main()

{

    int a[N],n,x,pos1,pos2,pos3;

    //从data2.txt文件中读入前N个数存入a中,函数返回成功读入的数据个数

    n=readData(a,N,"/Users/wsw/Desktop/数据结构/data2.txt");

    printf("请输入要查找的整数:");

    scanf("%d",&x);

    pos1=shunSearch(a,n,x);

    pos2=zheFeidiSearch(a,n,x);

    int low=0,high=n-1;

    pos3=zheDiSearch(a,low,high,x);

    if (pos1==-1||pos2==-1||pos3==-1)

        printf("查找失败!");

    else

        printf("顺序查找为:");

        printf("a[%d]=%d\n",pos1,x);

        printf("折半查找(非递归)为:");

        printf("a[%d]=%d\n",pos2,x);

        printf("折半查找(递归)为:");

        printf("a[%d]=%d\n",pos3,x);

    return 0;

}



猜你喜欢

转载自blog.csdn.net/qq_39360985/article/details/78795900