java实现冒泡排序、插入排序、归并排序、快速排序、选择排序、希尔排序

java实现冒泡排序

/*
    冒泡排序:
    比较相邻的两个元素,如果前面的元素较大,就交换两个元素
 */
public class BubbleSort {
    public static void main(String[] args) {
        /*int[] arr = {1,2,3,4,5,6};
        int[] sort = bubbleSort(arr);
        System.out.println(Arrays.toString(sort));*/

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
        String f1 = df.format(date);
        System.out.println(f1);

        //创建一个80000次的随机数组
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random() * 8000000);
        }

        bubbleSort(array);

        Date date2 = new Date();
        String f2 = df.format(date2);
        System.out.println(f2);

    }
    public static int[] bubbleSort(int[] array){
        //外层循环控制比较的次数多少
        int temp = 0;
        for (int i = 0; i < array.length - 1; i++) {

            int flag = -1;
            //内层循环控制一次冒泡中需要比较的次数
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]){
                     temp = array[j];
                     array[j] = array[j + 1];
                     array[j + 1] = temp;
                     flag = 0;
                }
            }
           /* System.out.println("第" + (i + 1) + "次排序后的数组为:");
            System.out.println(Arrays.toString(array));
            System.out.println();*/
            if(flag == -1){
                return array;
            }
        }
        return array;
    }
}

java实现插入排序

/*
    插入排序:
    一个数组看作两个表   一个是有序表  一个是无序表
    排序是每次从无序列表中取出一个元素 然后添加到有序列表中


    5 4 3 2

 */
public class InsertSort {
    public static void main(String[] args) {
        /*int[] array = {100,4,1,5,6};
        int[] array1 = {100,4,1,5,6};
        insertSort(array);
        System.out.println("**********************");
        insertSort0(array1);*/

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
        String f1 = df.format(date);
        System.out.println(f1);

        //创建一个80000次的随机数组
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random() * 8000000);
        }
//        System.out.println(Arrays.toString(array));
        insertSort(array);
        /* System.out.println(Arrays.toString(array));*/

        Date date2 = new Date();
        String f2 = df.format(date2);
        System.out.println(f2);

    }

    public static void insertSort0(int[] array){

        /*
        2021年03月17日 19:40:43
        2021年03月17日 19:40:46
        频繁的交换,会影响效率,采用移动的方法就会使效率大大的增加
         */
        int temp = 0;
        for (int i = 1; i < array.length; i++) {
            for (int j = i -1; j >= 0 ; j = j - 1) {
                if(array[j] > array[j+1]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
            //System.out.println("第" + i +"次进行排序后:" + Arrays.toString(array));
        }
    }

    public static void insertSort(int[] array){
        /*
        2021年03月17日 19:41:43
        2021年03月17日 19:41:43
        采用移动的方法,效率比较高
         */
        int insertValue;
        int insertIndex;
        for (int i = 1; i < array.length; i++) {
            //定义待插入的数
            insertValue = array[i];
            //插入之前的数的位置
            insertIndex = i - 1;

            while (insertIndex >= 0 && insertValue < array[insertIndex]){//还没有找到插入的位置
                //把这个插入位置的元素后移
                array[insertIndex + 1] = array[insertIndex];
                insertIndex --;
            }
            if(insertIndex + 1 != i){
                array[insertIndex + 1] = insertValue;//退出循环说明,插入的位置已经找到
            }
           // System.out.println("第" + i +"次进行排序后:" + Arrays.toString(array));
        }
    }
}

java实现归并排序

/*
    归并排序,分而治之的思想
 */
public class MergeSort {
    public static void main(String[] args) {
        int[] arr = {8,4,5,7,1,3,6,2};
        int[] temp = new int[8];
        mergeSort(arr,0,arr.length - 1,temp);
        System.out.println(Arrays.toString(arr));
    }
    public static void mergeSort(int[] arr,int left,int right,int[] temp){
        if(left < right){
            int mid = (left + right) / 2;
            //向左递归
            mergeSort(arr,left,mid,temp);
            //向右递归
            mergeSort(arr,mid + 1,right,temp);
            //合并
            merge(arr,left,right,mid,temp);
        }
    }

    /**
     *两个有序序列的合并为一个新的有序序列
     * @param arr 原始数组经过分后的数组
     * @param left 数组有序序列的初始索引
     * @param right 数组有序序列的右边的索引
     * @param mid 数组有序序列的中间索引
     * @param temp 临时数组,用作中转
     */
    public static void merge(int[] arr,int left,int right,int mid,int[] temp){
        //左边有序徐列的初始索引
        int i = left;
        //右边有序序列的初始索引
        int j = mid + 1;
        //指向temp数组的当前索引
        int t = 0;

        //1、先把左右两边有序序列的数据按照规则填到temp数组中,直到其中一个序列填充满为止
        while(i <= mid && j <= right){
            if(arr[i] <= arr[j]){//把小的先放在临时数组中去
                temp[t] = arr[i];
                i++;
                t++;
            }else {
                temp[t] = arr[j];
                j++;
                t++;
            }
        }
        //2、把没有填充完的序列季旭填充
        while (i <= mid){
            temp[t] = arr[i];
            i++;
            t++;
        }
        while (j <= right){
            temp[t] = arr[j];
            j++;
            t++;
        }

        //3、把临时数组中的填充好的有序序列再次拷贝到原始的数组中去
        t = 0;
        int tempLeft = left;
        while (tempLeft <= right){
            arr[tempLeft] = temp[t];
            tempLeft ++;
            t++;
        }
    }
}

java实现快速排序

/*
    快速排序
    1、选定中心轴pivot
    2、将大于pivot的数字放在pivot的右边
    3、将小于pivot的数字放在pivot的左边
    4、分别对左右子序列重复前三步操作
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] array = {0,2,3,-1,-2};
        quickSort1(array,0,array.length-1);
        System.out.println(Arrays.toString(array));
    }
    public static void quickSort0(int[] array,int left,int right){
        int l = left;
        int r = right;
        //定义中轴pivot为中间的元素
        int pivot = array[(left + right) / 2];

        while (l < r){
            //在pivot左边找到一个大于等于pivot的值
            while (array[l] < pivot){
                l ++;
            }
            //在pivot右边找到一个小于等于pivot的值
            while (array[r] > pivot){
                r --;
            }
            //说明左边已经<= pivot,右边已经>= pivot
            if(l >= r){
                break;
            }

            //交换 pivot左边大于等于pivot的值  与  pivot右边大于等于pivot的值
            int temp = array[l];
            array[l] = array[r];
            array[r] = temp;
            //如果交换完毕,发现array[l] == pivot,需要
            if(array[l] == pivot){
                r --;
            }

            //如果交换完毕,发现array[r] == pivot
            if(array[r] == pivot){
                l ++;
            }
        }

        if(l == r){//必须需要这一步,否则会出现栈溢出的现象
            l ++;
            r --;
        }

        //向左递归
        if(left < r){
            quickSort0(array,left,r);
        }
        //向右递归
        if(right > l){
            quickSort0(array,l,right);
        }
    }


    /**
     *
     * @param array 传入一个数组
     * @param left 左指针
     * @param right 右指针
     */
    public static void quickSort1(int[] array,int left,int right){
        if(left > right){
            return;
        }
        //定义中轴pivot为最左边的元素
        int pivot = array[left];
        int l = left;//定义左指针
        int r = right;//定义右指针
        //当left < right的时候不断的进行循环
        while (l < r){
            //开始先从右指针进行扫描,直到扫描到一个小于pivot的值
            while (array[r] >= pivot && l < r){
                r --;
            }
            //退出循环时候,说明可能找到一个小于pivot的数,或者是l = r
            array[l] = array[r];
            //然后扫描左指针,直到扫描到一个大于pivot的值
            while (array[l] <= pivot && l < r){
                l ++;
            }
            //退出循环,扫描到一个大于pivot的值,或者是l = r
            array[r] = array[l];
        }

        //退出循环,一次快速排序完成
        array[l] = pivot;
        //递归进行左排序
            quickSort1(array,left,r - 1);
        //递归进行右排序
            quickSort1(array,l + 1,right);
    }
}

