排序演算法 java實現

來源:互聯網
上載者:User

標籤:

幾個排序演算法,我是按照演算法的虛擬碼用java實現的,剛開始是int類型的,後來換成泛型。

這是好早之前寫的代碼了,現在那出來溫習下,就當是準備面試把

1.簡單選擇排序

這個演算法應該是最簡單的把,就是在數組中從頭開始迴圈,選擇最小的放在最前面,這個應該很好實現就不廢話了

    public static <T extends Comparable<T>> T[] genericsimpleselectionsort(T[] a){        for (int i = 0; i < a.length; i++) {            int min = i;            int j = i + 1;            while (j < a.length) {                if (a[j].compareTo(a[min])<0)                    min = j;                j++;            }                T temp = a[i];                a[i] = a[min];                a[min] = temp;        }        return a;    }

裡面加上了泛型,就是T要整合Comparable介面,就是說T類型是能夠比較的

可以寫個測試代碼,裡面加上個main函數就可以了

    public static void main(String[] args) {        Random random=new Random(47);        int n=20;        Integer[] a=new Integer[n];        for (int i = 0; i < a.length; i++) {            a[i]=random.nextInt(100);            System.out.print(a[i]+" ");        }        System.out.println();        Integer[] b=SimpleSelectSort.genericsimpleselectionsort(a);        for (int i = 0; i < b.length; i++) {            System.out.print(b[i]+" ");        }    }

這隻是測試20個數,可以改成10000,然後把輸出注釋掉,計算已耗用時間,比較不同排序演算法的已耗用時間

2.選擇排序演算法的改進

改進一下簡單選擇排序演算法,就是每趟選出最大,最小的,分別放在數組左右兩邊,所以迴圈只用做到n/2;

    public static <T extends Comparable<T>> T[] genericselectionsort(T[] a) {        int n = a.length;        for (int i = 0; i < n / 2; i++) {            int min = i;            int max = i;            int j = i + 1;            while (j < n - i) {                if (a[j].compareTo(a[min]) < 0)                    min = j;                if (a[j].compareTo(a[max]) > 0)                    max = j;                j++;            }            T temp = a[i];            a[i] = a[min];            a[min] = temp;            temp = a[n - i - 1];            a[n - i - 1] = a[max];            a[max] = temp;        }        return a;    }

3.插入排序

從給的數組的第二個數開始迴圈,數組前面是排好的,將取出的數一次跟前面排好序的數 從後向前比較,小就將該數的位置後移,知道找到合適的位置插入

好吧,用語言表達我說不好,應該用圖的,但是還不會畫圖~~還是直接上代碼把,很簡單的演算法,直接看代碼應該沒問題

    public static <T extends Comparable<T>> T[] genericInsertSort(T[] a) {        for (int i = 1; i < a.length; i++) {            int j = i;            T temp = a[j];            while (j > 0) {                if (temp.compareTo(a[j - 1]) < 0) {                    a[j] = a[j - 1];                    j--;                } else {                    break;                }            }            a[j] = temp;        }        return a;    }

 

到下班時間了,先暫時記錄這幾個簡單的排序

排序演算法 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.