Java 插入排序

來源:互聯網
上載者:User

標籤:stat   遍曆   length   author   排序   test   move   ati   array   

     插入排序和前面兩種有點不同啦。插入排序有比較,移動的動作,插入排序分為已排序部分和待插入部分,將待插入資料與有序部分比較,遍曆有序部分,找到第一個比插入資料的位置,插入資料,再移動插入位置之後的資料。

     比較、插入、移動資料,移動還是有點麻煩的

public class InsertOne {    /**     * @author chengdu     * @param args     */    private int[] array;        public void setArray(int[] array){        this.array = array;    }    public int[] getArray(){        return array;    }        public void insertOneMove(int x, int y){  // y > x         if(x > array.length || y > array.length){            System.out.println("數組越界");        }else{               if(array[x] > array[y]){                    int temp = array[x];                    array[x] = array[y];                    for(; y > x + 1 ; y--){                        array[y] = array[y-1];                    }                    array[x+1] = temp;               }            }    }        public static void main(String[] args) {        // TODO Auto-generated method stub        InsertOne insertone = new InsertOne();        int[] testarray = {1,2,7,4,5,6,3,8};        insertone.setArray(testarray);        insertone.insertOneMove(2, 6);        System.out.println(Arrays.toString(testarray));    }}

插入排序

public class InsertSort {    /**     * @author chengdu     * @param args     */    private InsertOne insertone;        public void setInsertone(InsertOne insertone){        this.insertone = insertone;    }    public InsertOne getInsertone(){        return insertone;    }        public void insertSortMethod(int[] array){        int pos;        int lenarray = array.length;        insertone.setArray(array);        for(pos=1; pos < lenarray; pos++){            System.out.println("次數---------:"+pos);            for(int i=0; i < pos; i++){                insertone.insertOneMove(i, pos);            }            //輸出已經排好順序的部分            for(int i=0; i < pos+1; i++){                System.out.println(array[i]);            }        }    }            public static void main(String[] args) {        // TODO Auto-generated method stub        InsertOne insertone = new InsertOne();        InsertSort insertsort = new InsertSort();        insertsort.setInsertone(insertone);        int[] array = {9, 1, 6, 3, 4, 2, 5, 13, 15, 10};        insertsort.insertSortMethod(array);        System.out.println("---------順序-----------------");        for(Integer i : array){            System.out.println(i);        }    }}

運行

次數---------:1
1
9
次數---------:2
1
6
9
次數---------:3
1
3
6
9
次數---------:4
1
3
4
6
9
次數---------:5
1
2
3
4
6
9
次數---------:6
1
2
3
4
5
6
9
次數---------:7
1
2
3
4
5
6
9
13
次數---------:8
1
2
3
4
5
6
9
13
15
次數---------:9
1
2
3
4
5
6
9
10
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.