五大查找



一、顺序查找:

    顺序查找的原理很简单,就是遍历整个列表,逐个进行记录的关键字与给定值比较,若某个记录的关键字和给定值相等,则查找成功,找到所查的记录。如果直到最后一个记录,其关键字和给定值比较都不等时,则表中没有所查的记录,查找失败。

  1. public static int SequenceSearch(int[] arr, int key)
  2. {
  3. for ( int i = 0; i < arr.Length; i++)
  4. {
  5. if (arr[i] == key)
  6. {
  7. return i;
  8. }
  9. }
  10. return -1;
  11. }

二、折半查找(二分查找):

    时间复杂度为O(LogN)。

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

    从二分查找的定义我们可以看出,使用二分查找有两个前提条件:

1,待查找的列表必须有序

2,必须使用线性表的顺序存储结构来存储数据。

  1. public static int BinarySearch(int[] arr, int key,int low,int high)
  2. {
  3. while (low <= high)
  4. {
  5. int middle = (low + high) / 2;
  6. //判断中间记录是否与给定值相等
  7. if (key == arr[middle])
  8. return middle;
  9. else
  10. {
  11. // 在中间记录的左半区查找
  12. if (key < arr[middle])
  13. high = middle - 1;
  14. // 在中间记录的左半区查找
  15. else
  16. low = middle + 1;
  17. }
  18. }
  19. return -1;
  20. }
三、分块查找(索引顺序查找):

    索引查找又称为分块查找,是一种介于顺序查找和二分查找之间的一种查找方法,

    分块查找的基本思想是:分块查找把线性表分成若干块,每块中的元素存储顺序是任意的,但是块与块之间必须按照关键字大小有序排列,即前一块中的最大关键字要小于后一块中的最小关键字。对顺序表进行分块查找需要额外建立一个索引表,表中的每一项对应线性表中的一块,每个索引项都有键值分量和链值分量,键值分量存放对应块的最大关键字,链值分量存放指向本块第一个元素和最后一个元素的指针(即在数组中的下标)。显然索引表中的所有索引项都是按照其关键字递增顺序排列的。

    首先查找索引表,可用二分查找或顺序查找,然后在确定的块中进行顺序查找。

    

  1. /// <summary>
  2. /// 分块
  3. /// </summary>
  4. /// <param name="arr"></param>
  5. ///<param name="blockSize">每块大小</param>
  6. /// <returns></returns>
  7. public static IndexItem[] DividBlock(int[] arr, int blockSize)
  8. {
  9. int totalBlock = ( int)Math.Ceiling(arr.Length * 1.0 / blockSize); //块的总数
  10. IndexItem[] blocks = new IndexItem[totalBlock];
  11. //确定每一块的信息
  12. int j = 0; //数组的索引
  13. int k = 0;
  14. int s = 0;
  15. for ( int i = 0; i < totalBlock; i++)
  16. {
  17. s = j * blockSize;
  18. k = s + blockSize - 1;
  19. blocks[i].start = s;
  20. if (k>arr.Length - 1)
  21. {
  22. k = arr.Length - 1;
  23. }
  24. blocks[i].end = k;
  25. blocks[i].key = arr[k];
  26. j++;
  27. }
  28. return blocks;
  29. }
  30. public static int IndexSearch(int[] arr,int key,int blockSize)
  31. {
  32. IndexItem[] indexItem = DividBlock(arr, blockSize); //对数组进行分块,得到索引列表
  33. int i= 0;
  34. while (i < indexItem.Length && key > indexItem[i].key) //从索引列表中查找key所在的索引列表
  35. {
  36. i++;
  37. }
  38. if (i >= indexItem.Length)
  39. return -1;
  40. int j = indexItem[i].start;
  41. int k = indexItem[i].end;
  42. for ( int l = j ; l <=k; l++) //根据key所在的索引列表的索引进行顺序查找key在数组中的位置
  43. {
  44. if (key == arr[l])
  45. {
  46. return l;
  47. }
  48. }
  49. return -1;
  50. }
  51. }
  52. /// <summary>
  53. /// 索引表:存储主表分块后的每一块信息
  54. /// </summary>
  55. public struct IndexItem
  56. {
  57. public int key; //存放对应块中的最大关键字
  58. public int start; //存放对应块的第一个元素位置
  59. public int end; //存放对应块的最后一个元素位置
  60. }
