堆排序&快排的Java实现

堆排序的Java实现

以从小到大排序为例,堆排主要包括两个过程:
1、构建最大堆(以数组为堆的数据结构)
2、替换当前堆顶元素和堆尾元素,忽略已经有序的堆尾,重新调整堆,不断重复(其实还是不断get&&remove堆顶最大或最小值放在有序队列中的过程)

堆排还是比较简单的,过程可以参考 堆排序算法
直接上Java实现代码:

public class HeapSort {

    public static void main(String[] args) {
        int[] nums = {17, 8, 100, 120, 122, 6, 3, 9};
        heapSort(nums);
        for (int num : nums) {
            System.out.print(" " + num);
        }
    }

    public static void heapSort(int[] nums) {
        if (nums == null || nums.length <= 1)
            return;
        int len = nums.length;
        for (int i = len / 2 - 1; i >= 0; i--) {
            adjustHeap(len, nums, i);
        }
        for (int i = len - 1; i > 0; i--) {
            int temp = nums[i];
            nums[i] = nums[0];
            nums[0] = temp;
            adjustHeap(i, nums, 0);
        }
    }

    private static void adjustHeap(int size, int[] nums, int index) {
        if (index * 2 + 1 > size - 1)
            return;
        int k = index * 2 + 1;
        if (index * 2 + 2 < size && nums[index * 2 + 2] > nums[index * 2 + 1]) {
            k++;
        }
        if (nums[index] < nums[k]) {
            int temp = nums[index];
            nums[index] = nums[k];
            nums[k] = temp;
            adjustHeap(size, nums, k);
        }
    }

    //可以用sinkAdjust直接替换adjustHeap
    //sinkAdjust方法和adjustHeap方法类似,只不过去除了adjustHeap里的递归,直接进行下沉
    private static void sinkAdjust(int len, int[] nums, int i) {
        int k = i, temp = nums[i], index = 2 * k + 1;
        while (index < len) {
            if (index + 1 < len) {
                if (nums[index] < nums[index + 1]) {
                    index = index + 1;
                }
            }
            if (nums[index] > temp) {
                nums[k] = nums[index];
                k = index;
                index = 2 * k + 1;
            } else {
                break;
            }
        }
        nums[k] = temp;
    }

}

快排的Java实现

直接以第一个元素为分界元素,下面是直接Java代码实现

public class QuickSort {

    public static void main(String[] args){
        int[] nums = {17, 8, 100, 120, 122, 6, 3, 9};
        quickSort(nums);
        for (int num : nums) {
            System.out.print(" " + num);
        }
    }

    public static void quickSort(int[] nums){
        quickSort(nums,0,nums.length-1);
    }

    public static void quickSort(int[] nums,int start,int end){
        if(start>=end)
            return;
        int low=start;
        int high=end;
        int splitKey=nums[start];
        while(start<end){
            while(start<end&&nums[end]>=splitKey)
                end--;
            nums[start]=nums[end];
            while(start<end&&nums[start]<=splitKey)
                start++;
            nums[end]=nums[start];
        }
        nums[end]=splitKey;
        quickSort(nums,low,end-1);
        quickSort(nums,end+1,high);
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值