1.代码如下
/**
*Sequential search and binary search.
*/
#include<stdio.h>
#include<malloc.h>
/**
*<key,value>pair.
*/
typedef struct Node{
int key;
char value;
}Node,*NodePtr;
/**
*The data structure of the aequential list.
*/
typedef struct SequentialList{
int length;
NodePtr elements;
}SequentialList,*ListPtr;
/**
*Initalize a data array.
*/
ListPtr initList(int *paraKeys,char *paraValues,int paraLength){
int i;
ListPtr resultPtr=(ListPtr)malloc(sizeof(struct SequentialList));
resultPtr->length=paraLength;
resultPtr->elements=(NodePtr)malloc((paraLength+1)*sizeof(struct Node));
for(i=0;i<paraLength;i++){
resultPtr->elements[i+1].key = paraKeys[i];
resultPtr->elements[i+1].value = paraValues[i];
}//of for i
return resultPtr;
}//of initList
/**
/SequentialList search.
*@return The value.
*/
char sequentialSearch(ListPtr paraListPtr,int paraKeys){
int i=paraListPtr->length;
paraListPtr->elements[0].key=paraKeys;
paraListPtr->elements[0].value='x';
while(paraListPtr->elements[i].key!=paraKeys){
i--;
}//of while
return paraListPtr->elements[i].value;
}//of sequentialSearch
/**
*Test the sequential search function.
*/
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));
}//of sequtntialSearchTest
/**
*Binary search.
*@return The value.
*/
char binarySearch(ListPtr paraListPtr,int paraKey){
int tempLeft =1;
int tempRight=paraListPtr->length;
int tempMiddle=(tempRight+tempLeft)/2;
while(tempLeft<=tempRight){
tempMiddle=(tempLeft+tempRight)/2;
if(paraListPtr->elements[tempMiddle].key==paraKey){
return paraListPtr->elements[tempMiddle].value;
}else if(paraListPtr->elements[tempMiddle].key<=paraKey){
tempLeft=tempMiddle+1;
}else{
tempRight=tempMiddle-1;
}//of if
}//of while
//Not found .
return 'x';
}//of binarysearch
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));
}//of binarysearchTest
/**
*The entrance
*/
int main() {
printf("\r\n------sequentialSearchTest------\r\n");
sequentialSearchTest();
printf("\r\n------binarySearchTest------\r\n");
binarySearchTest();
return 1;
}//of main
2.运行结果
3.小结
顺序查找和二分查找是两种基本的在数据结构(如数组或列表)中查找特定元素的算法。下面是对这两种查找方法的小结:
顺序查找(Linear Search)
定义:
顺序查找,也称为线性搜索,是一种简单的查找方法,通过遍历数据结构中的每个元素,逐个与目标值进行比较,直到找到匹配项或遍历完所有元素。
特点:
1. 简单易实现:不需要对数据进行预先排序。
2. 适用范围广:可以应用于任何类型的数据结构,包括无序的集合。
3. 效率:*平均和最坏情况下的时间复杂度都是O(n),其中n是数据结构中的元素数量。如果目标元素位于数据末尾,那么需要检查所有元素。
二分查找(Binary Search)
定义:
二分查找是一种在有序数组中查找特定元素的高效算法。它的工作原理是将数组分成两半,比较中间元素与目标值,根据比较结果缩小搜索范围,重复此过程直到找到目标值或确定不存在于数组中。
特点:
1. 前提条件:数据必须是有序的(升序或降序)。
2. 效率高:平均和最坏情况下的时间复杂度为O(log n)。这意味着对于大数组,二分查找比顺序查找要快得多。
3. 实现复杂度稍高:需要维护两个指针来界定搜索区间,并根据比较结果调整这些指针。
4. 不适用于无序数据:由于其依赖于数据的有序性,对于无序数据集,必须先排序再使用二分查找,这会增加额外的时间成本。
总结
选择依据:如果数据集较小或无序,顺序查找因其简单性可能更合适。对于大型且已排序的数据集,二分查找因其高效的查找速度成为首选。
性能对比: 二分查找在效率上通常优于顺序查找,特别是当数据量增大时,这种优势更加明显。
实际应用: 在实际开发中,根据具体需求和数据特性选择合适的查找方法。例如,在数据库索引、算法题解、以及需要快速检索的场景中,二分查找被广泛使用。而顺序查找则可能用于简单的列表处理或作为其他算法的基础步骤。