四、二叉排序树(二叉搜索树):
    什么是二叉排序树呢?二叉排序树具有以下几个特点。
1,若根节点有左子树,则左子树的所有节点都比根节点小。
2,若根节点有右子树,则右子树的所有节点都比根节点大。
3,根节点的左,右子树也分别为二叉排序树。


下面是二叉排序树常见的操作及思路。

1,插入节点

思路:比如我们要插入数字20到这棵二叉排序树中。那么步骤如下:

1) 首先将20与根节点进行比较,发现比根节点小,所以继续与根节点的左子树30比较。

2) 发现20比30也要小,所以继续与30的左子树10进行比较。

3) 发现20比10要大,所以就将20插入到10的右子树中。

此时二叉排序树效果如图:


2,查找节点

比如我们要查找节点10,那么思路如下:

1) 还是一样,首先将10与根节点50进行比较大小,发现比根节点要小,所以继续与根节点的左子树30进行比较。

2) 发现10比左子树30要小,所以继续与30的左子树10进行比较。

3) 发现两值相等,即查找成功,返回10的位置。

过程与插入相同,这里就不贴图了。

3,删除节点

删除节点的情况相对复杂,主要分以下三种情形:

1) 删除的是叶节点(即没有孩子节点的)。比如20,删除它不会破坏原来树的结构,最简单。如图所示。


2) 删除的是单孩子节点。比如90,删除它后需要将它的孩子节点与自己的父节点相连。情形比第一种复杂一些。


3) 删除的是有左右孩子的节点。

