zoukankan      html  css  js  c++  java
  • 常用算法之排序(1)

    package algorithm.sort;
    
    /**
     * @Auther: AaronPi
     * @Date: 2019-4-27 23:01
     * @Description:
     * 稳定性:待排序的序列中有想等值得元素排序后元素之间原有的先后顺序不变
     * 原地排序算法:特指空间复杂度为O(1)的排序算法
     */
    public class BaseSort {
    
        /**
         * 冒泡排序:最多排序n次能出结果,第一次排序能找到最大数放在最后,
         * 如此反复第k次排序能找到排第k大的数,所以每次排序对前n-k个数排序即可,所以第k次找到n-k-1)个数即可。
         * 并且,如果任何数据交换说明已经全部排好了,可以提前结束。
         *
         * 空间复杂度O(1) ->原地排序算法
         * 稳定的算法,相同大小不会改变顺序
         * 平均时间复杂度为O(n^2)
         */
        public static int[] bubbleSort(int[] array){
            int n = array.length;
            boolean flag = false;
            if(n != 0 && n != 1){
                for (int i = 0; i < n; ++i) {
                    for (int j = 0; j < n-i-1; ++j) {
                        if (array[j] > array[j + 1]) {
                            int temp = array[j];
                            array[j] = array[j + 1];
                            array[j + 1] = temp;
                            flag = true;
                        }
                    }
                    if(!flag){
                        return array;
                    }
                }
            }
            return array;
        }
    
        /**
         * 插入排序
         * 是原地排序算法,
         * 稳定
         * 平均时间复杂度为O(n^2)
         * 最好时间复杂度为O(n)
         * 最坏时间复杂度为O(n^2)
         */
        public static int[] insertSort(int[] array){
            int n = array.length;
            if(n > 1){
                for (int i = 1; i < n; ++i) {
                    int value = array[i];
                    int j = i-1;
                    for (; j >= 0; --j) {
                        if(array[j] > value){
                            array[j+1] = array[j];
                        }else{
                            break;
                        }
                    }
                }
            }
            return array;
        }
    
        /**
         * 选择排序
         * 思想:把数组分成已排序和未排序两部分,每次把未排序中最小的元素放在已排序部分的末端(交换)
         * 是原地排序算法
         * 不稳定
         * 平均时间复杂度为O(n^2)
         * 最好时间复杂度为O(n^2)
         * 最坏时间复杂度为O(n^2)
         */
        public static int[] selectionSort(int[] array){
            int n = array.length;
            for (int i = 0; i < n-1; i++) {
                int minIndex = i;
                //找出当前剩余未排序元素中最小的位置
                for (int j = i+1; j < n-1; j++) {
                    if(array[j] < array[minIndex]){
                        minIndex = j;
                    }
                }
                //把上边排出的当前次小元素和未排序数组的头交换
                int temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
    
            }
            return array;
        }
    
        public static void printAll(int[] array){
            for(int i : array){
                System.out.println(i);
            }
        }
    
        public static void main(String[] args) {
            int[] test = {1, 6, 8, 3, 2};
            int[] bubbleResult = bubbleSort(test);
            int[] selectionResult = selectionSort(test);
            printAll(selectionResult);
        }
    }
    
    
  • 相关阅读:
    iperf/iperf3网络测试工具的安装与使用
    驱动模块(4)——模块编译
    760. Find Anagram Mappings
    MySQL面试题
    MySQL 的数据存储引擎
    203. Remove Linked List Elements
    数据库事务隔离级别
    232. Implement Queue using Stacks
    MySQL中的事务
    482. License Key Formatting
  • 原文地址:https://www.cnblogs.com/pipicai96/p/11407559.html
Copyright © 2011-2022 走看看