Java 選擇排序

來源:互聯網
上載者:User

標籤:動作   比較   bsp   generate   實現   private   class   交換   log   

    選擇排序,每次將最小的數選出來,反覆執行兩個動作,比較、交換,放在最左邊,依次類推,用數組實現選擇排序

    交換兩個數

public class SwapTwo {    /**     *@author chengdu     *@param args     */    private int[] bubble;        public void setBubble(int[] bubble){        this.bubble = bubble;    }    public int[] getBubble(){        return bubble;    }        public void swapTwoNumber(int x, int y){   //傳入數組的索引位置,互動數組的兩個值        int lenbubble = bubble.length;        if(x > lenbubble - 1 || y > lenbubble - 1){            System.out.println("數組越界");        }         else {            int temp;            if(bubble[x] > bubble[y]){                temp = bubble[x];                bubble[x] = bubble[y];                bubble[y] = temp;            }        }        System.out.println("從小到大依次是:"+bubble[x]+","+bubble[y]);    }        public static void main(String[] args) {        // TODO Auto-generated method stub        SwapTwo st = new SwapTwo();        int[] bubble = {1, 9, 8, 5, 2, 6, 4, 3, 7};        System.out.println(bubble.length);        st.setBubble(bubble);        st.swapTwoNumber(0, 1);    }}

 選擇排序

public class SelectSort {    /**     * @author chengdu     * @param args     */    private SwapTwo swaptwo;        public void setSwaptwo(SwapTwo swaptwo){        this.swaptwo = swaptwo;    }    public SwapTwo getSwaptwo(){        return swaptwo;    }        public void selectSortMethod(int[] array){        int pos; //次數        int lenarray = array.length;        swaptwo.setBubble(array);        for(pos=1; pos < lenarray; pos++){            System.out.println("次數--------"+pos);            for(int i=pos-1; i < lenarray - 1; i++){                swaptwo.swapTwoNumber(pos - 1, i+1);            }        }            }        public static void main(String[] args) {        // TODO Auto-generated method stub        SwapTwo swaptwo = new SwapTwo();        SelectSort selectSort = new SelectSort();        selectSort.setSwaptwo(swaptwo);        int[] array = {1, 9, 6, 3, 4, 2, 5, 13, 15, 10};        selectSort.selectSortMethod(array);        for(Integer i : array){            System.out.println(i);        }    }}

執行:

次數--------1
從小到大依次是:1,9
從小到大依次是:1,6
從小到大依次是:1,3
從小到大依次是:1,4
從小到大依次是:1,2
從小到大依次是:1,5
從小到大依次是:1,13
從小到大依次是:1,15
從小到大依次是:1,10
次數--------2
從小到大依次是:6,9
從小到大依次是:3,6
從小到大依次是:3,4
從小到大依次是:2,3
從小到大依次是:2,5
從小到大依次是:2,13
從小到大依次是:2,15
從小到大依次是:2,10
次數--------3
從小到大依次是:6,9
從小到大依次是:4,6
從小到大依次是:3,4
從小到大依次是:3,5
從小到大依次是:3,13
從小到大依次是:3,15
從小到大依次是:3,10
次數--------4
從小到大依次是:6,9
從小到大依次是:4,6
從小到大依次是:4,5
從小到大依次是:4,13
從小到大依次是:4,15
從小到大依次是:4,10
次數--------5
從小到大依次是:6,9
從小到大依次是:5,6
從小到大依次是:5,13
從小到大依次是:5,15
從小到大依次是:5,10
次數--------6
從小到大依次是:6,9
從小到大依次是:6,13
從小到大依次是:6,15
從小到大依次是:6,10
次數--------7
從小到大依次是:9,13
從小到大依次是:9,15
從小到大依次是:9,10
次數--------8
從小到大依次是:13,15
從小到大依次是:10,13
次數--------9
從小到大依次是:13,15
1
2
3
4
5
6
9
10
13
15

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.