各种常见的排序 java版本(冒泡,选择,插入,希尔,快速)

1.冒泡排序

/**

 * Created by jinzhao.w on 2015/4/7.

 */

public class BubbleSort {


    public static int [] sort(int [] array){

        int temp=0;

        for(int i=array.length-1;i>0;--i){

            for(int j=0;j<i;j++ ){

                if(array[j]>array[j+1]){

                    temp=array[j];

                    array[j]=array[j+1];

                    array[j+1]=temp;

                }

            }

        }

        return array;

    }


    public static void main(String [] args){

        int[] test={1,3,2,5,4};

        BubbleSort.sort(test);

        for(int i=0;i<test.length;i++){

            System.out.print(test[i]);

        }

    }

}

-------------------------------------------------------------------------

2.选择排序

/**

 * Created by 737597978 on 2015/4/8.

 */

public class SelectSort {


    public static int[] sort(int[] array) {


        for (int i = 0; i < array.length - 1; i++) {

            int min = i;

            for (int j = i + 1; j < array.length; j++) {

                if (array[j] < array[min]) {

                    min = j;

                }

            }

            swap(i, min, array);

        }

        return array;

    }


    private static void swap(int i, int min, int[] array) {

        int temp = array[i];

        array[i] = array[min];

        array[min] = temp;

    }



    public static void main(String[] args) {

        int[] test = {7, 1, 4, 5, 9, 8};

        SelectSort.sort(test);

        for (int i = 0; i < test.length; i++) {

            System.out.print(test[i]);

        }

    }

}

-----------------------------------------------------------------------------------------

3.插入排序

/**

 * Created by 737597978 on 2015/4/8.

 */

public class InsertSort {


    public static int [] sort(int array[]){

        for(int i=1;i<array.length;i++){

            int temp=array[i];

            int inner=i;

            while (inner>0&&array[inner-1]>=temp){

                array[inner]=array[inner-1];

                --inner;

            }

            array[inner]=temp;

        }

        return array;

    }

    public static void main(String [] args){

        int[] test={3,2,4,5,7,1};

        InsertSort.sort(test);

        for(int i=0;i<test.length;i++){

            System.out.print(test[i]);

        }

    }

}

----------------------------------------------------------------------------------------------------

4.希尔排序

/**

 * Created by 737597978 on 2015/4/8.

 */

public class ShellSort {


    public static void sort(int[] array) {

        int inner, outer;

        int temp;

        int h = 1;

        while (h <= array.length / 3) {

            h = 3 * h + 1;

        }

        while (h > 0) {

            for (outer = h; outer < array.length; outer++) {

                temp = array[outer];

                inner = outer;

                while (inner > h - 1 && array[inner - h] >= temp) {

                    array[inner] = array[inner - h];

                    inner -= h;

                }

                array[inner] = temp;

            }

            h = (h - 1) / 3;

        }

    }


    public static void main(String[] args) {

        int[] test = {2, 3, 4, 1, 5};

        ShellSort.sort(test);

        for (int i = 0; i < test.length; i++) {

            System.out.print(test[i]);

        }

    }

}

----------------------------------------------------------------------------------------------

5.快速排序

/**

 * Created by 737597978 on 2015/4/9.

 */

public class QuickSort {


    private static int[] chaosAarray = {2, 3, 7, 1, 5};


    public static void sort(int left, int right) {

        if (right <= left) {

            return;

        } else {

            int pivot = chaosAarray[right];

            int partition = partitionIt(left, right, pivot);

            sort(left, partition - 1);

            sort(partition + 1, right);

        }

    }

    public static int partitionIt(int left, int right, int pivot) {

        int leftPtr = left - 1;

        int rightPtr = right;

        while (true) {

            while (chaosAarray[++leftPtr] < pivot) ;

            while (rightPtr > 0 && chaosAarray[--rightPtr] > pivot) ;

            if (leftPtr >= rightPtr) {

                break;

            } else {

                swap(leftPtr, rightPtr);

            }

        }

        //调整枢纽值在数组中的位置  将枢钮值跟数组最右边的值对换

        swap(leftPtr, right);

        return leftPtr;

    }


    public static void swap(int leftPtr, int rightPtr) {

        int temp = chaosAarray[rightPtr];

        chaosAarray[rightPtr] = chaosAarray[leftPtr];

        chaosAarray[leftPtr] = temp;

    }


    public static void main(String[] args) {

        QuickSort.sort(0, chaosAarray.length - 1);

        for (int i = 0; i < chaosAarray.length; i++) {

            System.out.print(chaosAarray[i]);

        }

    }


}


转载于:https://my.oschina.net/u/1390040/blog/419236

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值