算法之排序算法-选择排序与优化

package com.ebiz.sort;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author YHj
 * @create 2019-07-28 20:58
 * 选择排序
 */
public class Choose {

    public static void main(String[] args) {
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }



        String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("排序前 = " + s);

        getResult(arr);


        String l = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("排序后 = " + l);

    }



    public static void getResult(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            int min=arr[i]; //假设为最小值
            int minIdex=i;  //最小值的索引
            for (int j = i+1; j <arr.length; j++) {
                if (min>arr[j]){
                    //找到最小值以及最小值的位置
                    min=arr[j];
                    minIdex=j;
                }
            }
            //如果不是一开始的位置不进行交换  优化
            if (minIdex != i) {
                arr[minIdex]=arr[i];
                arr[i]=min;
            }
        }
    }



}

待完善...

猜你喜欢

转载自www.cnblogs.com/jiushixihuandaqingtian/p/11299207.html