1、找到该节点的右子树中的最左孩子(也就是右子树中序遍历的第一个节点),作为删除节点左右孩子的根结点。
2、把它的值和要删除的节点的值进行交换
3、然后删除这个节点即相当于把我们想删除的节点删除了,返回true;


  1. /// <summary>
  2. /// 创建二叉排序树
  3. /// </summary>
  4. /// <param name="arr"></param>
  5. /// <returns></returns>
  6. public static BSTree CreateBSTree(int[] arr)
  7. {
  8. if (arr == null || arr.Length == 0) return default(BSTree);
  9. BSTree bsTree = new BSTree()
  10. {
  11. Data = arr[ 0],
  12. Left = null,
  13. Right = null
  14. };
  15. for ( int i = 1; i < arr.Length; i++) //0位置已经给根节点了
  16. {
  17. bool isExcute = false;
  18. InsertBSTree(bsTree, arr[i], ref isExcute);
  19. }
  20. return bsTree;
  21. }
  22. /// <summary>
  23. /// 插入节点
  24. /// </summary>
  25. /// <param name="tree"></param>
  26. /// <param name="key"></param>
  27. /// <param name="isExcute"></param>
  28. public static void InsertBSTree(BSTree tree, int key, ref bool isExcute)
  29. {
  30. if (tree == null) return;
  31. if (tree.Data > key)
  32. InsertBSTree(tree.Left, key, ref isExcute);
  33. else
  34. InsertBSTree(tree.Right, key, ref isExcute);
  35. if (!isExcute)
  36. {
  37. BSTree current = new BSTree
  38. {
  39. Data = key,
  40. Left = null,
  41. Right = null
  42. };
  43. if (tree.Data > key) tree.Left = current;
  44. else tree.Right = current;
  45. isExcute = true;
  46. }
  47. }
  48. /// <summary>
  49. /// 中序遍历二叉排序树
  50. /// </summary>
  51. /// <param name="tree"></param>
  52. public static void LDR(BSTree tree)
  53. {
  54. if (tree == null) return;
  55. LDR(tree.Left);
  56. Console.Write(tree.Data + "\t");
  57. LDR(tree.Right);
  58. }
  59. /// <summary>
  60. /// 查找结点
  61. /// </summary>
  62. /// <param name="tree"></param>
  63. /// <param name="key"></param>
  64. /// <returns></returns>
  65. public static bool SearchBSTree(BSTree tree, int key)
  66. {
  67. if (tree == null) return false;
  68. if (tree.Data == key) return true;
  69. if (key < tree.Data) return SearchBSTree(tree.Left, key);
  70. else return SearchBSTree(tree.Right, key);
  71. }
  72. /// <summary>
  73. /// 删除结点
  74. /// </summary>
  75. /// <param name="tree"></param>
  76. /// <param name="key"></param>
  77. public static void DeleteNode(ref BSTree tree, int key)
  78.         {
  79.             if (tree == null) return;
  80.             //判断是否是要删除的节点
  81.             if (key == tree.Data)
  82.             {
  83.                 //第一种情况:叶子节点(没有孩子节点)
  84.                 if (tree.Left == null && tree.Right == null)
  85.                 {
  86.                     tree = null;
  87.                     return;
  88.                 }
  89.                 //第二种情况:仅有左子树
  90.                 if (tree.Left != null && tree.Right == null)
  91.                 {
  92.                     tree = tree.Left;
  93.                     return;
  94.                 }
  95.                 //第三种情况:仅有右子树
  96.                 if (tree.Left == null && tree.Right != null)
  97.                 {
  98.                     tree = tree.Right;
  99.                     return;
  100.                 }
  101.                 //第四种情况:有左,右子树
  102.                 if (tree.Left != null && tree.Right != null)
  103.                 {
  104.                     //利用中序遍历找到右节点的左子树的最左孩子
  105.                     var node = tree.Right;
  106.                     while (node.Left != null)
  107.                     {
  108.                         node = node.Left;
  109.                     }
  110.                     node.Left = tree.Left;
  111.                     //if (node.Right == null)
  112.                     //{
  113.                     //    DeleteNode(ref tree, node.Data);
  114.                     //    node.Right = tree.Right;
  115.                     //}
  116.                     tree = node;
  117.                 }
  118.             }
  119. //遍历找到要删除的节点
  120. if (key < tree.Data) { DeleteNode( ref tree.Left, key); }
  121. else DeleteNode( ref tree.Right, key);
  122. }
  1. /// <summary>
  2.     /// 二叉树
  3.     /// </summary>
  4.     public class BSTree
  5.     {
  6.         public int Data;
  7.         public BSTree Left;
  8.         public BSTree Right;
  9.     }

五、哈希查找(散列查找):

    根据给定的关键字来计算关键字在表中的地址。

    哈希技术是在记录的存储位置和记录的关键字之间建立一个确定的对应关系f,使得每个关键字key对应一个存储位置f(key)。查找时,根据这个确定的对应关系找到给定值的映射f(key),若查找集合中存在这个记录,则必定在f(key)的位置上。哈希技术既是一种存储方法,也是一种查找方法。

    六种哈希函数的构造方法:

1,直接定址法:
函数公式:f(key)=a*key+b (a,b为常数)
这种方法的优点是:简单,均匀,不会产生冲突。但是需要事先知道关键字的分布情况,适合查找表较小并且连续的情况。
2,数字分析法:
比如我们的11位手机号码“136XXXX7887”,其中前三位是接入号,一般对应不同运营公司的子品牌,如130是联通如意通,136是移动神州行,153是电信等。中间四们是HLR识别号,表示用户归属地。最后四们才是真正的用户号。
若我们现在要存储某家公司员工登记表,如果用手机号码作为关键字,那么极有可能前7位都是相同的,所以我们选择后面的四们作为哈希地址就是不错的选择。
3,平方取中法:
故名思义,比如关键字是1234,那么它的平方就是1522756,再抽取中间的3位就是227作为哈希地址。
4,折叠法:
折叠法是将关键字从左到右分割成位数相等的几个部分(最后一部分位数不够可以短些),然后将这几部分叠加求和,并按哈希表表长,取后几位作为哈希地址。
比如我们的关键字是9876543210,哈希表表长三位,我们将它分为四组,987|654|321|0 ,然后将它们叠加求和987+654+321+0=1962,再求后3位即得到哈希地址为962,哈哈,是不是很有意思。
5,除留余数法:
函数公式:f(key)=key mod p (p<=m)m为哈希表表长。
这种方法是最常用的哈希函数构造方法。

