Java-快排-堆排-归并

这篇博客深入介绍了三种常见的排序算法:快速排序、堆排序和归并排序。快速排序通过选取基准元素进行分区,然后递归排序两部分;堆排序利用最大堆或最小堆的性质,调整数组使其满足堆特性;归并排序则采用分治策略,将数组分为两半,分别排序后再合并。这些算法各有特点,在不同场景下有不同的效率表现。
摘要由CSDN通过智能技术生成

快速排序


public class SortArray {
    public int[] sortArray(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
        return nums;
    }

    public void quickSort(int[] nums, int left, int right) {
        if (left < right) {
            int index = partition(nums, left, right);
            quickSort(nums, left, index - 1);
            quickSort(nums, index + 1, right);
        }
    }

    public int partition(int[] nums, int l, int r) {
        int k = new Random().nextInt(r - l + 1) + l;
        swap(nums, k, r);
        int pivot = nums[r];
        while (l < r) {
            while (l < r && nums[l] <= pivot) l++;
            nums[r] = nums[l];
            while (l < r && nums[r] > pivot) r--; //注意这里是r--
            nums[l] = nums[r];
        }
        nums[r] = pivot;
        return r;
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

堆排序

package com.mk.www;

public class HeapSort {
    public int[] SortArray(int[] nums) {
        heapSort(nums);
        return nums;
    }

    public void heapSort(int[] nums) {
        int n = nums.length;
        buildMaxHeap(nums, n);
        for (int i = n - 1; i >= 1; --i) {
            swap(nums, 0, i);
            n--;
            adjustHeap(nums, 0, n);
        }
    }
    public void buildMaxHeap(int[] nums, int len) {
        for (int i = len / 2; i >= 0; --i) {
            adjustHeap(nums, i, len);
        }
    }

    public void adjustHeap(int[] nums, int root, int len) {
        for (; (root << 1) + 1 < len;) {
            int l = (root << 1) + 1;
            int r = (root << 1) + 2;
            int large = root;
            if (nums[l] > nums[large]) large = l;
            if (r < len && nums[r] > nums[large]) large = r;
            if (large != root) {
                swap(nums, large, root);
                root = large;
            } else {
                break;
            }
        }
    }

    public void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

归并排序

public class MergeSort {
    public int[] sortArray(int[] nums) {
        mergeSort(nums, 0, nums.length - 1);
        return nums;
    }

    public void mergeSort(int[] nums, int l, int r) {
        if (l >= r) return;
        int mid = (l + r) / 2;
        mergeSort(nums, l, mid);
        mergeSort(nums, mid + 1, r);
        int[] b = new int[nums.length];
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (nums[i] < nums[j]) {
                b[k++] = nums[i++];
            } else {
                b[k++] = nums[j++];
            }
        }
        while (i <= mid) b[k++] = nums[i++];
        while (j <= r) b[k++] = nums[j++];
        for (int p = 0; p < k; p++) {
            nums[p + l] = b[p];
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值