展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

链表插入排序

  • 创建节点类
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 + ",");
        }

    }
}
posted @ 2023-09-29 15:22  DogLeftover  阅读(12)  评论(0编辑  收藏  举报