Java 數組 之 一維數組 選擇排序演算法

來源:互聯網
上載者:User

標籤:static   TE   length   mini   core   插入   cts   最小值   ring   

http://www.verejava.com/?id=16992690199232

/*用選擇排序:思路:    1. 將數組中剩下的沒有排序的元素中選出最小的一個, 插入已經排序的後面*/public class SelectSort {        public static void sort(int[] arrays){        int minIndex;//儲存選擇最小值的索引        for (int i = 0; i < arrays.length-1; i++) {            minIndex=i;            int minValue=arrays[minIndex];//儲存每次迴圈最小值為迴圈的第一元素的值            for (int j = i; j < arrays.length-1; j++) {                //與每個元素比較如果小於最小值則交換值並且儲存交換值的索引為最小值索引                if(minValue>arrays[j+1]){                    minValue=arrays[j+1];                    minIndex=j+1;                }            }            if(i!=minIndex)//判斷如果最小值索引改變則把當前最小值與以排序的最後一個元素交換            {                int temp=arrays[i];                arrays[i]=arrays[minIndex];                arrays[minIndex]=temp;            }        }    }    public static void main(String[] args) {                int[] scores={90,70,50,80,60,85};        sort(scores);        for(int score:scores){            System.out.print(score+",");        }    }}

http://www.verejava.com/?id=16992690199232

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.