A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hopestar 中级黑马   /  2015-6-9 21:21  /  301 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test1 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arrays={34,23,45,67,80,12,13,423,1,5};//定义一个整型数组并进行对其初始化
                order (arrays);//调用选择排序函数
                for(int i=0;i<arrays.length;i++)//打印输出已排序完成的元素
                        System.out.print(arrays[i]+" ");
       
               
        }
        static void order(int[] a)//选择排序函数
        {
                int min_index;//定义一个最小元素索引
                for(int i=0;i<a.length-1;i++)//确定内层循环比较的次数
                {
                        min_index=i;
                        for(int j=i+1;j<a.length;j++)//每次扫描,确定最小项索引
                                if(a[j]<a[min_index])//排序顺序从小到大
                                        min_index=j;//记录最小元素索引
                               
                        if(min_index !=i)//根据内循环扫描到的最小项索引找到最小项交换,即将这一项移到列表中的正确位置
                        {  
                                int temp;
                            temp=a[i];
                                a[i]=a[min_index];
                                a[min_index]=temp;
                               
                        }
                }
        }

       
       
       
}


3 个回复

倒序浏览
学习了  加油
回复 使用道具 举报
学习了加油咱们一起!
回复 使用道具 举报
选择排序可以和冒泡排序比较着看
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马