第八章:3.顺序查找表

本篇文章参考的是《大话数据结构》,感谢作者程杰先生。

一:
顺序查找(Sequential Search):
又叫线性查找,是最基本的查找技术,它的查找过程是:
从表中第一个(或最后一个)记录开始,逐个进行记录的关键字和给定值比较,
若某个记录的关键字和给定的值相等,则查找成功,找到所查的记录;
如果直到最后一个(或第一个)记录,其关键字和给定的值比较都不相等时,
则表示表中没有所查的记录,查询不成功。

二:
设置“哨兵”编程技巧:

1.无哨兵顺序查找:缺点是每次都要检查是否越界
for(i=1;i<=n;i++)
{
if (a[i]==key)
return i;
}

2.有哨兵顺序查找:优点是不需要每次比较i和n的值了:
while(a[i]!=key) //key为哨兵
{
i–;
}

三:有序表查找:
1.折半查找(Binary Search):
又称二分查找,它的前提是线性表中的记录必须是关键码有序(通常从小到大有序),
线性表必须采用顺序存储。

折半查找的基本思想是:
在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键字相等,则查找成功;
若给定值小于中间记录的关键字,则在中间记录的左半区继续查找;
若给定值大于中间记录的关键字,则在中间记录的右半区继续查找。
不断重复上述过程,知道查找成功为止,或所有查找区域无记录,查找失败为止。

2.差值查找(Interpolation Search):
是根据要查找的关键字key与查找表中最大最小记录的关键字比较后的查找方法,
其核心就在于插值的计算公式 (key -a[low]) / (a[high] - a[low])

3.斐波那契查找(Fibonacci Search):
利用了黄金分割原理实现;

四:线性索引查找:
1.索引就是把一个关键字与它对应的记录相关联的过程,
一个索引由若干个索引构成,每个索引项至少应包含关键字和其对应的记录在存储器中的位置等信息。
索引技术是组织大型数据库以及磁盘文件的一种重要技术。

索引按照结构分类:
*线性索引:
*树形索引:
*多级索引:

三种线性索引:
*稠密索引:
*分块索引:
*倒排索引:

五:代码实现:

/*
 
本篇文章参考的是《大话数据结构》,感谢作者程杰先生。

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#include "bigtalk_data_structure.h"

#include "stdio.h"    
#include "stdlib.h"   

#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100 /* 存储空间初始分配量 */

typedef int Status;	/* Status是函数的类型,其值是函数结果状态代码,如OK等 */ 

int Fibo[MAXSIZE]; /* 斐波那契数列 */

/* 无哨兵顺序查找,a为数组,n为要查找的数组个数,key为要查找的关键字 */
int Sequential_Search(int *a,int n,int key)
{
    
    
	int i;
	for(i = 1; i <= n; i++)
	{
    
    
		if (a[i] == key)
		{
    
    
			return i;
		}			
	}
	return 0;
}
/* 有哨兵顺序查找 */
int Sequential_Search2(int *a,int n,int key)
{
    
    
	int i;
	a[0] = key;
	i = n;
	
	while(a[i] != key)
	{
    
    
		i--;
	}
	return i;
}


/* 折半查找 */
int Binary_Search(int *a,int n,int key)
{
    
    
	int low,high,mid;
	
	low = 1;	/* 定义最低下标为记录首位 */
	high = n;	/* 定义最高下标为记录末位 */

	while (low <= high)
	{
    
    
		mid=(low+high)/2;	/* 折半 */
		
		if (key<a[mid])		/* 若查找值比中值小 */
		{
    
    
			high=mid-1;		/* 最高下标调整到中位下标小一位 */
		}	
		else if (key>a[mid])/* 若查找值比中值大 */
		{
    
    
			low=mid+1;		/* 最低下标调整到中位下标大一位 */
		}
			
		else
		{
    
    
			return mid; 	/* 若相等则说明mid即为查找到的位置 */
		}					
	}
	
	return 0;
}


/* 插值查找 */
int Interpolation_Search(int *a,int n,int key)
{
    
    
	int low,high,mid;
	
	low = 1;	/* 定义最低下标为记录首位 */
	high = n;	/* 定义最高下标为记录末位 */
	
	while (low <= high)
	{
    
    
		mid = low + (high-low)*(key-a[low])/(a[high]-a[low]); /* 插值 */
		
		if (key<a[mid])		/* 若查找值比插值小 */
		{
    
    
			high=mid-1; 	/* 最高下标调整到插值下标小一位 */
		}			
		else if (key>a[mid])/* 若查找值比插值大 */
		{
    
    
			low=mid+1;		/* 最低下标调整到插值下标大一位 */
		}
		else
		{
    
    
			return mid; 	/* 若相等则说明mid即为查找到的位置 */
		}		
	}
	return 0;
}


