zoukankan      html  css  js  c++  java
  • JAVA常见的排序算法

    一、冒泡排序:

    package com;
    
    import java.util.Arrays;
    
    public class BubbleSort {
        public static Integer[] bubbleSort(Integer[] data) {
            // 数组长度
            int len = data.length;
            // 临时变量
            int temp = 0;
            // 冒泡次数
            for (int i = 0; i < len-1; i++) {
                // 交换次数
                for (int j = 0; j < len-i-1; j++) {
                    if (data[j]>data[j+1]) {
                        temp = data[j];
                        data[j] = data[j+1];
                        data[j+1] = temp;
                    }
                }
            }
            return data;
        }
        public static Integer[] bubbleSort2(Integer[] data) {
            int temp = 0;
            // 第一层循环是跑的趟数
            for (int i = 0; i < data.length; i++) {
                // 第二层循环是比较次数
                for (int j = i; j < data.length; j++) {
                    if (data[i]>data[j]) {
                        temp = data[i];
                        data[i] = data[j];
                        data[j] = temp;
                    }
                }
            }
            return data;
        }
        public static void main(String[] args) {
            Integer[] data = {9,5,6,2,7,8,1,3};
    //        int[] result = bubbleSort(data);
            Integer[] result = bubbleSort(data);
            System.out.println(Arrays.toString(result));
        }
    
    }
    View Code

    说明:

    1.时间复杂度:O(n^2)、空间复杂度:O(1)

    2.算法稳定性:稳定

    3.算法描述:每一次外层循环结束之后都可以把最大的数放在顶端,所以外层循环就是每趟都把最大的数往顶层放。内层循环就是把两两数据进行比较,如果前一个比后一个大,就交换,依次比较完整个数组。

    二、选择排序:

    package com;
    import java.util.Arrays;
    
    public class SelectSort {
        public static Integer[] selectSort(Integer[] data) {
            int len = data.length;
            for (int i = 0; i < len; i++) {
                int temp = data[i];
                int position = i;
                for (int j = i+1; j < len; j++) {
                    if (data[j]<temp) {
                        temp = data[j];
                        position = j;
                    }
                }
                data[position] = data[i];
                data[i] = temp;
            }
            return data;
        }
        public static void main(String[] args) {
            /**5 9 6 2 7 8  1 3**/
            Integer[] data = {9,5,6,2,7,8,1,3};
            Integer[] result = selectSort(data);
            System.out.println(Arrays.toString(result));
        }
    }
    View Code

    说明:

    1.时间复杂度:O(n^2)、空间复杂度:O(1)

    2.算法稳定性:不稳定

    3.算法描述:第一次循环先拿第一个数作为基准,依次和后面的数进行比较,每一次外层循环都可以确定出最大或者最小的数,后面依此类推。

    三、插入排序:

    package com;
    
    import java.util.Arrays;
    
    public class InsertSort{
        public static Integer[] insertSort(Integer[] data){
            int len = data.length;
            int insertNum;
            for(int i=1;i<len;i++){
                insertNum = data[i];
                int j=i-1;
                while(j>=0&&data[j]>insertNum){
                    data[j+1] = data[j];
                    j--;
                }
                data[j+1] = insertNum;
            }
            return data;
        }
        public static void main(String[] args) {
            /**5 9 6 2 7 8  1 3**/
            Integer[] data = {9,5,6,2,7,8,1,3};
            Integer[] result = insertSort(data);
            System.out.println(Arrays.toString(result));
        }
    }
    View Code

    说明:

    1.时间复杂度:O(n^2)、空间复杂度:O(1)

    2.算法稳定性:稳定

    3.算法描述:第一次循环先拿第二个数和第一个做比较,如果第二个数大于第一个数,就相互交换。第二次循环又把第三个数拿来和前面两个排好序作比较,看是否交换,依此类推。

    三、快速排序:

    package com;
    
    import java.util.Arrays;
    
    public class QuickSort{
        public static Integer partition(Integer[] data, int start, int end){
            int temp = data[start];
            while(start<end){
                if(start<end&&data[end]>temp){
                    end -= 1;
                }
                data[start] = data[end];
                if(start<end&&data[start]<temp){
                    start += 1;
                }
                data[end] = data[start];
            }
            data[start] = temp;
            return start;
        }
        public static Integer[] quickSort(Integer[] data, int start, int end){
            int middle;
            if(start<end){
                middle = partition(data,start, end);
                quickSort(data,start, middle-1);
                quickSort(data,middle+1,end);
            }
            return data;
        }
        public static void main(String[] args){
            Integer[] data = {9,5,6,2,7,8,1,3};
            Integer[] result = quickSort(data, 0 ,data.length-1);
            System.out.println(Arrays.toString(result));
        }
    }
    View Code

    说明:

    1.时间复杂度:O(nlog2n)、空间复杂度:O(nlog2n)

    2.算法稳定性:不稳定

    3.算法描述:思想是“分而治之”,第一次循环是先拿第一个数作为基准,把比第一个数大的数放在它的右边,把比第一个数小的数放在它的左边。

    努力有用的话,还要天才做什么呢?
  • 相关阅读:
    (转)Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等
    (转)Objective-C语言--属性和实例变量
    (转)iOS 属性字符串
    (转)git clone: error: RPC failed; result=18, HTTP code = 200 解决办法
    iOS设置UINavigationBar 的样式
    Install kubernetes without yum
    kubernetes install for centos
    Deploying docker registry v2
    build docker deivcemapper
    docker local registry server gave HTTP response to HTTPS client
  • 原文地址:https://www.cnblogs.com/crazy-xf/p/10024562.html
Copyright © 2011-2022 走看看