• java七大非线性时间比较类排序


     java七大非线性时间比较排序时间、空间复杂度的比较

    一、冒泡排序

    一、基本思想

    冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法
    它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从大到小、首字母从Z到A)错误就把他们交换过来。
    走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素列已经排序完成。
    这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”。

    二、代码实现思路

    比较相邻的两个元素,如果元素位置不正确(前面的比后面的大),则互换两个元素,直到将最大的元素换到数列的底部。

    每一次外层循环都将从待排元素中将最大元素换到这些元素的底部,待排元素数量减一,直到排为有序集合。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 冒泡排序,时间复杂度为N^2,空间复杂度为1
     * @author: wsq
     * @date: 2020年6月6日下午5:26:03
     */
    public class BubbleSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 6, 8, 2, 7, 4, 3, 9 };
            // 调用快排的方法
            bubbleSort(array);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void bubbleSort(int[] array) {
            // 用来交换数据
            int temp;
            // 外层排序,一次为一趟的比较
            for (int i = 0; i < array.length - 1; i++) {
                for (int j = 0; j < array.length - 1 - i; j++) {
                    // 前后的数据进行对比,如果前面的数据比后面的大,那么进行交换
                    if (array[j] > array[j + 1]) {
                        temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
        }
    }

    二、选择排序

    一、基本思想

    选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。

    二、代码实现思路

    选择排序说白了,就是每次循环都找出最小元素的下标,放到已排序元素的末尾,直到没有元素可以进行排序。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 选择排序,时间复杂度为:n^2,空间夫复杂度为1
     * @author: wsq
     * @date: 2020年6月6日下午5:42:24
     */
    public class SelectionSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 8, 9, 2, 4, 10, 6, 7 };
            // 调用选择排序
            selection(array);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void selection(int[] array) {
            // 按顺序取出数组中的第一个数字
            for (int i = 0; i < array.length - 1; i++) {
                // 获取最小的元素的下标
                int minIndex = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (array[minIndex] > array[j]) {
                        minIndex = j;
                    }
                }
                // 将i指向的当前位置的元素与最小元素互换
                // 判断是否需要进行互换
                if (minIndex != i) {
                    int temp = array[i];
                    array[i] = array[minIndex];
                    array[minIndex] = temp;
                }
            }
        }
    }

    三、插入排序

    一、基本思想

    插入排序,一般也被称为直接插入排序。对于少量元素的排序,它是一个有效的算法 

    插入排序是一种最简单的排序方法,它的基本思想是将一个记录插入到已经排好序的有序表中,从而一个新的、记录数增1的有序表。

    在其实现过程使用双层循环,外层循环对除了第一个元素之外的所有元素,内层循环对当前元素前面有序表进行待插入位置查找,并进行移动。

    二、代码实现思路

    插入排序就是从第二个元素开始,每次循环向前面找比自己小的元素,如果有就插入到它的后面,如果没有,则前面的元素后移,继续和前面的元素比较,直到放到合适的位置。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 插入排序,平均时间复杂度:n^2,空间复杂度1
     * @author: wsq
     * @date: 2020年6月6日下午6:36:16
     */
    public class InsertionSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 6, 8, 9, 2, 7, 4, 3, 9 };
            // 调用插入排序
            insertionSort(array);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void insertionSort(int[] array) {
            // 遍历外侧的下标大于1所有元素
            for (int i = 1; i < array.length; i++) {
                // 取出需要选择位置的元素
                int max = array[i];
                int j = i - 1;
                // 找出第一个值小于此元素的元素
                while (j >= 0 && max < array[j]) {
                    // 将元素后移
                    array[j + 1] = array[j];
                    j--;
                }
                // 将目标元素放到合适的位置上
                array[j + 1] = max;
            }
        }
    }

     四、希尔排序

    一、基本思想

    希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。
    希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序。
    随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。

    二、代码实现思路

    首先,采用循环让每次排序根据不同的增量进行,每次排序都采用插入排序的方法,增量每次都变成原来的二分之一,直到最后变成1。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 希尔排序,时间复杂度:nlog2 n,空间复杂度:1
     * @author: wsq
     * @date: 2020年6月6日下午9:25:27
     */
    public class ShellSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 6, 8, 9, 2, 7, 4, 3, 9 };
            // 调用希尔排序
            shellSort(array);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void shellSort(int[] array) {
            // 计算最小增量
            int gap = array.length / 2;
            while (gap > 0) {
                // 插入排序
                for (int i = gap; i < array.length; i++) {
                    int temp = array[i];
                    int j = i - gap;
                    while (j >= 0 && temp < array[j]) {
                        array[j + gap] = array[j];
                        j = j - gap;
                    }
                    array[j + gap] = temp;
                }
                // 计算最小增量
                gap /= 2;
            }
        }
    }

    五、归并排序

    一、基本思想

    归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。

    将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。

    若将两个有序表合并成一个有序表,称为二路归并。归并排序是一种稳定的排序方法。

    二、代码实现思路

    归并排序实际就是将数组通过递归不停的分为两个部分,然后再把对应的每两部分进行拼接(拼接时进行对比,判断谁小是在前,其中一个集合为空了之后,就不再对比,只放另一个集合的元素)

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 归并排序,时间复杂度:nlogn,空间复杂度:n
     * @author: wsq
     * @date: 2020年6月7日上午10:05:03
     */
    public class MergeSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 8, 9, 2, 4, 10, 6, 7 };
            // 调用归并排序
            int[] result = mergeSort(array);
            // 打印数组
            System.out.println(Arrays.toString(result));
        }
    
        public static int[] mergeSort(int[] array) {
            // 用于判断左右集合是不是不可以继续进行二次拆分,退出递归
            if (array.length < 2) {
                return array;
            }
            // 左数组
            int[] left = Arrays.copyOfRange(array, 0, array.length / 2);
            // 右数组
            int[] right = Arrays.copyOfRange(array, array.length / 2, array.length);
            return merge(mergeSort(left), mergeSort(right));
    
        }
    
    // 左右数组进行对比和并
        public static int[] merge(int[] left, int[] right) {
            int[] result = new int[left.length + right.length];
            for (int i = 0, j = 0, index = 0; index < result.length; index++) {
                if (i >= left.length) {
                    result[index] = right[j++];
                } else if (j >= right.length) {
                    result[index] = left[i++];
                } else if (left[i] > right[j]) {
                    result[index] = right[j++];
                } else {
                    result[index] = left[i++];
                }
            }
            return result;
        }
    }

    六、快速排序

    一、基本思想

    通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列

    二、代码实现思路

    快速排序,实际就是任意选择一个基准,将比他小的元素放置在它的左边,比他大的元素放置在它的右边,左右两边的指针同时移动,直到相遇,此时,基准元素的位置为最后位置。

    然后,基准元素的位置不动。将基准元素左边和右边的数组,看成两个不同的数组,再次寻找基准元素进行排序,把基准元素放到合适的位置上。一直递归操作即可,直到数组元素的个数都变为1,结束。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 快速排序,时间复杂度:nlogn,空间复杂度:1
     * @author: wsq
     * @date: 2020年6月7日上午11:05:32
     */
    public class QuickSort {
        public static void main(String[] args) {
            int[] array = new int[] { 5, 10, 9, 7, 8, 2, 1, 3, 4, 6 };
            // 调用快速排序
            quickSort(array, 0, array.length - 1);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void quickSort(int[] array, int low, int high) {
            // 最小数组
            if (low < high) {
                int i = low;
                int j = high;
                int middle = array[low];
                // 进行判断,并且互换
                while (i < j) {
                    // 寻找出左边的不合适元素
                    while (i < j && array[j] >= middle) {
                        j--;
                    }
                    // 寻找出右边的不合适元素
                    while (i < j && array[i] <= middle) {
                        i++;
                    }
                    if (i < j) {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
                // 将基准和最后指针位置的数进行互换
                array[low] = array[j];
                array[j] = middle;
                // 将基准数左边和右边进行同样的操作
                quickSort(array, low, j - 1);
                quickSort(array, j + 1, high);
            }
        }
    
    }

    七、堆排序

    一、基本思想

    堆排序(英语:Heapsort)是指利用堆这种数据结构所设计的一种排序算法。

    堆是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。

    二、代码实现思路

    首先,咱们先了解一下什么是完成二叉树,完全二叉树就是除了最后一层之外,其他每一层都有最多的节点数,简单点儿说,就是除了最后一层,其他各层的节点数都是满的(二的n-1次方)n代表层数

    之后,咱们再了解一下啥是堆,子结点的键值或索引总是小于(或者大于)它的父节点。在这里,为了实现正序排列,咱们使用的父节点的值总大于子节点

    最后是思路,首先要自己构成一个大顶堆(也就是父节点总比子节点的数大),之后将堆顶的数字和堆位的的节点交换,数组长度减一,然后递归次步骤,直到数组的长度变为1。

    三、代码实现

    /**
     * 
     * @version: 1.1.0
     * @Description: 堆排序,时间复杂度:nlogn,空间复杂度:1
     * @author: wsq
     * @date: 2020年6月7日上午11:47:07
     */
    public class HeapSort {
        public static void main(String[] args) {
            int[] array = new int[] { 1, 6, 8, 9, 2, 7, 4, 3, 9 };
            // 调用堆排序
            heapSort(array, array.length);
            // 打印数组
            System.out.println(Arrays.toString(array));
        }
    
        public static void heapSort(int[] array, int length) {
            for (int i = length / 2; i >= 0; i--) {
                judgeAndChange(array, i, length);
            }
            // 交换堆顶的值
            int temp = array[0];
            array[0] = array[length - 1];
            array[length - 1] = temp;
            // 递归调用
            length -= 1;
            if (length > 1) {
                heapSort(array, length);
            }
        }
    
        public static void judgeAndChange(int[] array, int i, int length) {
            // 默认自己为最大值
            int max = i;
            // 获取左子节点
            int leftCode = i * 2 + 1;
            // 获取右子节点
            int rightCode = leftCode + 1;
            // 比较左子节点和最大节点
            if (leftCode < length && array[leftCode] > array[max]) {
                max = leftCode;
            }
            // 比较左子节点和最大节点
            if (rightCode < length && array[rightCode] > array[max]) {
                max = rightCode;
            }
            // 进行父节点和子节点的交换
            if (max != i) {
                int temp = array[max];
                array[max] = array[i];
                array[i] = temp;
                // 处理节点交换导致的问题
                judgeAndChange(array, max, length);
            }
        }
    }
  • 相关阅读:
    MYSQL索引优化法则
    关于String对象到底占多少字节?
    类加载的时机
    HotSpot 虚拟机对象探秘
    JVM 内存结构
    Tomcat10-记录踩坑
    笔记 | 吴恩达新书《Machine Learning Yearning》
    图像分割论文 | DRN膨胀残差网络 | CVPR2017
    除了边缘场景,你还能在哪里使用K3s?
    基础指南:如何在K3s中配置Traefik?
  • 原文地址:https://www.cnblogs.com/mcjhcnblogs/p/13056307.html
Copyright © 2020-2023  润新知