首页 > 代码库 > 常见排序算法(java实现)

常见排序算法(java实现)

常见排序算法介绍

冒泡排序

  • 代码:
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 = http://www.mamicode.com/array[i];"hljs-keyword">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 = http://www.mamicode.com/array[i];"hljs-keyword">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 = http://www.mamicode.com/array[i];"hljs-keyword">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 = http://www.mamicode.com/array[left];"hljs-keyword">while (i != j) {
            while (array[j] >= bValue && i < j) {
                j--;
            }
            while (array[i] <= bValue && i < j) {
                i++;
            }
            if (i < j) {
                tValue = http://www.mamicode.com/array[i];"hljs-number">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/

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

常见排序算法(java实现)