排序算法

2020-05-16  本文已影响0人  engineer_tang
package com.joe.appointment.common.utils;

public class SortAlgorithm {

    /**
     * 冒泡排序算法
     * @param array
     */
    public static void bubbleSortData(int[] array) {
        if(array == null || array.length == 0) {
            return;
        }
        for(int i=0;i<array.length-1; i++)
            for(int j=i+1;j<=array.length-1;j++){
                if(array[i]>array[j]) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
    }

    /**
     * 快速排序算法
     * @param array
     */
    public static void quickSortData(int[] array, int left, int right) {
        int i,j;
        i = left;
        j = right;
        int middle = array[(left + right)/2];

        do{
            while(array[i] < middle && i<right) i++;
            while(array[j] > middle && j>left) j--;
            if(i<=j){
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
            i++;
            j--;
        }while(i <= j);
        if(i < right) {
            quickSortData(array, i+1, right);
        }
        if(j > left) {
            quickSortData(array, left, j-1);
        }
    }

    public static void quickSort(int[] array, int left, int right){
        int i=left,j= right;
        int m = array[right];
        do{
            while(m>array[i] && i<j){
                i++;
            }
            if(j>i){
                j--;
                while(m<array[j] && i<j){
                    j--;
                }
            }
            if(i<j) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
                i++;
                if(i == j){
                    int temp2 = array[i];
                    array[i] = array[right];
                    array[right] = temp2;
                }
            }else if(i == j){
                int temp2 = array[i];
                array[i] = array[right];
                array[right] = temp2;
            }
        }while(i<j);
        if(left<j) {
            quickSort(array, left, j-1);
        }
        if(right>i) {
            quickSort(array, i+1, right);
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{2,7,3,5,0,1};
        System.out.println("冒泡排序排序前... ");
        for(int d : array) {
            System.out.print(d + " ");
        }
        System.out.println("");
        System.out.println("排序后... ");
        bubbleSortData(array);
        for(int d : array) {
            System.out.print(d + " ");
        }


        System.out.println("");
        array = new int[]{4,8,2,6,9,1,5,2,6};
        System.out.println("快速排序排序前... ");
        for(int d : array) {
            System.out.print(d + " ");
        }
        System.out.println("");
        System.out.println("排序后... ");
//        quickSortData(array, 0, array.length-1);
        quickSort(array, 0, array.length-1);
        for(int d : array) {
            System.out.print(d + " ");
        }
        System.out.println("");
    }
}

上一篇 下一篇

猜你喜欢

热点阅读