常用数据结构与算法之查找算法

查找算法

/*
 * =====================================================================================
 *
 *       Filename:  search.h
 *
 *    Description:  template function of search algo
 *
 *        Version:  1.0
 *        Created:  2012年04月15日 16时28分42秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), luoyi.smt@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef __SEARCH_H_
#define __SEARCH_H_
namespace stcpp
{
		/* 顺序查找 */
	template<class T>
		int sequence_search(T a[],  int len,  T key) 
		{
			for (int i = 0; i < len; i++ )
			{
				if (a[i] == key)
					return i;
			}
			return -1;
		}

	/* 二分查找 */
	template<class T>
	int binary_search(T a[],  int len,  T key) 
	{
		int low  = 0;
		int high = len -1;
		while (low <= high)
		{
			int mid = (low + high)/2;/* 折半 */
			if (a[mid] < key)
			{
				low = mid + 1;
			}
			else if (a[mid] > key)
			{
				high = mid -1;
			}
			else
				return mid;
		}
		return -1;
	}

/* 插值查找 */
	template<class T>
	int interpolation_search(T a[],  int len,  T key) 
	{

		int low  = 0;
		int high = len -1;
		while (low <= high)
		{
			int mid = low + (high - low)*(key - a[low])/(a[high] - a[low]);
			if (a[mid] < key)
			{
				low = mid + 1;
			}
			else if (a[mid] > key)
			{
				high = mid -1;
			}
			else
				return mid;
		}
		return -1;
	}
/* 斐波那契查找 */
	template<class T>
		int Fibonacci_Search(T a[], int n, T key)
		{
			int i,k=0;
			int low = 0;	/* 定义最低下标为记录首位 */
			int high = n -1;	/* 定义最高下标为记录末位 */

			/* 构建 斐波那契 数组*/
			int F[100] = {0};
			F[0] = 0;
			F[1] = 1;
			for (i=2; i<100; i++)
				F[i] = F[i-1] + F[i-2];
			
			/* 计算n位于斐波那契数列的位置 */
			while(n > F[k]-1)
				k++;
			/* 将不满 的数值以最后一个元素的值补全 */
			for (i= n - 1; i < F[k]-1; i++)
				a[i]=a[n -1];

			while(low <= high)
			{
				int mid=low + F[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 - 1)
						return mid;		/* 若相等则说明mid即为查找到的位置 */
					else 
						return -1;
				}
			}
			return -1;
		}
}
#endif //__SEARCH_H_


测试用例

