1.代码如下
#include <stdio.h>
#include <malloc.h>
/**
* element
*/
typedef struct Node{
int key;
char value;
}Node, *NodePtr;
/**
* list
*/
typedef struct SequentialList{
int length;
NodePtr elements;
}SequentialList,*ListPtr;
/**
* initialize the list
*/
ListPtr initList(int* paraKeys, char* paraValues, int paraLength)
{
ListPtr resultPtr=(ListPtr)malloc(sizeof(SequentialList));
resultPtr->length=paraLength;
resultPtr->elements=(NodePtr)malloc(sizeof(Node)*(paraLength+1));
int i;
for(i=0;i<paraLength;i++)
{
resultPtr->elements[i+1].key=paraKeys[i];
resultPtr->elements[i+1].value=paraValues[i];
}
return resultPtr;
}
/**
* sequentialSearch
*/
char sequentialSearch(ListPtr paraList, int paraKey)
{
paraList->elements[0].key=paraKey;
paraList->elements[0].value='x';
int i=paraList->length;
while(paraList->elements[i].key!=paraKey)
{
i--;
}
return paraList->elements[i].value;
}
/**
* test
*/
void sequentialSearchTest()
{
int tempUnsortedKeys[]={4,5,3,6,10,7,1,9};
char tempContents[]={'h','e','l','o','w','r','d','!'};
ListPtr tempListPtr=initList(tempUnsortedKeys, tempContents, 8);
printf("Search result of 10 is: %c\r\n",sequentialSearch(tempListPtr,10));
printf("Search result of 5 is: %c\r\n",sequentialSearch(tempListPtr,5));
printf("Search result of 4 is: %c\r\n",sequentialSearch(tempListPtr,4));
printf("Search result of 2 is: %c\r\n",sequentialSearch(tempListPtr,2));
}
/**
* binarysearch
*/
char binarySearch(ListPtr paraListPtr, int paraKey)
{
int tempLeft=1;
int tempRight=paraListPtr->length;
int tempMiddle;
while(tempLeft<=tempRight)
{
tempMiddle=(tempLeft+tempRight)/2;
if(paraKey<paraListPtr->elements[tempMiddle].key)
{
tempRight=tempMiddle-1;
}
else if(paraKey>paraListPtr->elements[tempMiddle].key)
{
tempLeft=tempMiddle+1;
}
else
{
return paraListPtr->elements[tempMiddle].value;
}
}
return 'x';
}
/**
* test
*/
void binarySearchTest()
{
int tempUnsortedKeys[]={1,3,4,5,6,7,9,10};
char tempContents[]={'h','e','l','o','w','r','d','!'};
ListPtr tempListPtr=initList(tempUnsortedKeys, tempContents,8);
printf("Search result of 10 is: %c\r\n",binarySearch(tempListPtr,10));
printf("Search result of 5 is: %c\r\n",binarySearch(tempListPtr,5));
printf("Search result of 4 is: %c\r\n",binarySearch(tempListPtr,4));
printf("Search result of 2 is: %c\r\n",binarySearch(tempListPtr,2));
}
int main()
{
printf("\r\n--------sequentialSearchTest----------\r\n");
sequentialSearchTest();
printf("\r\n-------binaraSearchTest-------\r\n");
binarySearchTest();
return 0;
}
2.图示
3.代码说明
1)顺序查找时,存储数据从下标1开始存储,将下标0的位置存储为要查找的值(哨兵)
2)二分查找时,left初始为1,right初始为length,middle为(left+right)/2,每次进行比较大小后,将left替换为middle+1或者right替换为middle-1操作
4.运行结果
--------sequentialSearchTest----------
Search result of 10 is: w
Search result of 5 is: e
Search result of 4 is: h
Search result of 2 is: x
-------binaraSearchTest-------
Search result of 10 is: !
Search result of 5 is: o
Search result of 4 is: l
Search result of 2 is: x
--------------------------------
Process exited after 0.01776 seconds with return value 0
请按任意键继续. . .