Java實現六種常見的排序演算法

來源:互聯網
上載者:User

標籤:

import java.util.Random;public class Sort {    public Sort() {        // TODO Auto-generated constructor stub    }    // 冒泡排序,該排序總共要進行n-1趟,每一趟的比較次數遞減。穩定排序,複雜度為O(n^2).    public void bubbleSort(int[] a) {        if (a == null) {            return;        }        int len = a.length;        for (int i = 0; i < a.length - 1; i++)            for (int j = 0; j < len - i - 1; j++) {                if (a[j] > a[j + 1])                    swrap(a, j, j + 1);            }    }    // 直接插入排序,該排序首先要設定一個哨兵,因為在排序的時候向前比較的過程中不會越界,a<a,不可能成立。穩定排序,時間複雜度為O(n^2).    public void insertSort(int[] a) {        if (a == null) {            return;        }        int len = a.length;        int[] b = new int[len + 1];        System.arraycopy(a, 0, b, 1, len);        for (int i = 2; i <= len; i++) {            b[0] = b[i];            for (int j = i - 1; j > 0; j--) {                if (b[j] > b[0]) {                    b[j + 1] = b[j];// 元素後移一位                    b[j] = b[0];                }            }        }        System.arraycopy(b, 1, a, 0, len);    }    // shell排序,該排序是插入排序的一種,只是,間隔不再是1而是d.不穩定排序,時間複雜度為O(n^1.3)    public void shellSort(int[] a) {        if (a == null)            return;        int len = a.length;        int[] b = new int[len + 1];        System.arraycopy(a, 0, b, 1, len);        for (int d = len / 2; d >= 1; d /= 2) {            for (int i = d + 1; i <= len; i++) {                b[0] = b[i];                for (int j = i - d; j > 0; j -= d) {                    if (b[j] > b[0]) {                        b[j + d] = b[j];                        b[j] = b[0];                    }                }            }        }        System.arraycopy(b, 1, a, 0, len);    }    // 選擇排序,選出最小的值查到整個排好序的數組中,只要進行n-1次選擇就OK.穩定排序,時間複雜對為O(n^2)    public void selectSort(int[] a) {        if (a == null)            return;        int len = a.length;        for (int i = 0; i < len - 1; i++) {            int min = i;// 初始化最小元素的下標。            for (int j = i + 1; j < len; j++) {                if (a[j] < a[min]) {                    min = j;// 選出最小元素的下標。                }                swrap(a, i, min);            }        }    }    // 快速排序一次排序的結果,index左邊的值小於它,右邊的值大於它.    public int parition(int[] a, int start, int end) {        Random rand = new Random();        int index = start + rand.nextInt(end - start);        swrap(a, index, end);        int small = start - 1;        for (index = start; index < end; index++) {            if (a[index] < a[end]) {                small++;                if (small != index) {                    swrap(a, small, index);                }            }        }        small++;        swrap(a, small, end);        return small;    }    // 快速排序,通過快速排序一趟的排序來確定index值,然後遞迴排序。不穩定的排序。時間複雜度為O(nlogn).    public void quickSort(int[] a, int start, int end) {        if (a == null || start < 0 || end > a.length || start == end)            return;        int index = parition(a, start, end);        if (index > start)            quickSort(a, start, index - 1);        if (index < end)            quickSort(a, index + 1, end);    }    // 堆排序,選擇排序的一種,首先要構建堆,我們以大根堆為例,就是大根堆的左右子節點都比本身要小.不穩定排序,時間複雜度為O(nlogn)    public void heapSort(int[] a) {        if (null != a) {            int end = a.length - 1;            for (int i = (end - 1) / 2; i >= 0; i--)                adjustHead(a, i, end);            for (int i = end; i >= 0; i--) {                swrap(a, 0, i);                adjustHead(a, 0, i - 1);            }        }    }    public void adjustHead(int[] a, int start, int end) {        if (a == null || start < 0 || end > a.length || start == end)            return;        int temp = a[start];        for (int i = 2 * start + 1; i <= end; i = i * 2 + 1) {            if (i < end && a[i] < a[i + 1])                i++;            if ((a[i] > temp)) {                a[start] = a[i];                start = i;            } else                break;        }        a[start] = temp;    }    private void swrap(int[] a, int j, int i) {        // TODO Auto-generated method stub        int temp = a[j];        a[j] = a[i];        a[i] = temp;    }    private void print(int[] a) {        for (int temp : a)            System.out.print(temp + " ");        System.out.println();    }    public static void main(String[] args) {        int[] a = { 3, 5, 7, 2, 1, 8, 4 };        Sort s = new Sort();        s.bubbleSort(a);        s.print(a);        s.insertSort(a);        s.print(a);        s.shellSort(a);        s.print(a);        s.selectSort(a);        s.print(a);        s.quickSort(a, 0, a.length - 1);        s.print(a);        s.heapSort(a);        s.print(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.