六大排序算法(Java实现)

package sort;

import java.util.Arrays;

public class SortUtils {
    public static void main(String[] args) {
        int[] arr = { 54, 26, 93, 17, 77, 31, 44, 55, 20 };
        // bubbleSort(arr);
        // travel(arr);
        // selectSort(arr);
        // travel(arr);
        // quickSort(arr, 0, arr.length-1);
        // travel(arr);
        // insertSort(arr);
        // travel(arr);
        // shellSort(arr);
        // travel(arr);
        travel(mergeSort(arr));
    }

    public static void travel(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // 归并排序
    public static int[] mergeSort(int[] arr) {
        while (arr.length <= 1) {
            return arr;
        }
        // 二分分解
        int mid = arr.length / 2;
        int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
        int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));
        
        // 合并
        int[] result = new int[arr.length];
        int i = 0, l = 0, r = 0;
        while (l < left.length && r < right.length) {
            if (left[l] < right[r]) {
                result[i++] = left[l++];
            } else {
                result[i++] = right[r++];
            }
        }
        while (l < left.length) {
            result[i++] = left[l++];
        }
        while (r < right.length) {
            result[i++] = right[r++];
        }
        return result;
    }

    // 希尔排序
    public static void shellSort(int[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                for (int j = i; j >= gap; j -= gap) {
                    if (arr[j] < arr[j - gap]) {
                        int temp = arr[j];
                        arr[j] = arr[j - gap];
                        arr[j - gap] = temp;
                    }
                }
            }
        }
    }

    // 快速排序
    public static void quickSort(int[] arr, int first, int last) {
        if (first >= last) {
            return;
        }
        int midValue = arr[first];
        int low = first;
        int high = last;
        while (low < high) {
            while (low < high && arr[high] >= midValue) {
                high--;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] < midValue) {
                low++;
            }
            arr[high] = arr[low];
        }
        arr[low] = midValue;
        quickSort(arr, first, low - 1);
        quickSort(arr, low + 1, last);
    }

    // 插入排序
    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0; j--) {
                if (arr[j - 1] > arr[j]) {
                    int temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                } else {
                    break;
                }
            }
        }
    }

    // 选择排序
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }
}

 

posted @ 2018-11-12 15:49  人工智能之路上的菜鸡  阅读(607)  评论(0编辑  收藏  举报