/*
 * =====================================================================================
 *
 *       Filename:  test_search.cpp
 *
 *    Description:  test case of the search.h
 *
 *        Version:  1.0
 *        Created:  2012年04月15日 17时41分26秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Lavey Luo (lavey), luoyi.smt@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include "search.h"
#include <iostream>
using std::cout;
using std::endl;

int test_search(int argc,  char** argv)
{
	 int a[]  = {2, 6, 4, 3, 1, 5};
	 const unsigned int len  = sizeof(a)/sizeof(int);

	 int b[]  = {2, 4, 8, 10, 50, 88};
	 int key = 4;
	 int idx = stcpp::sequence_search(a, len, key);
	 cout << "sequence_search: idx of " << key << " is " << idx << endl;;

	 key = 10;
	 idx = stcpp::binary_search(b, len, key);
	 cout << "binary_search: idx of " << key << " is " << idx << endl;


	 key = 8;
	 idx = stcpp::interpolation_search(b, len, key);
	 cout << "interpolation_search: idx of " << key << " is " << idx << endl;
	 
	 key = 50;
	 idx = stcpp::Fibonacci_Search(b, len, key);
	 cout << "Fibonacci_Search: idx of " << key << "  is " << idx << endl;
	 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全集内容结构如下: ├─图 │ ├─关键路径(有向无环图及其应用2) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ CriticalPath.cpp │ │ CriticalPath.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的关节点 │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ FindArticul.cpp │ │ FindArticul.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的数组表示法 │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的遍历 │ │ ALGraph.cpp │ │ ALGraph.h │ │ DEBUG.txt │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MTraverse.cpp │ │ MTraverse.h │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的邻接表存储结构 │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─最短路径(从某个源点到其余各顶点的的最短路径) │ │ 1.txt │ │ 2.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ ShortestPath_DIJ.cpp │ │ ShortestPath_DIJ.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─最短路径(每一对顶点间的最短路径) │ 1.txt │ 2.txt │ InfoType.cpp │ InfoType.h │ Main.cpp │ map.txt │ MGraph.cpp │ MGraph.h │ RailwaySearch.cpp │ ShortestPath_FLOYD.cpp │ ShortestPath_FLOYD.h │ Status.h │ VertexType.cpp │ VertexType.h │ ├─排序 │ ├─冒泡排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_BubbleSort.cpp │ │ Sq_BubbleSort.h │ │ │ ├─哈希表(哈希查找) │ │ ElemType.cpp │ │ ElemType.h │ │ HashTable.cpp │ │ HashTable.h │ │ main.cpp │ │ Records.txt │ │ │ ├─基数排序 │ │ 1.txt │ │ main.cpp │ │ SLL_RadixSort.cpp │ │ SLL_RadixSort.h │ │ │ ├─归并排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ sq_MergeSort.cpp │ │ sq_MergeSort.h │ │ │ ├─快速排序 │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_QuitSort.cpp │ │ Sq_QuitSort.h │ │ │ ├─拓扑排序(有向无环图及其应用) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ TopologicalSort.cpp │ │ TopologicalSort.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─插入排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(希尔排序) │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(表插入排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ SL_InsertSort.cpp │ │ SL_InsertSort.h │ │ │ ├─选择排序(堆排序) │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_HeapSort.cpp │ │ Sq_HeapSort.h │ │ │ ├─选择排序(树形选择排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_TreeSelectionSort.cpp │ │ Sq_TreeSelectionSort.h │ │ │ └─选择排序(简单选择排序) │ 1.txt │ main.cpp │ RedType.cpp │ RedType.h │ Sq_SelectSort.cpp │ Sq_SelectSort.h │ ├─查找 │ ├─动态查找表(二叉排序树) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡二叉树(二叉排序树的平衡旋转生成) │ │ 1.txt │ │ BBSTree.cpp │ │ BBSTree.h │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡的m路查找树—B_树 │ │ BTree.cpp │ │ BTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─键树(Trie树) │ │ 1.txt │ │ main.cpp │ │ Record.h │ │ Status.h │ │ TrieTree.cpp │ │ TrieTree.h │ │ │ ├─键树(双链键树) │ │ 1.txt │ │ DLTree.cpp │ │ DLTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─静态查找表(有序表的查找) │ │ 1.txt │ │ main.cpp │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ │ ├─静态查找表(静态树表查找) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ TElmeType.h │ │ │ └─静态查找表(顺序表的查找) │ 1.txt │ main.cpp │ SElemType.cpp │ SElemType.h │ SSTable.cpp │ SSTable.h │ Status.h │ ├─树 │ ├─二叉树的二叉链表存储 │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─二叉树的顺序存储结构 │ │ main.cpp │ │ SqBiTree.cpp │ │ SqBiTree.h │ │ Status.h │ │ TELemType_define.cpp │ │ │ ├─哈夫曼树和哈夫曼编码 │ │ HuffmanTree.cpp │ │ HuffmanTree.h │ │ main.cpp │ │ Status.h │ │ │ ├─最小生成树 │ │ 1.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MiniSpanTree_Kruskal.cpp │ │ MiniSpanTree_Kruskal.h │ │ MiniSpanTree_PRIM.cpp │ │ MiniSpanTree_PRIM.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─树的二叉链表 │ │ CSTree.cpp │ │ CSTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─深度优先生成森林(无向图的连通性和生成树) │ │ ALGraph.cpp │ │ ALGraph.h │ │ CSTree.cpp │ │ CSTree.h │ │ DFSForest.cpp │ │ DFSForest.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─线索二叉树 │ BiThrTree.cpp │ BiThrTree.h │ main.cpp │ Status.h │ TElmeType.h │ └─表和数组 ├─KMP算法 │ Basic_operation_functions.h │ def_SString.h │ KMP.h │ main.cpp │ Status.h │ ├─n阶对称矩阵的压缩存储 │ Basic_operation_functions.h │ mian.cpp │ Status.h │ struct SyMatrix.h │ ├─三元组稀疏矩阵的压缩存储 │ Basic_operation_functions.h │ B_opera_fun_called_fun.h │ main.cpp │ Status.h │ struct TSMatrix.h │ Universal_main.h │ Universa_ts_b_opera_fun.h │ ├─不设头结点的单链表 │ LinkList.cpp │ LinkList.h │ main.cpp │ Status.h │ ├─串的堆存储结构 │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─串的定长顺序存储结构 │ Basic_operation_functions.h │ def_SString.h │ Main.cpp │ Status.h │ ├─广义表 │ GList.cpp │ GList.h │ main.cpp │ SString.cpp │ SString.h │ Status.h │ ├─数组 │ Basic_operation_functions.h │ main.cpp │ Status.h │ struct array.h │ ├─文本编辑(串和行表操作) │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─栈的顺序存储结构 │ main.cpp │ SqStack.cpp │ SqStack.h │ Status.h │ ├─走迷宫 │ Basic_called_functions.h │ Basic_operation_functions.h │ Main_Maze.cpp │ Status.h │ struct SqStack.h │ └─链队列 Basic_called_functions.cpp Basic_called_functions.h Basic_operation_functions.cpp main.cpp QElemType.h Status.h Struct_LinkQueue.h

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值