以下主要是 Java 版本的排序

1.冒泡排序

public class BubbleSort {
    public static void main(String[] args) {
        int[] a = new int[] { 3, 4, 2, 4, 2, 8, 4, 3, 3, 2, 2, 2, 2, 2, 254545,
                43 };
        printBefore(a);
        bubbleSort(a);
        printAfter(a);
    }

    private static void bubbleSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    swap(a, i, j);
                }
            }
        }
    }

    private static void swap(int[] a, int i, int j) {
        a[i] = a[i] + a[j];
        a[j] =a[i] - a[j] ;
        a[i] = a[i]-a[j];
    }

    private static void printAfter(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i] + ",");
        }
    }

    private static void printBefore(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i] + ",");
        }
    }
}

2.快速排序

public class FastSort {
    public static void main(String[] args) {
        int [] a= new int[]{2,4,5,2,5,2,5,2,5,788,3432,5,25,2,5} ;
        int low = 0 ;
        int high = a.length -1 ; 
        printBefore(a) ;
        fastSort(a,low,high) ;
        System.out.println("efref");
        printAfter(a) ;

    }

    private static void fastSort(int[] a, int low, int high) {
        int start = low ;
        int end = high ;
        int key = a[start] ;
        boolean flag = true ; 
        if(low>high || a.length<=1){
            return ;
        }
        if(start>=end){
            return ;
        }
        while(start != end){
            if(flag){
                if(a[end]<key){
                    swap(a,end,start) ;
                    flag = false ;
                }else{
                    end-- ;
                }
            }else{
                if(a[start]>key){
                    swap(a,start,end) ;
                    flag = true ;
                }else{
                    start++;

                }
            }
        }
        fastSort(a, low, start-1) ;
        fastSort(a,end+1 ,high) ;
    }

    private static void swap(int[] a, int end, int start) {
        a[end] = a[end] +a[start] ;
        a[start] =a[end] -a[start] ;
        a[end] = a[end] -a[start] ;
    }

    private static void printAfter(int[] a) {
        for(int i = 0;i<a.length ;i++){
            System.out.print(a[i]+",");
        }
    }

    private static void printBefore(int[] a) {
        for(int i = 0;i<a.length ; i++){
            System.out.print(a[i] +",");
        }
    }
}

3.插入排序

public class InsertSort {
    public static void main(String[] args) {
        int[] a = new int[] { 3, 4, 32, 4, 2, 4, 2, 5, 07, 8, 7, 6, 4, 4, 4 };
        printBefore(a);
        insertSort(a) ;
        System.out.println("排序之后:");
        printAfter(a) ;

    }

    private static void insertSort(int[] a) {
        for(int i =1 ;i<a.length ;i++){
            int currentData = a[i] ;
            while(i>0 && a[i-1] >currentData){
                a[i] = a[i-1] ;
                i-- ;

            }
            a[i] = currentData ;

        }
    }

    private static void printAfter(int[] a) {
        for(int i = 0 ;i<a.length ;i++){
            System.out.print(a[i]+",");
        }
    }

    private static void printBefore(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ",");
        }
    }
}

代码量之所以看起来比较多,是因为我把输出展示都写了,大家如有问题 ,欢迎沟通


↙↙↙阅读原文可查看相关链接,并与作者交流