来自 编程 2019-09-27 11:02 的文章
当前位置: 澳门三合彩票 > 编程 > 正文

顺序表静态查找

澳门三合彩票,实现顺序表的静态查找(SeqSearch、BinarySearch、GoldenSectionSearch、)

how many elements in your list:20Please enter the 1th number:1Please enter the 2th number:11Please enter the 3th number:2Please enter the 4th number:22Please enter the 5th number:3Please enter the 6th number:33Please enter the 7th number:4Please enter the 8th number:44Please enter the 9th number:5Please enter the 10th number:55Please enter the 11th number:6Please enter the 12th number:66Please enter the 13th number:7Please enter the 14th number:77Please enter the 15th number:8Please enter the 16th number:88Please enter the 17th number:9Please enter the 18th number:99Please enter the 19th number:10Please enter the 20th number:111The list you create is:1  11  2  22  3  33  4  44  5  55  6  66  7  77  8  88  9  99  10  111The list after sorting is:1  2  3  4  5  6  7  8  9  10  11  22  33  44  55  66  77  88  99  111Please enter the number you want to search:3SeqSearch=======18the number's position is:3BinarySearch=======4the number's position is:3GoldenSectionSearch=======5the number's position is:3请按任意键继续. . .

#include<stdio.h>#include<stdlib.h>#include<math.h>#define GOLD 0.618#define MAX_SIZE 100typedef int ElementType;typedef int KeyType;typedef struct {    ElementType elem[MAX_SIZE];    int length;}SSTable;int Search(SSTable *ST,KeyType key);void ListInit(SSTable *ST);void ListCreate(SSTable *ST, int count);void ListSort(SSTable *ST);int BinarySearch(SSTable *ST,KeyType key);void ListDisplay(SSTable *ST);int GoldenSectionSearch(SSTable *ST,KeyType key);void main(){    int i,count, num;    SSTable ST;    ListInit(&ST);    printf_s("how many elements in your list:");    scanf_s("%d",&count);    ListCreate(&ST,count);    printf_s("The list you create is:n");    ListDisplay(&ST);    ListSort(&ST);    printf_s("The list after sorting is:n");    ListDisplay(&ST);    printf_s("Please enter the number you want to search:");    scanf_s("%d",&num);    printf_s("SeqSearch");    i = Search(&ST,num);    printf_s("the number's position is:%dn",i);    printf_s("BinarySearch");    i = BinarySearch(&ST,num);    printf_s("the number's position is:%dn",i);    printf_s("GoldenSectionSearch");    i = GoldenSectionSearch(&ST,num);    printf_s("the number's position is:%dn",i);    system("pause");}void ListInit(SSTable *ST){    ST->length=0;}void ListCreate(SSTable *ST, int count){    int i,num;    ST->length=count;    for(i=1;i<=count;++i)    {        printf_s("Please enter the %dth number:",i);        scanf_s("%d",&num);        ST->elem[i]=num;    }}void ListSort(SSTable *ST){    int i,j,temp;    for(j=1;j<=ST->length-1;++j)    {        for(i=1;i<=ST->length-j;++i)        {            if(ST->elem[i]>ST->elem[i+1])            {                temp=ST->elem[i];                ST->elem[i]=ST->elem[i+1];                ST->elem[i+1]=temp;            }        }    }}int Search(SSTable *ST,KeyType key){    int i, counter;    counter = 0;    ST->elem[0]=key;    for(i = ST->length; i > 0; --i)    {        ++counter;        if(key==ST->elem[i])        {            printf_s("=======%dn",counter);            return i;        }    }    printf_s("=======%dn",counter);    return i;}int BinarySearch(SSTable *ST,KeyType key){    int low,high,mid,counter;    low=1;    counter=1;    high=ST->length;    while(low<=high)    {        mid=/2;        if(key==ST->elem[mid])        {            printf_s("=======%dn",counter);            return mid;        }else if(key<ST->elem[mid])        {            ++counter;            high=mid-1;        }else        {            ++counter;            low=mid+1;        }    }    printf_s("=======%dn",counter);    return 0;}int GoldenSectionSearch(SSTable *ST,KeyType key){    int low,high,mid,counter;    low=1;    counter=1;    high=ST->length;    while(low<=high)    {        mid=(high -low)*GOLD +low;        //mid = high * GOLD + low*;        if(key==ST->elem[mid])        {            printf_s("=======%dn",counter);            return mid;        }else if(key<ST->elem[mid])        {            ++counter;            high=mid-1;        }else        {            ++counter;            low=mid+1;        }    }    printf_s("=======%dn",counter);    return 0;}void ListDisplay(SSTable *ST){    int i;    for (i = 1; i <= ST->length; ++i)    {        printf_s("%d  ", ST->elem[i]);    }    printf_s("n");}

本文由澳门三合彩票发布于编程,转载请注明出处:顺序表静态查找

关键词: