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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

简单选择排序

(1)基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;

然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

(2)代码实现

  1. public class 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.                        
  7.                         int j=i+1;
  8.                         position=i;
  9.                         int temp=a[i];
  10.                         for(;j<a.length;j++){
  11.                         if(a[j]<temp){
  12.                                 temp=a[j];
  13.                                 position=j;
  14.                         }
  15.                         }
  16.                         a[position]=a[i];
  17.                         a[i]=temp;
  18.                 }
  19.                 for(int i=0;i<a.length;i++)
  20.                         System.out.println(a[i]);
  21.         }
  22. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马