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


(1)基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;
然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。


  1. publicclass selectSort {

  2.     public selectSort(){
  3.        int a[]={1,54,6,3,78,34,12,45};
  4.        int position=0;
  5.        for(int i=0;i<a.length;i++){     
  6.            int j=i+1;
  7.            position=i;
  8.            int temp=a[i];
  9.            for(;j<a.length;j++){
  10.               if(a[j]<temp){
  11.                  temp=a[j];
  12.                  position=j;
  13.               }
  14.            }
  15.            a[position]=a[i];
  16.            a[i]=temp;
  17.        }

  18.        for(int i=0;i<a.length;i++)
  19.            System.out.println(a[i]);
  20.     }
  21. }
复制代码



111.jpg (63.17 KB, 下载次数: 22)

111.jpg

1 个回复

倒序浏览
简洁明了,很清楚
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马