七种常见排序算法的java实现

包括冒泡、选择、插入、希尔、堆、归并、快排,归并和快排有递归和非递归两版。

public class Sort {
    //测试
    public static void main(String[] args) {
        int[] arr = {132, 991, 5, 4, 43, 44, 56, 33, 44, 64, 12, 53, 45, 32, 4, 553, 12, 32, 321, 124, 4, 12, 21, 3, 1, -5, -7};
        long t1 = System.nanoTime();
        quickSort_noRecursive(arr);
        long t2 = System.nanoTime();
        System.out.println("##耗时:" +  String.format("%.8fs", (t2 - t1) * 1e-9));
        for (int each : arr) {
            System.out.print(each + " ");
        }
    }

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

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

    //插入
    public static void insertSort(int[] arr) {
        int len = arr.length;
        int i, j;
        for (i = 1; i < len; i++) {
            int temp = arr[i];
            for (j = i; j > 0 && arr[j - 1] > temp; j--) {
                arr[j] = arr[j - 1];
            }
            arr[j] = temp;
        }
    }

    //希尔
    public static void shellSort(int[] arr) {
        int len = arr.length;
        int i, j;
        for (int increment = len / 2; increment > 0; increment /= 2) {
            for (i = increment; i < len; i++) {
                int temp = arr[i];
                for (j = i; j > increment - 1 && arr[j - increment] > temp; j -= increment) {
                    arr[j] = arr[j - increment];
                }
                arr[j] = temp;
            }
        }
    }

    //堆排序
    public static void heapSort(int arr[]) {
        int len = arr.length;
        for (int i = len / 2; i > 0; i--) {
            heapAdjust(arr, i, len);
        }
        for (int i = len; i > 1; i--) {
            int temp = arr[0];
            arr[0] = arr[i - 1];
            arr[i - 1] = temp;
            heapAdjust(arr, 1, i - 1);
        }
    }

    public static void heapAdjust(int arr[], int start, int end) {
        int temp = arr[start - 1];
        for (int i = 2 * start; i <= end; i *= 2) {
            if (i < end && arr[i - 1] < arr[i]) {
                i++;
            }
            if (temp > arr[i - 1]) {
                break;
            }
            arr[start - 1] = arr[i - 1];
            start = i;
        }
        arr[start - 1] = temp;
    }

    //归并(递归)
    public static void mergeSort_Recursive(int[] arr) {
        mSort(arr, 0, arr.length - 1, new int[arr.length]);
    }

    public static void mSort(int[] arr, int start, int end, int[] res) {
        if (start == end) {
            res[start] = arr[start];
        } else {
            int mid = (start + end) / 2;
            mSort(arr, start, mid, res);
            mSort(arr, mid + 1, end, res);
            merge(arr, start, end, res);
            for (int i = start; i <= end; i++)
                arr[i] = res[i];
        }
    }

    public static void merge(int[] arr, int start, int end, int[] res) {
        int start1 = start, start2 = (start + end) / 2 + 1;
        int end1 = (start + end) / 2, end2 = end;
        while (start1 <= end1 && start2 <= end2) {
            res[start++] = arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
        }
        while (start1 <= end1) {
            res[start++] = arr[start1++];
        }
        while (start2 <= end2) {
            res[start++] = arr[start2++];
        }
    }

    //归并(非递归)
    public static void mergeSort(int[] arr) {
        int[] res = new int[arr.length];
        for (int size = 1; size < 2 * arr.length; size *= 2) {
            for (int start = 0; start < arr.length; start += size) {
                if (size == 1) {
                    res[start] = arr[start];
                } else {
                    int s = start;
                    int mid = start + size / 2 < arr.length ? start + size / 2 - 1 : arr.length - 1;
                    int start1 = start, start2 = mid + 1, end1 = mid, end2 = start + size < arr.length ? start + size - 1 : arr.length - 1;
                    while (start1 <= end1 && start2 <= end2) {
                        res[s++] = arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
                    }
                    while (start1 <= end1) {
                        res[s++] = arr[start1++];
                    }
                    while (start2 <= end2) {
                        res[s++] = arr[start2++];
                    }
                }
            }
            for (int i = 0; i < arr.length; i++)
                arr[i] = res[i];
        }
    }

    //快排
    public static void quickSort(int[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }
    public static void quickSort(int[] arr, int left, int right) {
        if (left >= right) return;
        /*优化pivot选择,取左中右三数大小里中间那个
        int m=left+(right-left)/2;
        if(arr[left]<arr[m]) {
            arr[left]^=arr[m];
            arr[m]^=arr[left];
            arr[left]^=arr[m];
        }
        if(arr[m]<arr[right]){
            arr[m]^=arr[right];
            arr[right]^=arr[m];
            arr[m]^=arr[right];
        }
        if(arr[left]> arr[right]){
            arr[left]^=arr[right];
            arr[right]^=arr[left];
            arr[left]^=arr[right];
        }
        */
        int pivot = arr[left];
        int L = left, R = right;
        while (L < R) {
            while (L < R&&arr[R]>=pivot) R--;
                arr[L] = arr[R];
            while (L < R&&arr[L]<=pivot) L++;
                arr[R] = arr[L];
        }
        arr[L] = pivot;
        quickSort(arr, left, L-1);
        quickSort(arr, L + 1, right);
    }
    //快排非递归
    public static void quickSort_noRecursive(int[] arr) {
        quickSort_noRecursive(arr, 0, arr.length - 1);
    }

    public static void quickSort_noRecursive(int[] arr, int left, int right) {
        Deque<Integer> stack=new LinkedList<>();
        stack.push(right);
        stack.push(left);
        while(!stack.isEmpty()){
            int L=stack.remove();
            int R=stack.remove();
            left=L;right=R;
            int pivot=arr[L];
            while (L < R) {
                while (L < R&&arr[R]>=pivot) R--;
                arr[L] = arr[R];
                while (L < R&&arr[L]<=pivot) L++;
                arr[R] = arr[L];
            }
            arr[L] = pivot;
            if(left<L){
                stack.push(L-1);
                stack.push(left);
            }
            if(right>R){
                stack.push(right);
                stack.push(L+1);
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值