一、代码块
键值对
typedef struct Node{
int key;
char value;
}Node, *NodePtr;
构建表
typedef struct SequentialList{
int length;
NodePtr elements;
}SequentialList, *ListPtr;
创建表
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++){
//printf("setting key for index %d: %d and value: %c\r\n", i, paraKeys[i], paraValues[i]);
resultPtr->elements[i + 1].key = paraKeys[i];
resultPtr->elements[i + 1].value = paraValues[i];
}
return resultPtr;
}
顺序查找
char sequentialSearch(ListPtr paraListPtr, int paraKey){
int i = paraListPtr->length;
//未找到返回x
paraListPtr->elements[0].key = paraKey;
paraListPtr->elements[0].value = 'x';
while(paraListPtr->elements[i].key != paraKey){
i--;
}
return paraListPtr->elements[i].value;
}
二分查找
char binarySearch(ListPtr paraListPtr, int paraKey){
int tempLeft = 1;
int tempRight = paraListPtr->length;
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;
}
}
//未找到返回x
return 'x';
}
二、全部代码
//
// main.c
// 顺序查找与二分查找
//
// Created on 2022/6/6.
//
#include <stdio.h>
#include <malloc.h>
//键值对
typedef struct Node{
int key;
char value;
}Node, *NodePtr;
//构建表
typedef struct SequentialList{
int length;
NodePtr elements;
}SequentialList, *ListPtr;
//创建表
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++){
//printf("setting key for index %d: %d and value: %c\r\n", i, paraKeys[i], paraValues[i]);
resultPtr->elements[i + 1].key = paraKeys[i];
resultPtr->elements[i + 1].value = paraValues[i];
}
return resultPtr;
}
//顺序查找
char sequentialSearch(ListPtr paraListPtr, int paraKey){
int i = paraListPtr->length;
//未找到返回x
paraListPtr->elements[0].key = paraKey;
paraListPtr->elements[0].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->length;
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;
}
}
//未找到返回x
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));
}
//main
int main(){
printf("\r\n-------sequentialSearchTest-------\r\n");
sequentialSearchTest();
printf("\r\n-------binarySearchTest-------\r\n");
binarySearchTest();
return 0;
}
三、小结
顺序查找的时间复杂度为O(n) 二分查找的时间复杂度为O(log(n))