1.顺序查找

  1. /**
  2. * 顺序查找算法
  3. * 从数据序列中第一个元素开始,从头到尾依次逐个查找。直到找到所要的数据或搜索完整个数据序列
  4. */
  5. public static int searchFun(int a[], int x) {
  6. for (int i = 0; i < a.length; i++) {
  7. if (a[i] == x) {
  8. return i;
  9. }
  10. }
  11. return -1;
  12. }

2.折半查找

  1. /**
  2. * 折半查找,又称为二分法查找。要求数据序列呈线性结构,也就是经过排序的。
  3. * 从数据的1/2处匹配,如果与需要查询的值相等,直接返回,
  4. * 如果大于需要查询的关键字,则在从前半部分的1/2处查找,也就是整个数据的1/4处;
  5. * 如果小于需要查询的关键字,则从后半部分的1/2处查找,也就是整个数据的3/4出;
  6. * 以此类推,直到找到关键字或将查找范围缩小到只剩下一个元素也不等于关键字.
  7. */
  8. public static int binarySearch(int a[], int x) {
  9. int midNum, lowNum = 0, highNum = a.length - 1;
  10. while (lowNum <= highNum) {
  11. midNum = (lowNum + highNum) / 2;
  12. if (a[midNum] == x) {
  13. return midNum;
  14. } else if (a[midNum] > x) {
  15. highNum = midNum - 1;
  16. } else if (a[midNum] < x) {
  17. lowNum = midNum + 1;
  18. }
  19. }
  20. return -1;
  21. }

3.链表结构中的查找算法

链表结构(下一章总结数据结构,其中有介绍什么是链表结构)也是一种顺序结构,只不过采用的是链式存储的方式。链表结构中的查找算法有点类似于顺序查找的思想

  1. /**
  2. * 定义数据结构中的元素
  3. */
  4. class DataElement {
  5. String key;
  6. String other_messages;
  7. }
  8. /**
  9. * 定义链表结构
  10. */
  11. class DataLinkedList {
  12. DataElement dataElement;
  13. DataLinkedList nextNode;
  14. //我们这里只做查找算法的示例,省去添加节点,删除节点等其他方法,只做查找
  15. /**
  16. * 链表结构中的查找算法
  17. * 一般来说可以通过关键字查询,从表头依次查找
  18. */
  19. DataLinkedList findData(DataLinkedList head, String key) {
  20. DataLinkedList temp = head;//保存表头指针
  21. while (temp != null) {//节点有效,进行查找
  22. DataElement date = temp.dataElement;
  23. if (date.key.equals(key)) {//节点的关键字与传入的关键字相同
  24. return temp;//返回该节点指针
  25. }
  26. temp = temp.nextNode;//处理下一节点
  27. }
  28. return null;//未找到,返回空指针
  29. }
  30. }

4. 二叉树的查找算法

就是遍历二叉树(下一章中会有介绍什么是树和二叉树),查找对应节点的key是不是匹配关键字

  1. /**
  2. * 定义二叉树结构
  3. */
  4. static class TreeDate {
  5. String data;
  6. TreeDate leftData;
  7. TreeDate rightData;
  8. /**
  9. * 二叉树查找算法
  10. * 遍历二叉树中的每一个结点,逐个比较数据。
  11. * @param treeNode 树结点,首次调用传入树的根结点
  12. * @param data 要查找的结点
  13. * @return TreeDate查找结果
  14. */
  15. TreeDate findData(TreeDate treeNode, String data) {
  16. if (treeNode == null) {
  17. return null;
  18. } else {
  19. if (treeNode.data.equals(data)) {
  20. return treeNode;
  21. }
  22. if (findData(treeNode.leftData, data) != null) {//递归查找左结点
  23. return findData(treeNode.leftData, data);
  24. }
  25. if (findData(treeNode.rightData, data) != null) {//递归查找右结点
  26. return findData(treeNode.rightData, data);
  27. }
  28. }
  29. return null;
  30. }
  31. }

5. 图结构中的查找算法

就是遍历图结构(下一章中会有介绍什么是图结构)进行查找

  1. /**
  2. * 定义图结构
  3. */
  4. static class Graph {
  5. static final int SIZE = 5;//图的最大顶点数
  6. static final int MAX_VALUE = 65535;//最大值
  7. public char[] vertex = new char[SIZE];//保存顶点信息(序号或字母)
  8. // int graphType;//图的类型(0.无向图;1.有向图)
  9. int vertexNum;//顶点的数量
  10. // int edgeNum;//边的数量
  11. int[][] edgeWeight = new int[SIZE][SIZE];//保存边的权值
  12. int[] isTraversal = new int[SIZE];//遍历标志
  13. /**
  14. * 深度遍历图
  15. * 从第n个顶点开始遍历
  16. *
  17. * @param graph 图
  18. * @param n 第n个顶点
  19. * @param key 需要查找的顶点
  20. */
  21. static void deepTraOne(Graph graph, int n, char key) {
  22. graph.isTraversal[n] = 1;//标记该顶点已经处理过
  23. if (graph.vertex[n] == key) {//就是要找的结点,如果是进行遍历而不是查找,删除这个判断输出所有结点数据即可
  24. System.out.printf("->%c", graph.vertex[n]);//输出结点数据
  25. return;
  26. }
  27. //添加处理结点的操作
  28. for (int i = 0; i < graph.vertexNum; i++) {
  29. if (graph.edgeWeight[n][i] != MAX_VALUE && graph.isTraversal[n] == 0) {
  30. deepTraOne(graph, i, key);//递归遍历
  31. }
  32. }
  33. }
  34. /**
  35. * 深度优先遍历
  36. *
  37. * @param graph 图
  38. * @param key 需要查找的顶点
  39. */
  40. static void findVertex(Graph graph, char key) {
  41. //清除各顶点遍历标志
  42. // for (int i = 0; i < graph.vertexNum; i++) {
  43. // graph.isTraversal[i] = 0;
  44. // }
  45. Arrays.parallelSetAll(graph.isTraversal, index -> 0);//java8的写法,作用同上面的for循环
  46. System.out.printf("深度优先遍历结点:");
  47. for (int i = 0; i < graph.vertexNum; i++) {
  48. if (graph.isTraversal[i] == 0) {//若该点未遍历
  49. deepTraOne(graph, i, key);//调用遍历函数
  50. }
  51. }
  52. System.out.printf("\n");
  53. }
  54. }