用JAVA實(shí)現(xiàn)一種排序,JAVA類實(shí)現(xiàn)序列化的方法(二種)? 如在COLLECTION框架中,實(shí)現(xiàn)比較要實(shí)現(xiàn)什么樣的接口?

答:用插入法進(jìn)行排序代碼如下:

/**
 * Created by Felix on 2018/2/18 00:02
 * 插入排序
 */
public class InsertSort {
    private List<Integer> al;

    private InsertSort(int num, int mod) {
        al = new ArrayList<>(num);
        Random rand = new Random();
        System.out.println("The ArrayList Sort Before:");
        for (int i = 0; i < num; i++) {
            al.add(Math.abs(rand.nextInt()) % mod + 1);
            System.out.println("al[" + i + "]=" + al.get(i));
        }
    }

    private void sortIt() {
        int tempInt;
        int maxSize = 1;
        for (int i = 1; i < al.size(); i++) {
            tempInt = al.remove(i);
            if (tempInt >= al.get(maxSize - 1)) {
                al.add(maxSize, tempInt);
                maxSize++;
                System.out.println(al.toString());
            } else {
                for (int j = 0; j < maxSize; j++) {
                    if (al.get(j) >= tempInt) {
                        al.add(j, tempInt);
                        maxSize++;
                        System.out.println(al.toString());
                        break;
                    }
                }
            }
        }
        System.out.println("The ArrayList Sort After:");
        for (int i = 0; i < al.size(); i++) {
            System.out.println("al[" + i + "]=" + al.get(i));
        }
    }

    public static void main(String[] args) {
        InsertSort is = new InsertSort(10, 100);
        is.sortIt();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容