一、顺序查找和二分查找
#include<stdio.h>
#include<stdlib.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(SequentialList));
resultPtr->length = paraLength;
resultPtr->elements = (NodePtr)malloc(sizeof(Node) * (paraLength + 1));
for (i = 0; i < paraLength; 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;
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\n", sequentialSearch(tempListPtr, 10));
printf("Search result of 5 is: %c\n", sequentialSearch(tempListPtr, 5));
printf("Search result of 4 is: %c\n", sequentialSearch(tempListPtr, 4));
printf("Search result of 2 is: %c\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;
}
}
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\n", binarySearch(tempListPtr, 10));
printf("Search result of 5 is: %c\n", binarySearch(tempListPtr, 5));
printf("Search result of 4 is: %c\n", binarySearch(tempListPtr, 4));
printf("Search result of 2 is: %c\n", binarySearch(tempListPtr, 2));
}
int main() {
printf("\n--------sequentialSearchTest--------\n");
sequentialSearchTest();
printf("\n--------binarySearchTest--------\n");
binarySearchTest();
return 0;
}
二、哈希查找
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int key;
char value;
} Node, *NodePtr;
/**
*哈希表
*/
typedef struct HashList{
int length;
NodePtr elements;
} HashList, *ListPtr;
/**
*初始化
*/
ListPtr initList(int *paraKeys, char *paraValues, int paraLength) {
int i, tempPosition;
ListPtr resultPtr = (ListPtr)malloc(sizeof(HashList));
resultPtr->length = paraLength;
resultPtr->elements = (NodePtr)malloc(sizeof(Node) * 19);
for (i = 0; i < 19; i ++) {
resultPtr->elements[i].key = -1;
resultPtr->elements[i].value = 'x';
}
for (i = 0; i < paraLength; i ++) {
tempPosition = paraKeys[i] % 19;
while (resultPtr->elements[tempPosition].key != -1) {
tempPosition = (tempPosition + 1) % 19;
printf("Collision, move forward for key %d.\n", paraKeys[i]);
}
resultPtr->elements[tempPosition].key = paraKeys[i];
resultPtr->elements[tempPosition].value = paraValues[i];
}
return resultPtr;
}
/**
*查找
*/
char hashSearch(ListPtr paraPtr, int paraKey) {
int tempPosition = paraKey % 19;
while (paraPtr->elements[tempPosition].key != -1) {
if (paraPtr->elements[tempPosition].key == paraKey) {
return paraPtr->elements[tempPosition].value;
}
tempPosition = (tempPosition + 1) % 19;
}
return 'x';
}
void hashSearchTest() {
int tempUnsortedKeys[] = {16, 33, 38, 69, 57, 95, 86};
char tempContents[] = {'h', 'e', 'l', 'o', 'w', 'r', 'd'};
ListPtr tempPtr = initList(tempUnsortedKeys, tempContents, 7);
printf("Search resultPtr of 95 is: %c\n", hashSearch(tempPtr, 95));
printf("Search resultPtr of 38 is: %c\n", hashSearch(tempPtr, 38));
printf("Search resultPtr of 57 is: %c\n", hashSearch(tempPtr, 57));
printf("Search resultPtr of 4 is: %c\n", hashSearch(tempPtr, 4));
}
int main() {
hashSearchTest();
return 0;
}
顺序查找,做了一个哨兵,区分查找成功和失败
顺序查找:从表的一端开始,依次将表中的关键字和给定的值进行比较,若表中的关键字和给定的值相等,则查找成功,反之查找失败。
时间复杂度:O(n)
二分查找:前提是有序
每一次查找都可以将查找范围减半
时间复杂度:O(log2(N))
哈希查找:每个存储到哈希表中的元素,都配有一个唯一的下标,想查找哪个元素,凭借该元素对应的下标就可以直接找到它,无需遍历整个哈希表
先把数据存储,整理成一个哈希表,再来查找,如果两个元素的下标相同,则往后移动,直到找到一个空位,存储
时间复杂度:O(1)