java实现选择排序

/*
    选择排序
    4 3 2 1 0

    第一轮:假定arr[0]为最小值  从arr[1]到arr[4]之间选取最小值  如果arr[0]较大,就和最小值进行交换
    第二轮:假定arr[1]最小
    第三轮:假定arr[2]最小
    第四轮:假定arr[3]最小

 */
public class SelectSort {


    public static void main(String[] args) {
        /*int[] arr = {6,5,4,3,2,1,10};
        int[] sort = selectSort(arr);
        System.out.println(Arrays.toString(sort));*/

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
        String f1 = df.format(date);
        System.out.println(f1);

        //创建一个80000次的随机数组
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random() * 8000000);
        }
//        System.out.println(Arrays.toString(array));
        selectSort(array);
       /* System.out.println(Arrays.toString(array));*/

        Date date2 = new Date();
        String f2 = df.format(date2);
        System.out.println(f2);


    }

    public static int[] selectSort(int[] array){
        //外层循环控制比较的次数
        int min;
        int index ;
        for (int i = 0; i < array.length - 1; i++) {
            min =  array[i];//假定为最小值
            index = i;//假定为最小值的下标
            //内层循环,最小值与i + 1  到  array.length - 1之间的最小值进行比较
            for (int j = i + 1; j < array.length; j++) {
                if(min > array[j]){
                    min = array[j];//最小值
                    index = j;//最小值的下标
                }
            }
            if(i != index){
                array[index] = array[i];
                array[i] = min;
            }
        }
        return array;
    }
}

java实现希尔排序

/*
    希尔排序
    2021年03月17日 19:39:29
    2021年03月17日 19:39:34

    花费5秒
 */
public class ShellSort {
    public static void main(String[] args) {
        /*int[] array = {8,9,1,7,2,3,5,4,6,0};
        shellSort0(array);*/


        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
        String f1 = df.format(date);
        System.out.println(f1);

        //创建一个80000次的随机数组
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random() * 8000000);
        }
//        System.out.println(Arrays.toString(array));
        shellSort0(array);
        /* System.out.println(Arrays.toString(array));*/

        Date date2 = new Date();
        String f2 = df.format(date2);
        System.out.println(f2);

    }

    public static void shellSort0(int[] array){
        int temp ;
        int count = 0;
        for (int gap = array.length / 2; gap > 0; gap = gap / 2) {//希尔增量排序
            for (int i = gap; i < array.length; i++) {//插入排序
                for (int j = i - gap; j >= 0; j = j - gap) {
                    if(array[j] > array[j + gap]){
                        temp = array[j];
                        array[j] = array[j+gap];
                        array[j+gap] = temp;
                    }
                }
            }
           // System.out.println("第" + (++count) +"次进行排序后:" + Arrays.toString(array));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值