Java 選擇排序

來源:互聯網
上載者:User

標籤:play   select   它的   foreach   method   print   each   min   [1]   

選擇排序(Select Sorting)也是一種簡單的排序方法。它的基本思想是:

     第一次從R[0]-R[n1]中選取最小值,與R[0]交換,

     第二次從R[1]-R[n-1]中選取最小值,與R[1]交換,

     第三次從R[2]-R[n-1]中選取最小值,與R[2]交換,...,

     第i次從R[i-1]-R[n-1]中選取最小值,與R[i-1]交換,...,

     第n-1次從R[n-2]-R[n-1]中選取最小值,與R[n-2]交換,總共通過n-1次,得到一個按排序碼從小到大排列的有序序列。

 

package Sorting;public class SecectionSorting {                //選擇排序    public static void main(String[] args) {        // TODO Auto-generated method stub              int arr[]={2,5,11,23,14,12,20,17};      int temp=0;      for(int i=0;i<arr.length-1;i++){          //假設第一個就是最小的數          int min=arr[i];          //記錄最小的下標          int minindex=i;          for(int k=i+1;k<arr.length;k++){              if(min>arr[k]){                  //修改最小值                  min=arr[k];                  minindex=k;              }          }          //當推出本次外層for迴圈時就找到這次的最小值          temp=arr[i];          arr[i]=arr[minindex];          arr[minindex]=temp;                }      //foreach方法遍曆輸出數組a的排序結果      for(int e:arr){          System.out.print(e+"<");      }    }}

 

Java 選擇排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.