第四天:五大经典查找之---线性查找和二分查找
五大经典查找为:线性查找、二分查找、哈希查找、索引查找、二叉树查找
今天介绍的为:线性查找和二分查找---
首先,线性查找,很简单,就是遍历所有的元素,直到找到位置,直接上代码:
package cn.njupt.mdj; public class LineSearchDemo1 { public static void main(String args[]){ //给定要查找的元素 int[] arr = new int[]{1,0,8,2,5}; int result = linesearch(arr,0); if(result != -1){ System.out.println("0 元素 被查找到了,位置为:"+result); }else{ System.out.println("~~~~(>_<)~~~~ ,没有找到该元素"); } } //注,没有考虑重复元素,如果有重复则返回最后一个索引 public static int linesearch(int[] arr,int key){ int result = -1; for(int i=0;i<arr.length;i++){ if(key == arr[i]){ result = i; } } return result; } }
然后,介绍二分查找,二分查找的原理很简单,就是每次折半,既然是折半,所以开始要有折半的起点low,和终点high,然后开始不断折半比较:
注:二分查找,适合于 有序的顺序表,若无序则需要先排序。
package cn.njupt.mdj; public class LineSearchDemo1 { public static void main(String args[]){ //给定要查找的元素 int[] arr = new int[]{1,0,8,2,5}; //采用二分查找,先排序 bubbleSort(arr); for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); int result = binarySearch(arr, 3); if(result != -1){ System.out.println("0 元素 被查找到了,位置为:"+result); }else{ System.out.println("~~~~(>_<)~~~~ ,没有找到该元素"); } } //二分查找适合 顺序储存的有序表,所以查找之前必须让其有序 public static int binarySearch(int[] arr,int key){ //二分,所以先找到初始的 int low = 0; int hight = arr.length -1; while(low <= hight){ //首先取中间位置 int middle = (low + hight)/2; if(key == arr[middle]){ return middle; } if(arr[middle] > key){ hight = middle -1; }else{ low = middle + 1; } } return -1; } public static void bubbleSort(int[] arr){ for(int i=0;i<arr.length-1;i++){ for(int j=arr.length-1;j>i;j--){ if(arr[j] < arr[j-1]){ int temp = arr[j]; arr[j] = arr[j-1]; arr[j-1]=temp; } } } } }
补充昨日的:复习巩固线性表的操作 -- 顺序表的:--昨天的可能有点小问题:
package cn.njupt.mdj; class LineTable2{ private int capacity; private Object[] data = null; private int current; public LineTable2(int c){ if(c > 0){ capacity = c; }else{ capacity = 1; } data = new Object[capacity]; current = 0; } //添加元素,线性表,所以元素依次往后加 public void add(Object ele){ //判断空间是否已满了 if(isFull()){ throw new RuntimeException("元素空间已满了"); //可以采用扩进行。 }else{ //没有满,则添加元素 data[current++] = ele; } } public boolean isFull(){ return current == capacity; } //可以添加元素,那么线性表也可以删除,删除可以指定索引来删除 public Object remove(int index){ //首先判断元素是否已经空了 if(isEmpty()){ throw new RuntimeException("元素已经没有了"); }else{ //然后判断索引是否正确,即使有元素,索引也要在有元素处 if(index < 0 || index > current){ throw new RuntimeException("越界了"); }else{ //可以开始执行删除操作 Object temp = data[index]; for(int i=index;i<current-1;i++){ data[i] = data[i+1]; } //若是最后一个元素,即如果index=current-1处,则不能上面那样了,因为current+1.则越界了 //同时元素空间要空出一个来,所以是data[current-1] == null;而不是data[index] = null //因为data[index] = null;还是占据了位置的 data[current-1] = null; current--; return temp; } } } public boolean isEmpty(){ return current < 0; } //插入元素 public void insert(int index,Object ele){ //判断是否已满 if(isFull()){ throw new RuntimeException("元素空间已满了"); }else{ //判断索引,即使是插入,也不能破坏线性表的结构 if(index < 0 || index > current){ throw new RuntimeException("越界了"); }else{ //开始插入 for(int i= current;i>index;i--){ data[i] = data[i-1]; } data[index] = ele; current++; } } } //查找 public Object find(int index){ if(index < 0 || index >= current){ throw new RuntimeException("越界了"); }else{ return data[index]; } } //遍历 public String toString(){ if(isEmpty()){ return "[]"; }else{ StringBuffer s = new StringBuffer(); s.append("["); for(int i=0;i<current;i++){ s.append(data[i]+","); } s.append("]"); return s.toString(); } } public int getSize(){ return current; } } public class ArrayListDemo03 { public static void main(String args[]){ LineTable2 lineTable = new LineTable2(3); //添加 lineTable.add(1); lineTable.add(2); lineTable.add(3); //lineTable.add(4); //删除 System.out.println(lineTable.remove(1)); lineTable.add(4); //遍历 System.out.println(lineTable.toString()); //插入 lineTable.remove(1); lineTable.insert(1, 5); System.out.println(lineTable.toString()); //查找 System.out.println(lineTable.find(2)); //个数 System.out.println(lineTable.getSize()); } }
队列的巩固:
package cn.njupt.mdj.QueueDataStruct; //含有统计数据项字段的队列实现 class Queue5{ private int size; private Object[] data = null; private int head; private int tail; private int count; public Queue5(int s){ size = s; data = new Object[size]; head = 0; tail = -1; count = 0; } public void insert(Object ele){ if(isFull()){ throw new RuntimeException("空间已满,无法再添加元素"); }else{ if(tail == size-1){ tail = -1; } data[++tail] = ele; count++; } } public boolean isFull(){ return count == size; } public Object remove(){ if(isEmpty()){ throw new RuntimeException("元素已空没法再删除元素"); }else{ Object temp = data[head++]; if(head == size){ head = 0; } count--; return temp; } } public boolean isEmpty(){ return count == 0; } public int getSize(){ return count; } public Object peekHead(){ return data[head]; } } //不含有统计数据项字段的队列实现 class Queue6{ private int size; private Object[] data = null; private int head; private int tail; public Queue6(int s){ size = s+1; data = new Object[size]; head = 0; tail = -1; } public void insert(Object ele){ if(isFull()){ throw new RuntimeException("元素已满,不能在添加"); }else{ if(tail == size-1){ tail = -1; } data[++tail]=ele; } } public boolean isFull(){ return (tail+2 == head || (head+size-2 == tail)); } public Object remove(){ if(isEmpty()){ throw new RuntimeException("已经没有元素可以删除"); }else{ Object temp = data[head++]; if(head == size){ head = 0; } return temp; } } public boolean isEmpty(){ return (tail+1 == head || (head+size-1 == tail)); } public int getSize(){ if(tail >= head){ return tail-head+1; }else{ return (size-head)+(tail+1); } } public Object peekHead(){ return data[head]; } } public class TestDemo03 { public static void main(String args[]){ Queue6 queue6 = new Queue6(3); queue6.insert(1); queue6.insert(2); queue6.insert(3); //queue6.insert(4); queue6.remove(); queue6.remove(); queue6.remove(); queue6.insert(4); queue6.insert(5); while(!queue6.isEmpty()){ Object temp = queue6.remove(); System.out.print(temp); System.out.print(" "); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端