单链表冒泡排序

单链表的存储原理,简单易懂,head为头节点,他不存放任何的数据,只是充当一个指向链表中真正存放数据的第一个节点的作用,而每个节点中都有一个next引用,指向下一个节点,就这样一节一节往下面记录,直到最后一个节点,其中的next指向null。

而双链表相对于单链表的存储区别在节点不仅有一个next引用,还有一个pre引用指向上一个节点。

数组的优点:
随机访问性强
查找速度快


数组的缺点:
插入和删除效率低
可能浪费内存
内存空间要求高,必须有足够的连续内存空间。
数组大小固定,不能动态拓展


链表的优点:
插入删除速度快
内存利用率高,不会浪费内存
大小没有固定,拓展很灵活。


链表的缺点:
不能随机查找,必须从第一个开始遍历,查找效率低。

之前看了别人使用单链表实现的冒泡排序,发现有的好像实现有点问题,自己琢磨了下实现,跑了一下发现简单功能都能实现。代码及注释如下:

package com.example.javalib.sort.quicksort;

/**
 * 普通冒泡排序  链表冒泡排序
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] array = new int[]{4, 6, 3, 15, 7, 1, 30, 40, 18, 5, 1, 14, 100, 494, 45, 19, 18, 263, 173};
        //快速排序
//        normalSort(array);
        Node head = arrayToSingleLinked(array); // 链表初始节点
        singleLinkedSort(head, array.length);
    }

    /**
     * 普通的冒泡排序
     *
     * @param array
     */
    public static void normalSort(int[] array) {
        if (array == null) {
            return;
        }
        int size = array.length;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        for (int a : array) {
            System.out.print(a + " ");
        }
    }

    /**
     * 单链表冒泡排序
     */

    public static class Node<T extends Comparable<? extends T>> {
        int value;
        Node next; //下一个节点的引用
        Node pre;  // 上一个节点的引用

        public Node() {
        }

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * 将数组元素依次加入到链表结构中
     *
     * @param array
     * @return
     */
    public static Node arrayToSingleLinked(int[] array) {
        Node head = new Node();
        Node pre = head;
        Node next = null;
        Node cur = null;
        for (int i = 0; i < array.length; i++) {
            cur = next;
            if (i < array.length - 1) {
                next = new Node(array[i + 1]);
            } else {
                next = null;
            }
            if (i == 0) {
                cur = new Node(array[i]);
                pre.next = cur;
            }
            cur.next = next;
            pre = cur;
        }
//        printList(head);
        return head;
    }

    /**
     * 单链表排序
     *
     * @param head 链表初始节点
     */
    public static void singleLinkedSort(Node head, int size) {
        int j = 0;
        Node current = head.next; // 排序开始的首个节点 也就是我们需要排序的数组的第一个元素

        Node pre = head;   //开始排序的首节点的上一个节点
        Node next = current.next;  // 开始排序的首节点的下一个节点
        Node last = null;
        int endValue = Integer.MIN_VALUE / 50;
        for (int i = 0; i < size; i++) {
            //这个while循环只是一轮比较 将最大值冒泡到最上面
            //当开始排序的首节点的下一个节点不为空,并且开始节点的值不等于上一次排序的最大值才继续while循环排序,
            //因为如果开始节点的值等于上一次排序的值就意味着两个相同的值相碰,并且已经排序在最前面,也就是最小值了
            //那么 排序结束
            while (current.next != last && current.value != endValue) {
                if (current.value > next.value) {  //当前节点的值大于下一个节点的值  此时需要交换
                    int temp = current.value;
                    current.value = next.value;
                    next.value = temp;
                    next = next.next;
                    current = current.next;
                } else {
                    pre = current;
                    next = next.next;
                    current = current.next;
                }
                System.out.println("第" + ++j + "轮排序:current" + current.value + "next" + (next == null ? -1 : next.value));
                if (next == null || current.next.value == endValue)
                    break; //如果当前节点的下一个节点value值等于上一轮排序的最大value值 那么这一轮排序也停止

            }
            System.out.println("第 " + j + "轮结束: currentValue:" + current.value);
            endValue = current.value;

            printList(head);
            current = head.next;   // 每一轮while循环结束之后都重置current为head节点的下一个节点,也就是数组元素的第一个
            next = current.next;
            if(current.value == endValue) break;
        }


    }

    /**
     * 打印链表各节点的值
     * @param head
     */
    public static void printList(Node head) {
        String str = "";
        Node node = head.next;
        while (node.next != null) {
            str += node.value + " ";
            node = node.next;
        }
        str += node.value;
        System.out.println("链表中个节点的值为:" + str + "");
    }
}

控制台输出打印出每一轮排序的情况如下:

