常見排序演算法(java實現)

來源:互聯網
上載者:User

標籤:media   oid   for   nim   ber   split   ring   obb   技術   

常見排序演算法介紹冒泡排序
  • 代碼:
public class BubbleSort {    public static void sort(int[] array) {        int tValue;        for (int i = 0; i < array.length; i++) {            for (int j = i; j < array.length; j++) {                if (array[i] > array[j]) {                    tValue = array[i];                    array[i] = array[j];                    array[j] = tValue;                }            }        }    }    public static void main(String[] args) {        int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };        sort(a);        for (int v : a) {            System.out.print(v + " ");        }    }}

輸入結果:
1 2 4 5 6 7 9 10

插入排序:

  • 代碼:

public class InsertSort {    public static void sort(int[] array) {        int tValue;        int j;        for (int i = 0; i < array.length; i++) {            j = i;            tValue = array[i];            while (j > 0 && tValue < array[j - 1]) {                array[j] = array[j - 1];                j--;            }            array[j] = tValue;        }    }    public static void main(String[] args) {        int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };        sort(a);        for (int v : a) {            System.out.print(v + " ");        }    }} 

輸入結果:
1 2 4 5 6 7 9 10

選擇排序:
  • 代碼:
public class SelectSort {    public static void sort(int[] array) {        int index, tValue;        for (int i = 0; i < array.length; i++) {            index = i;            for (int j = i + 1; j < array.length; j++) {                if (array[j] < array[index]) {                    index = j;                }            }            if (index != i) {                tValue = array[i];                array[i] = array[index];                array[index] = tValue;            }        }    }    public static void main(String[] args) {        int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };        sort(a);        for (int v : a) {            System.out.print(v + " ");        }    }}

輸入結果:
1 2 4 5 6 7 9 10

高速排序:
  • 代碼:
public class QuickSort {    public static void quickSort(int[] array, int left, int right) {        int i, j, bValue, tValue;        if (left > right) {            return;        }        i = left;        j = right;        bValue = array[left];        while (i != j) {            while (array[j] >= bValue && i < j) {                j--;            }            while (array[i] <= bValue && i < j) {                i++;            }            if (i < j) {                tValue = array[i];                array[i] = array[j];                array[j] = tValue;            }        }        array[left] = array[i];        array[i] = bValue;        quickSort(array, left, i - 1);        quickSort(array, i + 1, right);    }    public static void main(String[] args) {        int a[] = { 6, 2, 4, 5, 1, 9, 10, 7 };        quickSort(a, 0, a.length - 1);        for (int v : a) {            System.out.print(v + " ");        }    }}

輸入結果:
1 2 4 5 6 7 9 10

參考資料:

http://blog.jobbole.com/11745/

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