栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构第十三天——顺序查找、二分查找和哈希查找

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构第十三天——顺序查找、二分查找和哈希查找

一、顺序查找和二分查找

#include
#include


 
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: %cn", sequentialSearch(tempListPtr, 10));
	printf("Search result of 5 is: %cn", sequentialSearch(tempListPtr, 5));
	printf("Search result of 4 is: %cn", sequentialSearch(tempListPtr, 4));
	printf("Search result of 2 is: %cn", 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: %cn", binarySearch(tempListPtr, 10));
	printf("Search result of 5 is: %cn", binarySearch(tempListPtr, 5));
	printf("Search result of 4 is: %cn", binarySearch(tempListPtr, 4));
	printf("Search result of 2 is: %cn", binarySearch(tempListPtr, 2));
}

int main() {
	printf("n--------sequentialSearchTest--------n");
	sequentialSearchTest();
	
	printf("n--------binarySearchTest--------n");
	binarySearchTest();
	
	return 0;
}

二、哈希查找

#include
#include

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: %cn", hashSearch(tempPtr, 95));
	printf("Search resultPtr of 38 is: %cn", hashSearch(tempPtr, 38));
	printf("Search resultPtr of 57 is: %cn", hashSearch(tempPtr, 57));
	printf("Search resultPtr of 4 is: %cn", hashSearch(tempPtr, 4));
}

int main() {
	hashSearchTest();
	return 0;
}

顺序查找,做了一个哨兵,区分查找成功和失败

顺序查找:从表的一端开始,依次将表中的关键字和给定的值进行比较,若表中的关键字和给定的值相等,则查找成功,反之查找失败。

时间复杂度:O(n)

二分查找:前提是有序

每一次查找都可以将查找范围减半

时间复杂度:O(log2​(N))

哈希查找:每个存储到哈希表中的元素,都配有一个唯一的下标,想查找哪个元素,凭借该元素对应的下标就可以直接找到它,无需遍历整个哈希表

先把数据存储,整理成一个哈希表,再来查找,如果两个元素的下标相同,则往后移动,直到找到一个空位,存储

时间复杂度:O(1)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955294.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号