七大排序算法代码

package leetcode;

import java.util.Arrays;


public class Sort {
    /*
    * 1. 直接插入排序
    * */
    public static int[] insertionSort(int[] nums) {
        int n = nums.length;
        for (int i = 1; i < n; i++) {
            if (nums[i] < nums[i-1]) {
                int x = nums[i];
                int j = i - 1;
                while (j >= 0 && x < nums[j]) {
                    nums[j+1] = nums[j];
                    j--;
                }
                nums[j+1] = x;
            }
        }
        return nums;
    }

    /*
    * 2. 希尔排序
    * */
    public static void shellSortInsert(int[] nums, int dk) {
        int n = nums.length;
        for (int i = dk; i < n; i++) {
            if (nums[i] < nums[i-dk]) {
                int guard = nums[i];
                int j = i - dk;
                while (j >= 0 && guard < nums[j]) {
                    nums[j+dk] = nums[j];
                    j -= dk;
                }
                nums[j+dk] = guard;
            }
        }
    }
    public static int[] shellSort(int[] nums) {
        int n = nums.length;
        int dk = n / 2;
        while (dk >= 1) {
            shellSortInsert(nums, dk);
            dk /= 2;
        }
        return nums;
    }

    /*
    * 3. 简单选择排序
    * */
    public static int[] simpleSelectionSort(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int min_j = i;
            for (int j = i + 1; j < n; j++) {
                if (nums[j] < nums[min_j]) {
                    min_j = j;
                }
            }
            if (min_j != i) {
                int tmp = nums[i];
                nums[i] = nums[min_j];
                nums[min_j] = tmp;
            }
        }
        return nums;
    }

    /*
    * 4. 冒泡排序
    * */
    public static int[] bubbleSort(int[] nums) {
        int n = nums.length;
        int i = n - 1;
        int swap;
        while (i > 0) {
            swap = 0;
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[j+1]) {
                    swap = j;
                    int tmp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = tmp;
                }
            }
            i = swap;
        }
        return nums;
    }

    /*
    * 5. 堆排序:最小堆
    * */
    private static int n = 0; //数组大小
    /*
    * 插入元素
    * */
    public static int[] minHeapAdd(int[] nums, int val) {
        nums[n] = val;
        n++;
        int i = n - 1;
        int p = n / 2 - 1;
        int tmp = nums[i];
        while (p >= 0 && tmp < nums[p]) {
            nums[i] = nums[p];
            i = p;
            p = (i - 1) / 2;
        }
        nums[i] = tmp;
        return nums;
    }

    /*
    * 删除元素
    * */
    public static int minHeapDelete(int[] nums) {
        int ans = nums[0];
        nums[0] = nums[n-1];
        n--;
        minHeapAdjust(nums, 0);
        return ans;
    }

    /*
    * 调整最小堆
    * */
    public static void minHeapAdjust(int[] nums, int i) {
        int tmp = nums[i];
        while (2 * i + 1 < n) {
            int child = 2 * i + 1;
            if (child + 1 < n && nums[child+1] < nums[child]) child++;
            if (tmp <= nums[child]) break;
            nums[i] = nums[child];
            i = child;
        }
        nums[i] = tmp;
    }

    /*
    * 初始化最小堆
    * */
    public static void initMinHeap(int[] nums) {
        for (int i = n / 2 - 1; i >= 0; i--) {
            minHeapAdjust(nums, i);
        }
    }
    public static int[] heapSort(int[] nums) {
        n = nums.length;
        initMinHeap(nums);
        return nums;
    }

    /*
    * 6. 快速排序
    * */
    public static void quickSort2(int[] nums, int left, int right) {
        if (left >= right) return;
        int l = left;
        int r = right;
        int guard = nums[left];
        while (true) {
            while (l < r && nums[r] > guard) r--;
            while (l < r && nums[l] <= guard) l++;
            if (l >= r) break;
            int tmp = nums[l];
            nums[l] = nums[r];
            nums[r] = tmp;
        }
        nums[left] = nums[r];
        nums[r] = guard;
        quickSort2(nums, left, r - 1);
        quickSort2(nums, r + 1, right);
    }
    public static int[] quickSort(int[] nums) {
        quickSort2(nums, 0, nums.length - 1);
        return nums;
    }

    /*
    * 7. 归并排序
    * */
    public static void mergeArray(int[] nums, int first, int mid, int last, int[] temp) {
        int k = first;
        int i = first;
        int j = mid + 1;
        while (i <= mid && j <= last) {
            if(nums[i] < nums[j]) {
                temp[k++] = nums[i++];
            }else {
                temp[k++] = nums[j++];
            }
        }
        while (i <= mid) temp[k++] = nums[i++];
        while (j <= last) temp[k++] = nums[j++];
        for (int t = first; t <= last; t++) {
            nums[t] = temp[t];
        }
    }
    public static void mergeSort2(int[] nums, int first, int last, int[] temp) {
        if (first >= last) return;
        int mid = (first + last) / 2;
        mergeSort2(nums, first, mid, temp);
        mergeSort2(nums, mid + 1, last, temp);
        mergeArray(nums, first, mid, last, temp);
    }
    public static int[] mergeSort(int[] nums) {
        int n = nums.length;
        mergeSort2(nums, 0, n - 1, new int[n]);
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {4, 1, 9, 3, 10};
//        Sort.insertionSort(nums);
//        Sort.simpleSelectionSort(nums);
//        Sort.bubbleSort(nums);
//        Sort.heapSort(nums);
//        while (n > 0) {
//            System.out.println(Sort.minHeapDelete(nums));
//        }
        Sort.shellSort(nums);
        System.out.println(Arrays.toString(nums));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值