miluframe({ /*个人链接地址*/ Youself:'https://www.cnblogs.com/miluluyo/', /*导航栏信息*/ custom:[{ name:'留言板', link:'https://www.cnblogs.com/miluluyo/p/11578505.html', istarget:false },{ name:'技能树', link:'https://miluluyo.github.io/', istarget:true }], /*自己的友链页面后缀*/ Friends_of_the:'p/11633791.html', /*自己的友链信息*/ resume:{ "name":"麋鹿鲁哟", "link":"https://www.cnblogs.com/miluluyo", "headurl":"https://images.cnblogs.com/cnblogs_com/elkyo/1558759/o_o_my.jpg", "introduction":"大道至简,知易行难。" }, /*友链信息*/ unionbox:[{ "name":"麋鹿鲁哟", "introduction":"生活是没有标准答案的。", "url":"https://www.cnblogs.com/miluluyo", "headurl":"https://images.cnblogs.com/cnblogs_com/elkyo/1558759/o_o_my.jpg" },{ "name":"麋鹿鲁哟的技能树", "introduction":"大道至简,知易行难。", "url":"https://miluluyo.github.io/", "headurl":"https://images.cnblogs.com/cnblogs_com/elkyo/1558759/o_o_my.jpg" }], /*点击页面时候的弹出文本显示*/ clicktext:new Array("ヾ(◍°∇°◍)ノ゙加油哟~ ——麋鹿鲁哟","生活是没有标准答案的。 ——麋鹿鲁哟"), /*github链接*/ githuburl:'https://github.com/miluluyo' })

2022.07.15 第三小组 陈迪 学习笔记

1、算法:

1.1、数据结构:

​ 1、数组是最基本的数据结构,是一张线性表
​ (数据元素之间一对一,除了第一个和最后一个,其余元素首尾相连)
​ 2、链表(单向链表:只维护下一个元素。双向链表:上下元素都维护)
​ 3、树
​ 4、图

例题1:

​ 找出一个数在数组中的位置
​ 在数组中是否存在,存在,返回下标,不存在,返回-1
​ 如果找到了,则把下标保存起来,显示你要找的数是xxx,在目标数组中的下标是xxx.
​ 如果找不到,则显示要找的数字xxx,在目标数组中不存在

int []arr=new int[]{1,23,54,67,43,23};
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个数");
int num=sc.nextInt();
for (int i = 0; i < arr.length; i++) {
    if (arr[i]==num){
        System.out.println("你要找的数是"+num+",在目标数组中的下标是"+i);
        return;
    }
}
System.out.println("你要找的数是"+num+",在目标数组中是不存在的。");

​ 写代码思路分析
​ 1,先完成需求要求的功能
​ 2、根据结果进行优化处理
​ 3、代码重构
​ 4、提升效率

1.2、查找算法

​ 1、 线性查找:简单,便于理解
​ 2、二分法查找:找数字,数字必须有顺序

例题2:

//二分法查找
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字");
int num = sc.nextInt();
int left = 0;
int right = arr.length - 1;
if (num < arr[left] || num > arr[right]) {
    System.out.println(num + "不存在");
} else {
    int tes = -1;
    while (left <= right) {
        int middle = (left + right) / 2;
        if (arr[middle] == num) {
            tes = middle;
            break;
        } else if (arr[middle] > num) {
            right = middle - 1;
        } else {
            left = middle + 1;
        }
    }
    System.out.println("您要找的数字是" + num + ",在目标函数的下标是" + tes);
}

2、排序

八大排序算法:

​ 1、冒泡排序
​ 2、快速排序
​ 3、插入排序
​ 4、选择排序
​ 5、希尔排序
​ 6、堆排序
​ 7、归并排序
​ 8、桶排序

2.1、冒泡排序:

冒泡排序思路分析:
1.用第一个数和后面的一一比较,大了换位
2.第一轮比完,从头开始继续比,最后一位不用比
3.以此类推,每次越比越少,直到次数变为0
4.嵌套循环

例题3:

