(1)简单选择排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26569761/article/details/75452819

原理:对于一组数据,第一轮,找到最小数之后与第一个数据的位置交换

                                        第二轮,从不包括第一个数据的其他数据中找到最小数,与第二个数据进行交换

                                        重复该过程,直到只有一个记录时为止。

例子:{38 27 56 5 80}

第一趟排序   5  [38 27 56  80]

第二趟排序   5 27 [38 56  80]

第三趟排序   5 27 38  [56 80]

第四趟排序   5 27 38  56  [80]

最终结果       5 27 38  56  80

public class Testsort(){
     public static void selectSort(int[] a){
           int temp = 0;
           int flag = 0;
           int n = a.lenght();
           for(int i=0;i<n;i++) {
              temp = a[i];
              flag = i;
              for(int j=i+1;j<n;j++) {
                 if(a[j] < temp) {
                    temp = a[j];
                    flag = j;
                 }
              }
              if(flag != i){
                 a[flag] = a[i];
                 a[i] = temp;
              }
           }
     }
     public static void main(String[] args){
          int a[] = {2,5,1,2,3,54,22,93};
          selectSort(a);
          for(int i=0;i<a.leght;i++){
	      System.out.println(a[i] + "");
          }
     }
}


猜你喜欢

转载自blog.csdn.net/qq_26569761/article/details/75452819
今日推荐