堆排序(2)

//构建大顶堆,依次取出堆顶值(最大值),即可有序排列
//该算法需要一个顶堆数据结构,排列的结果会改变相同值的相对顺序

 class HeapSort2 {
    public static void main(String[] args)
    {
        int[] heap=createHeap(new int[]{5, 2, 7, 3, 6, 1, 4, 6});
        sort(heap,0);
        println(heap);
    }

    private static void println(int[] arr)
    {
        for (int i : arr) {
            System.out.print(" "+i);
        }
        System.out.println();
    }
    private static int[] createHeap(int[] randomArray)
    {
        int[] heap=new int[randomArray.length];

        for(int index=0;index<randomArray.length;index++)
        {
            addElement(heap,index-1,randomArray[index]);
        }
        return heap;
    }

    private static void addElement(int[] src,int lastIndex,int element)
    {
        src[++lastIndex]=element;
        int parentIndex=lastIndex;
        int currentIndex=lastIndex;
        while (currentIndex>0)
        {
            parentIndex=(lastIndex-1)/2;
            int parentValue=src[parentIndex];
            //大的向上冒泡
            if(element>parentValue)
            {
                src[parentIndex]=element;
                src[currentIndex]=parentValue;
                currentIndex=parentIndex;
            }else break;
        }
    }

    private static void sort(int [] src,int poppedCount)
    {
       if (poppedCount<src.length)
        {
            poppedCount++;
            int ele=src[0];
            int lastIndex=src.length-poppedCount;
            //尾结点替换堆顶
            src[0]=src[lastIndex];
            src[lastIndex]=ele;

            //堆顶下沉
            int childIndex=0;
            int currentIndex=0;
            lastIndex=lastIndex-1;
            while (currentIndex<=lastIndex) {
                int childIndexLeft = currentIndex * 2 + 1;
                if (childIndexLeft > lastIndex)
                    break;

                int childIndexRight = currentIndex * 2 + 2;

                int childLeftValue = src[childIndexLeft];
                int desChildIndex=childIndexLeft;
                int desChildValue =childLeftValue;
                if (childIndexRight <= lastIndex)
                {
                    int childRightValue = src[childIndexRight];
                    if(childLeftValue < childRightValue)
                    {
                        desChildIndex =childIndexRight;
                        desChildValue =childRightValue;
                    }
                }

                int parentValue = src[currentIndex];
                //下沉
                if (parentValue != Math.max(parentValue, desChildValue)) {

                    src[desChildIndex] = parentValue;
                    src[currentIndex] = desChildValue;
                    currentIndex = desChildIndex;

                } else break;
            }
            sort(src,poppedCount);
        }

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值