zoukankan      html  css  js  c++  java
  • 数据结构实验五:查找

    查找——实现监视哨法查找

    #include<stdio.h>
    #include<stdlib.h>
    int cmp(const void *a,const void *b)
    {
        return *(int *)a-*(int *)b;
    }
    int main()
    {
        int a[101],i;
        for(i=1;i<=100;i++)
            a[i]=rand()%100;
        for(i=1;i<=100;i++)
            printf("%d ",a[i]);
        printf("\n");
        int key;
        scanf("%d",&key);
        printf("顺序查找\n");
        a[0]=key;
        for(i=100;i>=0;i--)
            if(a[i]==key){
                    printf("%d\n",i);
                    break;
                    }
        qsort(&a[1],100,sizeof(int),cmp);
        for(i=1;i<=100;i++)
            printf("%d ",a[i]);
        printf("\n");
        printf("折半查找\n");
        int mid,start=1,end=100;
        mid=(start+end)/2;
        while(start<=end)
        {
            if(a[mid]==key){
                printf("%d\n",mid);
                break;
            }
            if(a[mid]>key){
                end=mid-1;
                mid=(end+start)/2;
            }
            else{
                start=mid+1;
                mid=(start+end)/2;
            }
        }
        printf("end\n");
    }
  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/ma6174/p/2313327.html
Copyright © 2011-2022 走看看