码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序

时间:2020-01-07 18:00:56      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:each   temp   select   下标   public   length   reac   sort   index   

数组排序之选择排序

/**
 * 选择排序
 * @author  努力Coding
 * @version
 * @data
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] array = {6,3,8,2,9,1};
        System.out.println("排序前的数组为:");
        for(int before : array) {//foreach遍历
            System.out.print(before + ",");
        }
        
        int min = array[0];//假设第一个元素是最小值
        int minIndex = 0;//最小值下标
        
        for(int i = 0; i < array.length; i++) {
            min = array[i];
            minIndex = i;
            /*获取最小值的下标*/
            for(int j = i + 1; j < array.length; j++) {
                if(array[j] < min) {
                    min = array[j];
                    minIndex = j;
                }
            }
            /*进行排序*/
            if(minIndex > i) {
                int temp = array[i];
                array[i] = array[minIndex];
                array[minIndex] = temp;
            }
        }
        
        System.out.println();
        System.out.println("排序后的数组为:");
        for(int i = 0; i < array.length; i++) {//循环遍历
            System.out.print(array[i] + ",");
        }
    }

}

选择排序

标签:each   temp   select   下标   public   length   reac   sort   index   

原文地址:https://www.cnblogs.com/Zhouge6/p/12162817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!