首页 > 其他分享 >链表冒泡排序

链表冒泡排序

时间:2023-09-29 15:22:22浏览次数:39  
标签:Node head cur top 冒泡排序 next 链表 nex

  • 创建节点类
public class Node {

    int n;

    Node next;

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

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

        // 长度
        int len = 4;

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

        Node top;
        Node cur;
        Node nex;

        /**
         * 初始为 9,6,2,5,
         * 第1轮比较开始
         */
//        top=head;
//        cur=top.next;
//        nex = cur.next;

        // [head][9][6][2][5]
        // [head][cur][nex][][]
        cur=head;
        top=cur;
        for (int i = 0; i < 0; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 1; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        }
        // 否则不做改变

        // [head][6][9][2][5]
        // [head][top][cur][nex][]
        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;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        }
        // 否则不做改变

        // [head][6][2][9][5]
        // [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;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        } // 6,2,5,9
        // 否则不做改变

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

        /**
         * 第1轮结束为 6,2,5,9,
         * 第2轮比较开始
         */
        // [head][6][2][5][9]
        // [head][cur][nex][][]
        cur=head;
        top=cur;
        for (int i = 0; i < 0; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 1; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        }
        // 否则不做改变

        // [head][2][6][5][9]
        // [head][top][cur][nex][]
        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;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        }
        // 否则不做改变

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

        /**
         * 第2轮结束为 2,5,6,9,
         * 第3轮比较开始
         */
        // [head][2][5][6][9]
        // [head][cur][nex][][]
        cur=head;
        top=cur;
        for (int i = 0; i < 0; i++) {  // 找top节点
            top=top.next;
        }
        for (int i = 0; i < 1; i++) {  // 找cur节点
            cur=cur.next;
        }
        nex=cur.next;
        if(cur.n>nex.n){
            cur.next= nex.next;
            top.next= nex;
            nex.next=cur;
        }
        // 否则不做改变

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

    }
}
  • 最终完善
public class test {
    public static void main(String[] args) {
        // 新建节点
        Node node1 = new Node();
        node1.n = 9;
        Node node2 = new Node();
        node2.n = 6;
        Node node3 = new Node();
        node3.n = 2;
        Node node4 = new Node();
        node4.n = 5;

        // 链接
        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 + ",");
        }

        // 排序
        // [head][9][6][2][5]
        //[head][node1][node2][node3][node4]
        Node top;
        Node cur;
        Node nex;
        for(int b = len-1; b>0; b--){  // 3轮比较
            int index=0;
            for(int a = b; a>0; a--){
                cur=head;
                top=cur;
                for (int i = 0; i < index; i++) {  // 找top节点
                    top=top.next;
                }
                for (int i = 0; i < index+1; i++) {  // 找cur节点
                    cur=cur.next;
                }
                nex=cur.next;
                if(cur.n>nex.n){
                    cur.next= nex.next;
                    top.next= nex;
                    nex.next=cur;
                }
                index++;
            }
        }

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

    }
}
  • 升序的情况下插入一条数据,链表依然为升序
public class test7 {
    public static void main(String[] args) {
        // 新建节点
        Node node1 = new Node();
        node1.n = 2;
        Node node2 = new Node();
        node2.n = 6;
        Node node3 = new Node();
        node3.n = 7;
        Node node4 = new Node();
        node4.n = 9;

        // 链接
        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 node5 = new Node();
        node5.n = 9;

        // [head][2][6][7][9]
        Node top;
        Node cur;
        for (int i = 0; i < len; i++) {    // 比较len轮
            // 每次找到要比较的节点和指定节点的前一个节点
            cur=head;
            top=cur;
            for (int j = 0; j < i; j++) {  // 找top节点
                top=top.next;
            }
            for (int j = 0; j < i+1; j++) {  // 找cur节点
                cur=cur.next;
            }
            if (node5.n<cur.n){  // 在某本节点前时
                // 插入位置
                top.next=node5;
                node5.next=cur;
                // 长度++
                len++;
                // 结束整个循环
                break;
            }else { // 大于等于node1
                // 不成立则continue,进入下一轮比较
                continue;
            }
        }
        Node end=head;
        for(int j=0; j<len; j++){  // 找到最后
            end=end.next;
        }
        // 当插入的值比数组中最大的值还大时,插入到最后
        if(node5.n>=end.n){
            end.next=node5;
            len++;
        }

        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,top,冒泡排序,next,链表,nex
From: https://www.cnblogs.com/dogleftover/p/17737020.html

相关文章

  • 数组冒泡排序
    第1次推导publicclasstest{publicstaticvoidmain(String[]args){int[]ints={6,5,9,5};inttmp;if(ints[0]>ints[1]){tmp=ints[0];ints[0]=ints[1];ints[1]=tmp;}......
  • 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底......
  • 关于数据结构单链表
    单链表作为最基础也是最常用的数据结构之一,在这门课程中占据着十分重要的地位。本文将对单链表这一章节的知识点进行总结,包括单链表的定义、基本操作、常见应用以及时间复杂度等方面。一、单链表的定义和基本操作单链表的定义:单链表由节点组成,每个节点包含数据和指向下一个节点的指......
  • 双向链表
    双向链表双向链表属于链表的一种,也叫双链表双向即是说它的链接方向是双向的,它由若干个节点组成,每个节点都包含下一个节点和上一个节点的指针,所以从双向链表的任意节点开始,都能很方便访问他的前驱结点和后继节点。 特点: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图的存储结构图的存储结构相较线性表与树来说就更加复杂了。首先,我们口头上说的“顶点的位置”或“邻接点的位置”只是一个相对的概念。其实从图的逻辑结构定义来看,图上任何......
  • leetcode21. 合并两个有序链表
    合并两个有序链表题目链接21.合并两个有序链表将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0......
  • 轻松掌握冒泡排序算法,值得收藏
    冒泡排序(BubbleSort)是一种简单的排序算法,其基本思想是多次遍历待排序的数组,每次比较相邻的两个元素,如果它们的顺序不正确就交换它们,直到整个数组有序为止。冒泡排序的基本步骤如下:从数组的第一个元素开始,比较相邻的两个元素,如果它们的顺序不正确就交换它们。重复步骤1,直到遍历......