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

排序算法之选择排序

时间:2019-02-02 23:29:23      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:.so   ram   test   选择   ref   stat   splay   ade   return   

快要过节了,目前先把代码贴上,后续加上图示和复杂度信息

package com.jdk8.SortTest;

public class SelectSortTest {
    public static void main(String[] args){
        int[] params = new int[]{1,9,5,2,7,4,3,8};
        System.out.println("排序前的数据为:");
        display(params);
        selectSorted(params);
        System.out.println("排序后的数据为:");
        display(params);
    }

    public static void display(int[] arrays){
        for(int i = 0;i < arrays.length;i++){
            System.out.print(" " +  arrays[i] + " ");
        }
    }

    private static void selectSorted(int[] params) {
        if(null == params || params.length < 1){
            return ;
        }
        int min = 0;
        int ref = 0;
        for(int i = 0;i < params.length - 1;i++){
            min =  params[i];
            for(int j = i + 1;j < params.length;j++){
                if(min > params[j]){
                    min = params[j];
                    ref = j;
                }
            }
            min =  params[i];
            params[i] = params[ref];
            params[ref] = min;
        }
        System.out.println();
    }
}

运行结果如下:

排序前的数据为:
 1  9  5  2  7  4  3  8 
排序后的数据为:
 1  2  3  4  5  7  8  9 

排序算法之选择排序

标签:.so   ram   test   选择   ref   stat   splay   ade   return   

原文地址:https://www.cnblogs.com/ITBlock/p/10349373.html

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