首页 > 编程语言 > 详细

选择排序算法

时间:2018-08-26 12:49:23      阅读:156      评论:0      收藏:0      [点我收藏+]
import cn.idestiny.util.GeneratedArray;

/**
 * @Auther: FAN
 * @Date: 2018/8/25 20:11
 * @Description:选择排序 每次排序选择出最小的数字放在对应位置
 * 1) 3,5,1,2 最小值 1 和3交换
 * 2) 1,5,3,2 最小值 2 和5交换
 * 3) 1,2,3,5 排序完成
 **/
public class SelectionSort {

    public static void main(String[] args) {

        int[] arr = GeneratedArray.randomGeneratedArray(10, 50, 10000);
        long start = System.currentTimeMillis();
        selectionsort(arr);
        System.out.println(System.currentTimeMillis() - start);
        //判断数组是否有序
        GeneratedArray.isSorted(arr);

    }

    /**
     * 选择排序算法实现
     *
     * @param arr
     */
    public static void selectionsort(int[] arr) {

        for (int i = 0; i < arr.length; i++) {
            //默认标记当前位置元素为最小值
            int minIndex = i;
            //循环遍历当前元素是不是最小值,若不是,替换标记
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            //交换元素位置
            int tmp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = tmp;
        }

    }

}

 

选择排序算法

原文:https://www.cnblogs.com/lfdestiny/p/9536841.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!