希尔排序(四)

package com.fh.sort;

/**
 * @author
 *
 * @create 2018-03-06 下午10:22
 **/
public class Sort {

    public static void main(String[] args){
        Sort sort=new Sort();
//        sort.quickSort();
//        sort.xRSort();
//        sort.simpleSort();
        sort.bubbleSort();
    }


    /**
     * 插入排序-直接插入排序
     *基本思想:将一个记录插入到已经排序好的有序表中,从而达到一个新的,记录数加1的有序表
     * 先将序列的第一个记录看成有序的自序列,然后从第二个序列逐个插入到序列中,直到整个序列有序
     */
    public void quickSort(){
        int[] array={12,23,35,67,89};
        int len=array.length;
        for (int i=1;i<len;i++){
            int courrentData=array[i];//需要插入的数据
            int a=i-1;//有序数据
            while(a>0&&courrentData<array[a]){//比较,循环比较数据,当数组中的数据大于当前数据时
                array[a+1]=array[a];//进行数据的移动,
                a--;
            }
            array[a+1]=courrentData;//赋值
        }
        for (int a:array
             ) {
            System.out.println("
"+a);

        }
    }

    /**
     * 希尔排序--处理大数据量
     * 将数组的个数设置为n,k=n/2,将下标差距为k的划分为一组,构成有序序列
     * 在取k=k/2,将下标值为k的构成一组有序序列
     */
    public void xRSort(){
        int[] array={12,23,34,45,56,78,34,56,12,78,99};
        int b=array.length;
        while (b!=0){
            b=b/2;
            for (int x=0;x<b;x++){//分组的个数
                for (int i=x+b;i<array.length;i+=b){//组内的元素
                    int courrent=array[i];//直接插入排序的东西
                    int j=i-b;
                    while (j>0&&courrent<array[j]){
                        array[j+b]=array[j];
                        j=j-b;
                    }
                    array[j+b]=courrent;
                }
            }
        }
        for (int c:array
             ) {
            System.out.println("
"+c);

        }
    }

    /**
     * 简单选择排序
     * 如果每次比较都交换,就是交换排序,如果循环结束在交换,就是简单选择排序
     * 遍历最小的数,将最小的数放到最前面
     * 遍历之后的数,将最小的数放到最前面
     * 重复之前的过程
     */
    public void simpleSort(){
        int[] array={12,32,22,45,67,89,9,2,23,34};
        int length=array.length;
        for (int i=0;i<length;i++){
            int key=array[i];
            int position=i;
            for (int j=i+1;j<length;j++){
                if(array[j]<key){
                    key=array[j];
                    position=j;
                }
            }
            array[position]=array[i];
            array[i]=key;
        }

        for (int a:array
             ) {
            System.out.println("
"+a);

        }

    }

    /**
     * 冒泡排序
     * 将序列中的元素进行两两进行比较,最大的放到最后面的
     * 将剩下的元素进行比较,将最大的放到最后面
     */
    public void bubbleSort() {
        int[] array = {12, 23, 34, 12, 45, 23, 34, 2, 67, 9};
        int length = array.length;
        int tem;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i - 1; j++) {//需要-1,不然后续的j+1会下标越界
                if (array[j] > array[j + 1]) {//如果前一个数据大于后一个数据,进行数据数据交换
                    tem = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tem;
                }
            }
        }
        for (int a : array
                ) {
            System.out.println("
" + a);

        }

    }
    /**
     * 快速排序
     * 选择第一个数为p,小于p的数放到左边,大于p的数放到右边
     */
    public void quickSorts(){

    }

    /**
     * 归并排序
     * 选择相邻两个数组成有序序列
     * 选择两个有序序列组组成有序序列
     * 重复上面的步骤
     */
    public void gBSorts(){

    }

    /**
     *基数排序
     * 将数组的各位数取出,按照各位数进行排序,构成有序序列
     * 将构成好的有序序列取出十位数,进行排序
     */
    public void jsSorts(){

    }
}
原文地址:https://www.cnblogs.com/nihaofenghao/p/8969833.html