6,随机数法:
函数公式:f(key)= random(key)。
这里random是随机函数,当关键字的长度不等是,采用这种方法比较合适。


    两种哈希函数冲突解决方法:
我们设计得最好的哈希函数也不可能完全避免冲突,当我们在使用哈希函数后发现两个关键字key1!=key2,但是却有f(key1)=f(key2),即发生冲突。
方法一:开放定址法:
在开放定址法中,以发生冲突的Hash地址为自变量,通过某种冲突解决函数得到一个新的空闲的Hash地址方法有很多种,一下的是常用的两种:

 1.线性探查法:

    从发生冲突的地址(设为d)开始,依次探查d的下一个地址(当到达下标为m-1的Hash表表尾时,下一个探查的地址是表首地址0),知道找到一个空位置为止,当m>=n(n是表中的关键字的个数)时一定能找到一个空位置。

    线性探查法的公式为:Hi(key)=(H(key)+i) Mod m(1<=i<=m-1)

    线性探查法容易产生堆积问题,因为当连续出现若干同义词后,设第一个同义词占用单元d,这连续的若干同义词将占用Hash表的d,d+1,d+2等单元,此时,随后任何d+1,d+2等单元上的Hash映射都会由于前面的同义词堆积而产生冲突,尽管所有这些关键字并没有同义词。

2.平方探查法:

    设发生冲突的地址为d,则用平方探查法所得到的新的地址序列为:d+1^2,d-1^,d+2^2,d-2^2,...,平方探查法是一种较好的处理冲突的方法,可以避免出现堆积问题。它的缺点是不能探查到Hash表上的所有单元,但至少能探查到一半的单元。


方法二:链地址法:

    链地址法是把所有的同义词用单链表连接起来的方法,在这种方法中,hash表每个 单元中存放的不再是记录本身,而是相应同义词单链表的表头指针。

  1. /// <summary>
  2. /// hash表插入
  3. /// </summary>
  4. /// <param name="hastTable"></param>
  5. /// <param name="key"></param>
  6. public static void InsertHashTable(int[] hashTable, int key)
  7. {
  8. int hashAddress = Hash(hashTable, key);
  9. while (hashTable[hashAddress] != 0)
  10. {
  11. hashAddress = (++hashAddress + 1) % hashTable.Length;
  12. }
  13. hashTable[hashAddress] = key;
  14. }
  15. /// <summary>
  16. /// hash查找
  17. /// </summary>
  18. /// <param name="hashTable"></param>
  19. /// <param name="key"></param>
  20. public static int HashSearch(int[] hashTable, int key)
  21. {
  22. int hashAddress = Hash(hashTable, key);
  23. while (hashTable[hashAddress]!=key)
  24. {
  25. hashAddress = (hashAddress + 1) % hashTable.Length;
  26. if (hashTable[hashAddress] == 0 || hashAddress == Hash(hashTable, key)) return -1;
  27. }
  28. return hashAddress;
  29. }
  30. /// <summary>
  31. /// hash函数(除留余数法)
  32. /// </summary>
  33. /// <param name="hashTalbe"></param>
  34. /// <param name="data"></param>
  35. /// <returns></returns>
  36. private static int Hash(int[] hashTalbe, int data)
  37. {
  38. return data % hashTalbe.Length;
  39. }

猜你喜欢

转载自blog.csdn.net/weixin_41042404/article/details/80912037