首页 > 其他分享 >链表插入排序

链表插入排序

时间:2023-09-29 15:34:06浏览次数:42  
标签:Node head cur 插入排序 next 链表 nex top

  • 创建节点类
public class Node {

    int n;

    Node next;

}
  • 第1次推导
public class test {
    public static void main(String[] args) {
        // 新建节点
        Node node1 = new Node();
        node1.n=2;
        Node node2 = new Node();
        node2.n=5;
        Node node3 = new Node();
        node3.n=3;
        Node node4 = new Node();
        node4.n=1;

        // 链接
        Node head=new Node();
        head.next=node1;
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;

        // 长度
        int len=4;

        System.out.println("--- 初始链表 ---");
        // 遍历
        Node node=head;
        while (node.next != null){
            node=node.next;
            int num = node.n;
            System.out.print(num + ",");
        }

        Node top;
        Node cur;
        Node nex; // 存储临时变量
        int j; // 需要与前面几个节点比较

        /**
         * 第1轮
         * 与前面1个数比较
         */
        // [head][2][5][3][1]
        j = 1;  // 取出节点与前面1个节点比较
        // 取值[head][top][cur][nex][1]
        cur=head;
        top=cur;
        for (int i = 0; i < 1; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 2; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        top.next=nex;
        // [head][2][3][1]
        // [head][top][nex][1]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }

        // 当取出的数是最小的数时,赋值给第0位
        if(cur.n<head.next.n){
            cur.next=head.next;
            head.next=cur;
        }

        System.out.println();
        // 遍历
        Node cur1=head;
        while (cur1.next != null){
            cur1=cur1.next;
            int num = cur1.n;
            System.out.print(num + ",");
        }

        /**
         * 第2轮
         * 与前面2个数比较
         * 第1轮结束后 2,5,3,1,
         */
        j = 2;  // 取出节点与前面2个节点比较
        // 取值[head][][top][cur][nex]
        cur=head;
        top=cur;
        for (int i = 0; i < 2; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 3; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        top.next=nex;
        // [head][2][5][1]
        // [head][2][top][nex]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }
        // 第2轮第2次比较
        // [head][2][5][1]
        // 取值[head][top][nex][1]
        nex=head;
        top=nex;
        for (int i = 0; i < 1; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 2; i++) {  // 找nex节点
            nex=nex.next;
        }
        // [head][2][5][1]
        // [head][top][nex][1]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }

        // 当取出的数是最小的数时,赋值给第0位
        if(cur.n<head.next.n){
            cur.next=head.next;
            head.next=cur;
        }

        System.out.println();
        // 遍历
        Node cur2=head;
        while (cur2.next != null){
            cur2=cur2.next;
            int num = cur2.n;
            System.out.print(num + ",");
        }

        /**
         * 第3轮
         * 与前面3个数比较
         * 第2轮结束后 2,3,5,1,
         */
        j = 3;  // 取出节点与前面3个节点比较
        // 取值[head][][][top][cur]
        cur=head;
        top=cur;
        for (int i = 0; i < 3; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 4; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        top.next=nex;
        // [head][2][3][5]
        // [head][2][3][top]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }
        // 第3轮第2次比较
        // [head][2][3][5]
        // 取值[head][2][top][nex]
        nex=head;
        top=nex;
        for (int i = 0; i < 2; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 3; i++) {  // 找nex节点
            nex=nex.next;
        }
        // [head][2][3][5]
        // [head][2][top][nex]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }
        // 第3轮第3次比较
        // [head][2][3][5]
        // 取值[head][top][nex][5]
        nex=head;
        top=nex;
        for (int i = 0; i < 1; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 2; i++) {  // 找nex节点
            nex=nex.next;
        }
        // [head][2][3][5]
        // [head][top][nex][5]
        // 比较
        if(cur.n > top.n){
            // 取出的值比前一个值大
            top.next=cur;
            cur.next=nex;
        }else {
            // break;
        }

        // 当取出的数是最小的数时,赋值给第0位
        if(cur.n<head.next.n){
            cur.next=head.next;
            head.next=cur;
        }

        System.out.println();
        // 遍历
        Node cur3=head;
        while (cur3.next != null){
            cur3=cur3.next;
            int num = cur3.n;
            System.out.print(num + ",");
        }

    }
}
  • 模拟循环
public class test {
    public static void main(String[] args) {
        for (int i = 1; i < 4; i++) {
            for (int j = i; j > 0; j--) {
                if(j>=i){
                    System.out.println("执行了a方法");
                }else {
                    System.out.println("执行了c方法");
                }
            }
        }

    }
}

// a、b与i的关系
// i a b
// 1 1 2
// 2 2 3
// 3 3 4

// c、d与j的关系
// j c d
// 1 1 2
// 2 2 3
// 1 1 2
  • 最终完善
public class test {
    public static void main(String[] args) {
        // 新建节点
        Node node1 = new Node();
        node1.n=2;
        Node node2 = new Node();
        node2.n=5;
        Node node3 = new Node();
        node3.n=3;
        Node node4 = new Node();
        node4.n=1;

        // 链接
        Node head=new Node();
        head.next=node1;
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;

        // 长度
        int len=4;

        System.out.println("--- 初始链表 ---");
        // 遍历
        Node node=head;
        while (node.next != null){
            node=node.next;
            int num = node.n;
            System.out.print(num + ",");
        }

        // 排序
        Node top;
        Node cur = null;
        Node nex; // 存储临时变量
        int i; // 第几轮比较
        int j; // 需要与前面几个节点比较

        for (i = 1; i < len; i++) {
            for (j = i; j > 0; j--) {
                if(j>=i){
                    cur=head;
                    top=cur;
                    for (int a = 0; a < i; a++) {  // 找top节点
                        top=top.next;
                    }
                    for (int b = 0; b < i+1; b++) {  // 找cur节点
                        cur=cur.next;
                    }
                    nex=cur.next;
                    top.next=nex;
                    // 比较
                    if(cur.n > top.n){
                        // 取出的值比前一个值大
                        top.next=cur;
                        cur.next=nex;
                    }else {
                        // 本次循环结束
                        continue;
                    }
                }else {
                    nex=head;
                    top=nex;
                    for (int c = 0; c < j; c++) {  // 找top节点
                        top=top.next;
                    }
                    for (int d = 0; d < j+1; d++) {  // 找nex节点
                        nex=nex.next;
                    }
                    // 比较
                    if(cur.n > top.n){
                        // 取出的值比前一个值大
                        top.next=cur;
                        cur.next=nex;
                    }else {
                        continue;
                    }
                }
            }
            // 当取出的数是最小的数时,赋值给第0位
            if(cur.n<head.next.n){
                cur.next=head.next;
                head.next=cur;
            }
        }

        System.out.println();
        System.out.println("--- 排序后 ---");
        // 遍历
        Node cur1=head;
        while (cur1.next != null){
            cur1=cur1.next;
            int num = cur1.n;
            System.out.print(num + ",");
        }

    }
}

标签:Node,head,cur,插入排序,next,链表,nex,top
From: https://www.cnblogs.com/dogleftover/p/17737026.html

相关文章

  • 链表冒泡排序
    创建节点类publicclassNode{intn;Nodenext;}第1次推导publicclasstest{publicstaticvoidmain(String[]args){//新建节点Nodenode1=newNode();node1.n=9;Nodenode2=newNode();no......
  • 数组插入排序
    第1次推导publicclasstest{publicstaticvoidmain(String[]args){int[]ints={2,5,3,1,8,9};inttmp;//存储临时变量intj;//开始比较第几位的数//第1次//2,5,3,1,8,9j=1;//比较索引1的数......
  • Linux-----单链表
    Linux中实现链表//定义链表节点结构体structNode{intdata;//数据区structNode*next;//指向下一个地址域};//初始化链表为空格structNode*head=NULL;//插入元素到链表的末尾voidinsert(intdata){sturctNode*newNode=(structNode*)malloc(sizeof(struct......
  • Redis系列 - Redis底层数据结构(简单动态字符串(SDS)、链表、字典、跳跃表、整数集合
    转自:https://blog.csdn.net/u011485472/article/details/109460490Redis系列-Redis底层数据结构(简单动态字符串(SDS)、链表、字典、跳跃表、整数集合、压缩列表)简单动态字符串(simpledynamicstring,SDS)链表字典跳跃表整数集合压缩列表RedisObject在介绍Redis底......
  • 【从0学习Solidity】 10. 控制流,用solidity实现插入排序
    【从0学习Solidity】10.控制流,用solidity实现插入排序博主简介:不写代码没饭吃,一名全栈领域的创作者,专注于研究互联网产品的解决方案和技术。熟悉云原生、微服务架构,分享一些项目实战经验以及前沿技术的见解。关注我们的主页,探索全栈开发,期待与您一起在移动开发的世界中,不断进步和......
  • 关于数据结构单链表
    单链表作为最基础也是最常用的数据结构之一,在这门课程中占据着十分重要的地位。本文将对单链表这一章节的知识点进行总结,包括单链表的定义、基本操作、常见应用以及时间复杂度等方面。一、单链表的定义和基本操作单链表的定义:单链表由节点组成,每个节点包含数据和指向下一个节点的指......
  • 双向链表
    双向链表双向链表属于链表的一种,也叫双链表双向即是说它的链接方向是双向的,它由若干个节点组成,每个节点都包含下一个节点和上一个节点的指针,所以从双向链表的任意节点开始,都能很方便访问他的前驱结点和后继节点。 特点:1创建时无需指定长度。2比起单向,双向需要多一......
  • 合并K个排序链表
    合并K个排序链表描述合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。示例:输入:[ 1->4->5, 1->3->4, 2->6]输出:1->1->2->3->4->4->5->6题解使用heapq,但是ListNode没有比较函数,所以需要自行定义:ListNode.__lt__=lambdax,y:x.val<......
  • Linux的双链表复习—Apple的学习笔记
    一,前言   今天想把linux的双链表base代码拿来单片机用,于是看了下,结果有点混乱了。那么就画了个链表变化图,且做了实验进行巩固。二,分析链表头插方法主要是root然后添加t1,然后添加t2。那么链表的变化是RootRoot->t1Root->t2->t1如下图,R代表root头节点,1代表t1节点,2代表t2节点。......
  • (转)图的存储结构|邻接矩阵、邻接表、十字链表、邻接多重表、边集数组
    原文:https://juejin.cn/post/6996132859001962504?searchId=20230925172238C35D1579B2CBC3D2F78A7.4图的存储结构图的存储结构相较线性表与树来说就更加复杂了。首先,我们口头上说的“顶点的位置”或“邻接点的位置”只是一个相对的概念。其实从图的逻辑结构定义来看,图上任何......