十大排序演算法Java實現

來源:互聯網
上載者:User

標籤:ini   lin   計數   while   相對   style   選擇   數值   str   

1.冒泡排序Bubble Sort

public class BubbleSort {    public static void main(String[] args) {        int[] arr = {2,5,7,1,6,4,9};        for (int i = 0; i < arr.length; i++) {            for (int j = 0; j < arr.length - 1 - i ; j++) {                if(arr[j]>arr[j+1]){                    int temp = arr[j+1];                    arr[j+1] = arr[j];                    arr[j] = temp;                }            }        }        for (int i = 0; i < arr.length; i++) {            System.out.print(arr[i]);            //1245679        }    }}

代碼解釋: 外層for迴圈可以當成是一個計數器:控制內層for迴圈的次數。

每一次外圈迴圈完成後會將最大元素排到最後一位,這樣下一次內圈迴圈就不必比較相對最後兩位元素了,於是內圈迴圈次數減少相應的數值

 

2.選擇排序Selection Sort

public class SelectionSort {    public static void main(String[] args) {        int[] arr = {2,5,7,1,6,4,9};        int minIndex,temp;        for (int i = 0; i < arr.length-1; i++) {            minIndex = i;            for (int j = i+1; j < arr.length; j++) {                if(arr[j]<arr[minIndex]){                    minIndex = j;                }            }            temp = arr[i];            arr[i] = arr[minIndex];            arr[minIndex] = temp;        }        for (int i = 0; i < arr.length; i++) {            System.out.print(arr[i]);            //1245679        }    }}

 

3.插入排序Insertion Sort

public class InsertionSort {    public static void main(String[] args) {        int[] arr = { 2, 5, 7, 1, 6, 4, 9 };        int preIndex, current;        for (int i = 1; i < arr.length; i++) {            preIndex = i - 1;            current = arr[i];            while (preIndex >= 0 && arr[preIndex] > current) {                arr[preIndex + 1] = arr[preIndex];                preIndex--;            }            arr[preIndex + 1] = current;        }        for (int i = 0; i < arr.length; i++) {            System.out.print(arr[i]);            //1245679        }    }}

 

4.希爾排序Shell Sort

public class ShellSort {    public static void main(String[] args) {        int[] arr = { 2, 5, 7, 1, 6, 4, 9 };        int temp, j;        int gap = 1;        while (gap < arr.length / 3) {            gap = gap * 3 + 1;        }        for (; gap > 0; gap = (int) Math.floor(gap / 3)) {            for (int i = gap; i < arr.length; i++) {                temp = arr[i];                for (j = i - gap; j > 0 && arr[j] > temp; j -= gap) {                    arr[j + gap] = arr[j];                }                arr[j + gap] = temp;            }        }        for (int i = 0; i < arr.length; i++) {            System.out.println(arr[i]);            // 1245679        }    }}

 

5.歸併排序Merge Sort

 

後邊的會陸續更新

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