O(n^2)排序之选择排序/插入排序/希尔排序/冒泡排序

package com.fgy.learn;

/**
 * Created by snow_fgy on 2019/3/31.
 */
public class Sort {

    //获取随机数组
    public int[] getReadom(int n, int random) {

        int [] a = new int[n];
        for (int i = 0; i < n; i++){
            a[i] = (int) (Math.random() * random);
        }
        return a;

    }

    /**
     * 第一轮: 从数组中选出最大或者最小的元素,将其索引位置和第一个元素替换。
     * 第二轮: 从第二个位置开始到最后一个元素中,找到最大或者最小的元素,将其索引位置和第二个元素替换
     * 。。。。
     * 最后一轮:最后一个位置上的元素就不变了
     * @param a 数组
     */
    public void selectionSort(int [] a){
        for (int i = 0; i < a.length; i ++) {
            int minIndex = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[minIndex] > a[j]){
                    minIndex = j;
                }
            }
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
    }


    /**
     * 升序:8 6 2 3 1 5 7 4
     * 第一轮: 第二个元素和第一个元素比较,第一元素大于第二个元素,将第一个元素和第二个元素交换位置。6 8 2 3 1 5 7 4
     * 第二轮: 第三个元素和第二个元素比较,第三个元素比第二个元素小,将第二个元素和第三个元素交换位置。6 2 8 3 1 5 7 4。
     * 然后交换完后的第二个元素在和第一个元素笔比较,第二个元素比第一个元素小,将第二个元素和第一个元素交换位置。2 6 8 3 1 5 7 4
     * 。。。。
     * 最后一轮: 将倒数第一个元素和倒数第二个元素比较。最后一个元素小,就将最后一个元素和倒数第二个元素的位置交换。将交换完后的,倒数第二个元素和倒数第三个元素进行比较。。。。
     * @param a
     */
    public void insertSort(int [] a){
        for (int i = 1; i < a.length; i ++) {

            for (int j = i; j > 0 && a[j-1] > a[j]; j--) {
                int temp = a[j-1];
                a[j-1] = a[j];
                a[j] = temp;
            }
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();


    }

    /**
     * 插入算法改进1
     * 将比较和交换分开,原始的插入算法,是一次比较,三次交换。新的是将换成一次比较一次赋值
     * 第一轮: 第二个元素和第一个元素比较,第一元素大于第二个元素,将第二个元素的值赋值给一个临时变量temp,将第一个元素的值赋值给第二个元素。在将temp的值赋值给第一个元素。6 8 2 3 1 5 7 4
     * 第二轮: 第三个元素和第二个元素比较,第二个元素比第三个元素大,将第三个元素的值赋值给临时变量temp。第二个元素的值赋值给第三个元素。在比较第一个元素的值和temp的大小。
     * 若第一个元素的值比temp的值小。将temp的值赋值给第二个元素。如第一个元素的值比temp值大,则第一个元素的值赋值给第二个元素,temp的值赋值给第一个元素。6 2 8 3 1 5 7 4。
     * @param a
     */
    public void insertSort2(int [] a){
        for (int i = 1; i < a.length; i++) {
            int temp = a[i];
            int j;
            for (j = i; j > 0 && temp< a[j-1]; j--) {
                a[j] = a[j-1];
            }
            a[j] = temp;
        }
        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();

    }

    public void shellSort(int [] a){
        int temp;
        int j;

        for (int increment = a.length / 2; increment > 0; increment /= 2) {

            for (int i = increment; i < a.length; i ++) {
                temp = a[i];

                for (j = i - increment; j >= 0; j -= increment) {

                    if (temp < a[j]) {
                        System.out.println("a" + (j+increment) + "=" + a[j+increment]);
                        a[j + increment] = a[j];
                    }else {
                        break;
                    }
                }
                a[j + increment] = temp;

            }
        }

        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();

    }

    public void bubbleSort(int []a){
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }

        }

        for (int i : a) {
            System.out.print(i + " ");
        }
        System.out.println();
    }



    public static void main(String[] args) {
        Sort sort = new Sort();
        int[] arr = sort.getReadom(1000 , 100);
        long time1 = System.currentTimeMillis();
        sort.selectionSort(arr);
        long time2 = System.currentTimeMillis();
        System.out.println("selectionSort: " + (time2 - time1) / 1000.0 + "s");
        sort.insertSort(arr);
        long time3 = System.currentTimeMillis();
        System.out.println("insertSort:" + (time3 - time2) / 1000.0 + "s");
        sort.insertSort2(arr);
        long time4 = System.currentTimeMillis();
        System.out.println("insertSort:" + (time4 - time3) / 1000.0 + "s");
        sort.shellSort(arr);
        long time5 = System.currentTimeMillis();
        System.out.println("shellSort:" + (time5 - time4) / 1000.0 + "s");


        sort.bubbleSort(arr);
        long time6 = System.currentTimeMillis();
        System.out.println("bubbleSort: " + (time6-time5) / 1000.0 + "s");

    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值