通用堆排序算法B

欢迎回到“排序”文章。

为了完整性,您可以直接复制并粘贴此排序实用程序

在您自己的秘密技巧中,这是整个课程:


public final class HeapSort { 
    private HeapSort() { } // no istances of this class possible 
    // given a Sortable, build a heap starting at node i. There a n nodes in total
    private static void heapify(Sortable s, int p, int n) { 
        for (int r, l= (p<<1)+1; l < n; p= l, l= (p<<1)+1) { 
            // l is the maximum of l and r, the two subnodes of p 
            if ((r= l+1) < n && s.compare(l, r) < 0) l= r; 
            // check if parent p is less than maximum l
            if (s.compare(p, l) < 0) s.swap(p, l);
            else break;
        } 
    // build a heap out of the Sortable in place
    private static void phase1(Sortable s) { 
        // heapify all the non-leaf nodes         
        for (int n= s.length(), p= n/2; p >= 0; p--)
            heapify(s, p, n);
    } 
    // sort the Sortable
    private static void phase2(Sortable s) { 
        for (int n= s.length(); --n > 0; ) {
            s.swap(0, n);         // put the root element in its place
            heapify(s, 0, n);     // and restore the heap again
        }        
    } 
    // driver for the worked methods
    public static Sortable sort(Sortable s) {  
        phase1(s);     // build initial heap
        phase2(s);     // sort the sortable given the heap 
        return s;     // return the Sortable for convenience
    }
} 
在我的VI文本编辑器中,此类大约需要一页文本。 让我们退后一步

这段代码中的一点点,实现了我们刚刚创建的内容:这种算法是

运行时非常高效且稳定。 此实现不知道

关于需要精确排序的任何内容,即仅使用Sortable

接口。

该类是最终的,包含一个私有的默认构造函数,因此没有

其他任何人创建HeapSort对象的实例的方式; 因为没有这样

需要对象:此类是仅具有功能的实用程序类。

第一篇文章对堆排序算法本身进行了一些解释。

现在让我们回到最初的问题:给定以下两个数组:


String[] first= { "Fred", "Barney", "Wilma", "Betty", "Bambam", "Pebbles" };
String[] last= { "Flintstone", "Rubble", "FlintStone", "Rubble", "Rubble", "Flintstone" }; 
我们必须创建一个Sortable,它可以返回名字/姓氏的数量,

可以比较两个名字(例如),并且可以交换两个名字

名字和姓氏数组。 我们需要根据以下两个数组进行排序

名字 另一个数组只需要“跟随”,即它的姓氏必须

排序完成后对应于名字。

这是没有很多评论的完整示例:


public class Flintstones implements Sortable { 
    private String[] first;
    private String[] last; 
    public FlintStones(String[] first, String[] last) { 
        this.first= first;
        this.last = last;
    } 
    public int length() { // interface implementation 
        return first.length; 
    }  
    public int compare(int i, int j) { // interface implementation 
        return first[i].compareTo(first[j]);
    } 
    private void swap (String[] s, int i, int j) { // private helper method 
        String t= s[i]; s[i]= s[j]; s[j]= t; 
    }
    public void swap(int i, int j) { // interface implementation 
        swap(first, i, j);
        swap(last, i, j);
    } 
    public static void main(String[] args) { 
        String[] first= { "Fred", "Barney", "Wilma", "Betty", "Bambam", "Pebbles" };
        String[] last= { "Flintstone", "Rubble", "FlintStone", "Rubble", "Rubble", "Flintstone" }; 
        HeapSort.sort(new ArraySortable(first, last)); 
        for (int i= 0; i < first.length; i++)
            System.out.println(first[i]+" "+last[i]);
    }
} 
那么,“本周技巧”到底是什么呢? 它表明你不能排序

只是数组或列表,但是任何实现Sortable接口的东西。 它

还显示了堆排序算法的一些细节。

如果我现在不被踢出去,我想告诉您一些有关排列的信息

和下周技巧中的组合以及如何有效地进行组合

产生没有太多麻烦。

亲切的问候,

乔斯

From: https://bytes.com/topic/java/insights/739752-generic-heap-sort-algorithm-b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值