From 2b13c1eae4a15b163225900fd71eec5e28a4109f Mon Sep 17 00:00:00 2001 From: sawyergore <3077528351@qq.com> Date: Fri, 22 Jul 2022 22:33:01 +0800 Subject: [PATCH] =?UTF-8?q?#002=E7=BA=BF=E6=80=A7=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=92=8C=E4=BA=8C=E5=88=86=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/sort/TestBinarySearch.java | 49 +++++++++++++++++++ .../src/main/java/sort/TestSearch.java | 28 +++++++++++ .../com/example/h2db/H2dbApplication.java | 1 - .../java/com/example/h2db/entity/User.java | 4 -- 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 data_structure/src/main/java/sort/TestBinarySearch.java create mode 100644 data_structure/src/main/java/sort/TestSearch.java diff --git a/data_structure/src/main/java/sort/TestBinarySearch.java b/data_structure/src/main/java/sort/TestBinarySearch.java new file mode 100644 index 0000000..76fdac7 --- /dev/null +++ b/data_structure/src/main/java/sort/TestBinarySearch.java @@ -0,0 +1,49 @@ +package sort; + +/** + * 二分查找,对于目标数组必须有序 + * @author Sawyer + * @date 2022/7/22 + */ +public class TestBinarySearch { + public static void main(String[] args) { + //目标数组 + int [] arr = new int []{1,2,3,4,5,6,7,8,9}; + //目标元素 + int target = 8; + //目标元素下标 + int index = -1; + //记录开始位置 + int begin = 0; + //记录结束位置 + int end = arr.length-1; + //记录中间位置 + int mid = (begin+end)/2; + int i =0; + //循环查找 + while (itarget){ + //把结束位置调整到中间位置前一个位置 + end = mid-1; + //中间这个元素比目标元素小 + }else { + //把开始位置调整到中间位置的后一个位置 + begin = mid+1; + } + //取出新的中间位置 + mid=(begin+end)/2; + i++; + + } + } + System.out.println("index:"+index); + + } +} diff --git a/data_structure/src/main/java/sort/TestSearch.java b/data_structure/src/main/java/sort/TestSearch.java new file mode 100644 index 0000000..36dd441 --- /dev/null +++ b/data_structure/src/main/java/sort/TestSearch.java @@ -0,0 +1,28 @@ +package sort; + +/** + * 线性查找 + * @author Sawyer + * @date 2022/7/22 + */ +public class TestSearch { + public static void main(String[] args) { + /* + 目标数组 + */ + int[] arr = new int[]{1,2,3,4,5,6,7}; + //目标元素 + int target=3; + //目标元素所在下标 + int index=-1; + for (int j : arr) { + if (j == target) { + index = target; + break; + } + } + //打印目标元素下标 + System.out.println("index"+index); + } + +} diff --git a/h2db/src/main/java/com/example/h2db/H2dbApplication.java b/h2db/src/main/java/com/example/h2db/H2dbApplication.java index 1cea5e2..0de1fec 100644 --- a/h2db/src/main/java/com/example/h2db/H2dbApplication.java +++ b/h2db/src/main/java/com/example/h2db/H2dbApplication.java @@ -1,7 +1,6 @@ package com.example.h2db; import org.mybatis.spring.annotation.MapperScan; -import org.mybatis.spring.annotation.MapperScans; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/h2db/src/main/java/com/example/h2db/entity/User.java b/h2db/src/main/java/com/example/h2db/entity/User.java index 0e47f48..1082c07 100644 --- a/h2db/src/main/java/com/example/h2db/entity/User.java +++ b/h2db/src/main/java/com/example/h2db/entity/User.java @@ -1,9 +1,5 @@ package com.example.h2db.entity; -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** -- 2.25.1