第1次排序:current6next3
第2次排序:current6next15
第3次排序:current15next7
第4次排序:current15next1
第5次排序:current15next30
第6次排序:current30next40
第7次排序:current40next18
第8次排序:current40next5
第9次排序:current40next1
第10次排序:current40next14
第11次排序:current40next100
第12次排序:current100next494
第13次排序:current494next45
第14次排序:current494next19
第15次排序:current494next18
第16次排序:current494next263
第17次排序:current494next173
第18次排序:current494next-1
第 1轮结束: currentValue:494
链表中个节点的值为:4 3 6 7 1 15 30 18 5 1 14 40 100 45 19 18 263 173 494
第19次排序:current4next6
第20次排序:current6next7
第21次排序:current7next1
第22次排序:current7next15
第23次排序:current15next30
第24次排序:current30next18
第25次排序:current30next5
第26次排序:current30next1
第27次排序:current30next14
第28次排序:current30next40
第29次排序:current40next100
第30次排序:current100next45
第31次排序:current100next19
第32次排序:current100next18
第33次排序:current100next263
第34次排序:current263next173
第35次排序:current263next494
第 2轮结束: currentValue:263
链表中个节点的值为:3 4 6 1 7 15 18 5 1 14 30 40 45 19 18 100 173 263 494
第36次排序:current4next6
第37次排序:current6next1
第38次排序:current6next7
第39次排序:current7next15
第40次排序:current15next18
第41次排序:current18next5
第42次排序:current18next1
第43次排序:current18next14
第44次排序:current18next30
第45次排序:current30next40
第46次排序:current40next45
第47次排序:current45next19
第48次排序:current45next18
第49次排序:current45next100
第50次排序:current100next173
第51次排序:current173next263
第 3轮结束: currentValue:173
链表中个节点的值为:3 4 1 6 7 15 5 1 14 18 30 40 19 18 45 100 173 263 494
第52次排序:current4next1
第53次排序:current4next6
第54次排序:current6next7
第55次排序:current7next15
第56次排序:current15next5
第57次排序:current15next1
第58次排序:current15next14
第59次排序:current15next18
第60次排序:current18next30
第61次排序:current30next40
第62次排序:current40next19
第63次排序:current40next18
第64次排序:current40next45
第65次排序:current45next100
第66次排序:current100next173
第 4轮结束: currentValue:100
链表中个节点的值为:3 1 4 6 7 5 1 14 15 18 30 19 18 40 45 100 173 263 494
第67次排序:current3next4
第68次排序:current4next6
第69次排序:current6next7
第70次排序:current7next5
第71次排序:current7next1
第72次排序:current7next14
第73次排序:current14next15
第74次排序:current15next18
第75次排序:current18next30
第76次排序:current30next19
第77次排序:current30next18
第78次排序:current30next40
第79次排序:current40next45
第80次排序:current45next100
第 5轮结束: currentValue:45
链表中个节点的值为:1 3 4 6 5 1 7 14 15 18 19 18 30 40 45 100 173 263 494
第81次排序:current3next4
第82次排序:current4next6
第83次排序:current6next5
第84次排序:current6next1
第85次排序:current6next7
第86次排序:current7next14
第87次排序:current14next15
第88次排序:current15next18
第89次排序:current18next19
第90次排序:current19next18
第91次排序:current19next30
第92次排序:current30next40
第93次排序:current40next45
第 6轮结束: currentValue:40
链表中个节点的值为:1 3 4 5 1 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第94次排序:current3next4
第95次排序:current4next5
第96次排序:current5next1
第97次排序:current5next6
第98次排序:current6next7
第99次排序:current7next14
第100次排序:current14next15
第101次排序:current15next18
第102次排序:current18next18
第103次排序:current18next19
第104次排序:current19next30
第105次排序:current30next40
第 7轮结束: currentValue:30
链表中个节点的值为:1 3 4 1 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第106次排序:current3next4
第107次排序:current4next1
第108次排序:current4next5
第109次排序:current5next6
第110次排序:current6next7
第111次排序:current7next14
第112次排序:current14next15
第113次排序:current15next18
第114次排序:current18next18
第115次排序:current18next19
第116次排序:current19next30
第 8轮结束: currentValue:19
链表中个节点的值为:1 3 1 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第117次排序:current3next1
第118次排序:current3next4
第119次排序:current4next5
第120次排序:current5next6
第121次排序:current6next7
第122次排序:current7next14
第123次排序:current14next15
第124次排序:current15next18
第125次排序:current18next18
第126次排序:current18next19
第 9轮结束: currentValue:18
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第127次排序:current1next3
第128次排序:current3next4
第129次排序:current4next5
第130次排序:current5next6
第131次排序:current6next7
第132次排序:current7next14
第133次排序:current14next15
第134次排序:current15next18
第 10轮结束: currentValue:15
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第135次排序:current1next3
第136次排序:current3next4
第137次排序:current4next5
第138次排序:current5next6
第139次排序:current6next7
第140次排序:current7next14
第141次排序:current14next15
第 11轮结束: currentValue:14
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第142次排序:current1next3
第143次排序:current3next4
第144次排序:current4next5
第145次排序:current5next6
第146次排序:current6next7
第147次排序:current7next14
第 12轮结束: currentValue:7
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第148次排序:current1next3
第149次排序:current3next4
第150次排序:current4next5
第151次排序:current5next6
第152次排序:current6next7
第 13轮结束: currentValue:6
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第153次排序:current1next3
第154次排序:current3next4
第155次排序:current4next5
第156次排序:current5next6
第 14轮结束: currentValue:5
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第157次排序:current1next3
第158次排序:current3next4
第159次排序:current4next5
第 15轮结束: currentValue:4
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第160次排序:current1next3
第161次排序:current3next4
第 16轮结束: currentValue:3
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494
第162次排序:current1next3
第 17轮结束: currentValue:1
链表中个节点的值为:1 1 3 4 5 6 7 14 15 18 18 19 30 40 45 100 173 263 494

猜你喜欢

转载自blog.csdn.net/qq_24505921/article/details/85158243