/* 斐波那契查找 */
int Fibonacci_Search(int *a,int n,int key)
{
    
    
	int low,high,mid,i,k=0;
	low = 1;						/* 定义最低下标为记录首位 */
	high = n;						/* 定义最高下标为记录末位 */
	
	while(n > Fibo[k]-1)
	{
    
    
		k++;
	}
		
	for (i = n; i < Fibo[k]-1; i++)
	{
    
    
		a[i] = a[n];
	}
		
	while(low <= high)
	{
    
    
		mid = low + Fibo[k-1] - 1;
		
		if (key < a[mid])
		{
    
    
			high = mid-1;		
			k = k-1;
		}
		else if (key > a[mid])
		{
    
    
			low = mid+1;		
			k = k-2;
		}
		else
		{
    
    
			if (mid <= n)
			{
    
    
				return mid; 	/* 若相等则说明mid即为查找到的位置 */
			}
			else 
			{
    
    
				return n;
			}
		}
	}
	
	return 0;
}


//查找:静态查找
void test_main_8_1()
{
    
    
	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	   

	int a[MAXSIZE+1],i,result;
	int arr[MAXSIZE]={
    
    0,1,16,24,35,47,59,62,73,88,99};
		
	for(i = 0; i <= MAXSIZE; i++)
	{
    
    
		a[i] = i;
		//printf("[%s:%d]:[yang] a[%d] = %d\n",__FUNCTION__,__LINE__,i, a[i]);
	}
	
	result = Sequential_Search(a, MAXSIZE, MAXSIZE);
	printf("[%s:%d]:[yang] Sequential_Search: result = %d\n",__FUNCTION__,__LINE__,result);	   

	result = Sequential_Search(a, MAXSIZE, 50);
	printf("[%s:%d]:[yang] Sequential_Search: result = %d\n",__FUNCTION__,__LINE__,result);

	result = Sequential_Search(a, MAXSIZE, 150);
	printf("[%s:%d]:[yang] Sequential_Search: result = %d\n",__FUNCTION__,__LINE__,result);


	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	   
	

	result = Sequential_Search2(a, MAXSIZE, 1);
	printf("[%s:%d]:[yang] Sequential_Search2: result = %d\n",__FUNCTION__,__LINE__,result);

	result = Sequential_Search2(a, MAXSIZE, 50);
	printf("[%s:%d]:[yang] Sequential_Search2: result = %d\n",__FUNCTION__,__LINE__,result);	


	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	   

	result = Binary_Search(a, MAXSIZE, 50);
	printf("[%s:%d]:[yang] Binary_Search: result = %d\n",__FUNCTION__,__LINE__,result);	

	result = Binary_Search(arr, 10, 62);
	printf("[%s:%d]:[yang] Binary_Search: result = %d\n",__FUNCTION__,__LINE__,result);	

	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	   


	result = Interpolation_Search(arr, 10, 62);
	printf("[%s:%d]:[yang] Interpolation_Search: result = %d\n",__FUNCTION__,__LINE__,result);	

	result = Interpolation_Search(arr, 10, 99);
	printf("[%s:%d]:[yang] Interpolation_Search: result = %d\n",__FUNCTION__,__LINE__,result);	


	printf("[%s:%d]:[yang] ******************* 我是分割线******************* \n",__FUNCTION__,__LINE__);	   

	
	Fibo[0] = 0;
	Fibo[1] = 1;
	
	for (i = 2; i < 100; i++)  
	{
    
     
		Fibo[i] = Fibo[i-1] + Fibo[i-2];  
	} 
	
	result = Fibonacci_Search(arr,10,62);
	printf("[%s:%d]:[yang] Fibonacci_Search: result = %d\n",__FUNCTION__,__LINE__,result);	

	result = Fibonacci_Search(arr,10,99);
	printf("[%s:%d]:[yang] Fibonacci_Search: result = %d\n",__FUNCTION__,__LINE__,result);	

	#if 0

	#endif
}



打印:

[main:14]:[yang] ***************************************** 
[test_main_8_1:170]:[yang] ******************* 我是分割线******************* 
[test_main_8_1:182]:[yang] Sequential_Search: result = 100
[test_main_8_1:185]:[yang] Sequential_Search: result = 50
[test_main_8_1:188]:[yang] Sequential_Search: result = 0
[test_main_8_1:191]:[yang] ******************* 我是分割线******************* 
[test_main_8_1:195]:[yang] Sequential_Search2: result = 1
[test_main_8_1:198]:[yang] Sequential_Search2: result = 50
[test_main_8_1:201]:[yang] ******************* 我是分割线******************* 
[test_main_8_1:204]:[yang] Binary_Search: result = 50
[test_main_8_1:207]:[yang] Binary_Search: result = 7
[test_main_8_1:209]:[yang] ******************* 我是分割线******************* 
[test_main_8_1:213]:[yang] Interpolation_Search: result = 7
[test_main_8_1:216]:[yang] Interpolation_Search: result = 10
[test_main_8_1:219]:[yang] ******************* 我是分割线******************* 
[test_main_8_1:231]:[yang] Fibonacci_Search: result = 7
[test_main_8_1:234]:[yang] Fibonacci_Search: result = 10

猜你喜欢

转载自blog.csdn.net/yanghangwww/article/details/111410844