1.代码实现
代码如下(示例):
#include <stdio.h>
#include <malloc.h>
typedef struct Node {
int key;
char value;
}Node, * NodePtr;
typedef struct SequentialList {
int lenth;
NodePtr elements;
}SequentialList, * ListPtr;
ListPtr initList(int* paraKeys, char* paraValues, int paralenth) {
int i;
ListPtr resultPtr = (ListPtr)malloc(sizeof(SequentialList));
resultPtr->lenth = paralenth;
resultPtr->elements = (NodePtr)malloc((paralenth + 1) * sizeof(Node));
for (int i = 0; i < paralenth; i++) {
resultPtr->elements[i].key = paraKeys[i];
resultPtr->elements[i].value = paraValues[i];
}
return resultPtr;
}
char sequentialSearch(ListPtr paraListPtr, int paraKey) {
int i=0;
paraListPtr->elements[paraListPtr->lenth].key = paraKey;
paraListPtr->elements[paraListPtr->lenth].value = 'x';
while (paraListPtr->elements[i].key != paraKey) {
i++;
}
return paraListPtr->elements[i].value;
}
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));
}
char binarySearch(ListPtr paraListPtr, int paraKey) {
int tempLeft = 1;
int tempRight = paraListPtr->lenth;
int tempMiddle = (tempLeft + tempRight) / 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;
}
}
return 'x';
}
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------binarySearchTest------\r\n");
binarySearchTest();
return 1;
}
2.读入数据
代码如下(示例):
------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
------binarySearchTest------
Search result of 10 is: !
Search result of 5 is: o
Search result of 4 is: l
Search result of 2 is: x
---
# 总结
顺序查找:
1.点睛之笔:多开辟一个空间,赋值-1,‘x’,所以无论表中是否存在需要查询的元素,结果总是不会越界。
2.所以这个节点可以加在开头,也可以加在结尾,此代码为在结尾添加了一个哨兵。
二分查找:
设立左右“指针”,每次取中间值来查询key,如果中间值比key大,就右“指针”=middle-1;否则相反为左“指针”=middle+1;直到查询到指定元素或者哨兵。