int[] arr = new int[]{51, 23, 54, 67, 43, 24};
for (int i = 0; i < arr.length - 1; i++) {
    for (int j = 0; j < arr.length - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
            int temp = 0;
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
    System.out.print("第" + (i + 1) + "轮的结果是:");
    System.out.print("[");
    for (int arr1 : arr) {
        System.out.print(arr1 + ",");
    }
    System.out.println("]");
}

2.2、选择排序

首先找到数组中最小的元素,其次,将它和数组中的第一个元素交换位置(如果第一个元素最小,自己和自己交换),其次,在剩下的元素中找到最小元素和第二个位置的元素进行排序,依此类推

例题4:

int[] arr = new int[]{32, 41, 6, 12, 5, 1, 25, 9};
for (int i = 0; i < arr.length - 1; i++) {
    int minIndex = i;
    for (int j = i + 1; j < arr.length; j++) {
        if (arr[minIndex] > arr[j]) {
            minIndex = j;
        }
    }
    int temp = arr[minIndex];
    arr[minIndex] = arr[i];
    arr[i] = temp;
    System.out.print("第" + (i + 1) + "轮的结果是");
    for (int a : arr) {
        System.out.print(a + "、");
    }
    System.out.println();
}

2.3插入排序

int[] arr = new int[]{43, 32, 76, 2, 54, 3, 64, 4};
//定义参照物
int a;
for (int i = 0; i < arr.length - 1; i++) {
    a = arr[i + 1];
    //定义上一个元素的下标
    int b = i;
    //当上一个数的下标有效不能小于0
    //还要保证当前是数比上一个小
    //这时候。才能让当前数向前移位
    while (b >= 0 && a < arr[b]) {
        arr[b + 1] = arr[b];
        b--;
    }
    arr[b+1]=a;
    for (int c:arr){
        System.out.print(c+",");
    }
    System.out.println();
}

2.4数组的反转:

​ 思路1:
​ 创建一个等长的数组
​ 把当前输入的每一个元素倒着添加到新数组里
​ 新数组赋值给老数组
​ 新建数组会占内存

int[] arr = new int[]{43, 32, 76, 2, 54, 3, 64, 4};
int[] newArr = new int[arr.length];

        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[arr.length - i - 1];
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = newArr[i];
            System.out.print(arr[i] + ",");
        }

3、遍历整型数组/遍历字符串型数组

public static void mm(int[]a){
    for (int i : a) {
        System.out.println(i+" ");
    }
    }
public static void md(String[]a){
    for ( String i : a) {
        System.out.println(i+" ");
    }
    }

心得体会:

今天学习了算法,排序,理解起来较为困难,可以看懂,但是自己编写还有一定差距,晚上多练习,多请教同学,争取加深理解,早点掌握学到的知识,并加以运用。

posted @   jinjidecainiao  阅读(28)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
@media only screen and (max-width: 767px){ #sidebar_search_box input[type=text]{width:calc(100% - 24px)} } L2Dwidget.init({ "model": { jsonPath: "https://unpkg.com/live2d-widget-model-hijiki/assets/hijiki.model.json", "scale": 1 }, "display": { "position": "left", "width": 100, "height": 200, "hOffset": 70, "vOffset": 0 }, "mobile": { "show": true, "scale": 0.5 }, "react": { "opacityDefault": 0.7, "opacityOnHover": 0.2 } }); window.onload = function(){ $("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") } 参数说明 名称 类型 默认值/实例 描述Youself 字符串 https://www.cnblogs.com/miluluyo/ 个人博客园首链接 custom 数组 [{ name:'相册', link:'https://www.cnblogs.com/elkyo/gallery.html', istarget:false },{ name:'技能树', link:'https://miluluyo.github.io/', istarget:true },{ name:'留言板', link:'https://miluluyo.github.io/p/11578505.html', istarget:false }] 导航信息 name 导航名 link 导航链接 istarget true跳转到新页面上,false当前页面打开 Friends_of_the 字符串 11633791 友链文章的后缀名,若字符串为空则不显示友链 resume 对象 { "name":"麋鹿鲁哟", "link":"https://www.cnblogs.com/miluluyo/", "headurl":"https://images.cnblogs.com/cnblogs_com/ elkyo/1558759/o_o_my.jpg", "introduction":"大道至简,知易行难。" } 自己的友链信息 name 导航名 link 导航链接 headurl 头像 introduction 语录 unionbox 数组 [{ "name":"麋鹿鲁哟", "introduction":"生活是没有标准答案的。", "url":"https://www.cnblogs.com/miluluyo", "headurl":"https://images.cnblogs.com/cnblogs_com/ elkyo/1558759/o_o_my.jpg" },{ "name":"麋鹿鲁哟的技能树", "introduction":"大道至简,知易行难。", "url":"https://miluluyo.github.io/", "headurl":"https://images.cnblogs.com/cnblogs_com/ elkyo/1558759/o_o_my.jpg" }] 友链数组 name 昵称 introduction 标语 url 链接地址 headurl 头像地址 clicktext 新数组 new Array("ヾ(◍°∇°◍)ノ゙加油哟~ ——麋鹿鲁哟", "生活是没有标准答案的。 ——麋鹿鲁哟"), 点击页面时候的弹出显示 githuburl 字符串 https://github.com/miluluyo github链接
点击右上角即可分享
微信分享提示