简体   繁体   English

使用插入排序java删除重复项

[英]removing duplicates using insertion sort java

I am having half an issue with the following code, it is an insertion sort method used with the Comparable interface. 我的以下代码有一半问题,它是与Comparable接口一起使用的插入排序方法。 In this specific case it is supposed to sort elements in decreasing order, which it does fine. 在这种特定情况下,应该以递减的顺序对元素进行排序,这样做确实很好。 However, I am also trying to remove duplicates within the same method but it is not working. 但是,我也试图在同一方法中删除重复项,但是它不起作用。 I was wondering whether it is actually possible to do that within the same method? 我想知道实际上是否可以用相同的方法来做到这一点? I have looked at the answer at the following question Removing Duplicates Inside Insertion Sort but I am not sure how to apply it in here. 我已经查看了以下问题的答案,即“ 删除插入排序中的重复项”,但是我不确定如何在此处应用它。 Not necessarily looking for the solution but if you can point me into the right direction from where I can take it further. 不一定要寻找解决方案,但是如果您可以向我指出正确的方向,那么我可以采取进一步的措施。 Thanks in advance. 提前致谢。

public void InsertionSortDecrease(){
    for(int i=1;i<size();i++){
        T keyelement = get(i);
        int pos=i;
        while(pos > 0 && 
(((Comparable)keyelement).compareTo((Comparable)get(pos-1)) > 0)){
            set(pos,get(pos-1));
            pos--;
        }
        set(pos,keyelement);
        if(((Comparable)get(pos)).compareTo((Comparable)get(pos+1)) 
== 0){
            remove(pos);
        }
    }
}

I would personaly use a TreeSet which does what you want. 我会个人使用TreeSet来完成您想要的事情。

You can only add an item if it is not already present in the set which is always kept sorted. 如果项目始终不存在于集合中,则只能add该项目。

You are attempting to remove elements while sorting them. 您正在尝试对元素进行排序时删除它们。 For something like this an Iterator would be a lot better to avoid running into a concurrent modification error. 对于这样的事情,Iterator会更好,避免遇到并发的修改错误。

Your bug is you are counting up , but you should be counting down , because there's no more room to the left of the final result so you should be shuffling to the right as you find the insertion point. 你的错误是您计数 ,但你应该计算下来 ,因为没有更多的空间,最终结果的左侧,所以你应该为你找到插入点洗牌的权利

You'll then need to compare for duplicate the current and the previous (if it exists) positions, because you'll already know it's less than the next due to the terminating condition of the first loop. 然后,您需要比较当前位置和先前位置(如果存在)是否重复,因为由于第一个循环的终止条件,您已经知道它小于下一个位置。 But then you'll have to shuffle them all back if it's a dupe. 但是,如果这是骗子,则必须将它们全部洗掉。

Better, first find the insertion point, then if it's a dupe do nothing otherwise shuffle and insert. 更好的方法是,首先找到插入点,然后如果是重复项,则不进行任何操作,否则将随机播放并插入。 If you refactor your code in this way, you can then improve the insertion point finder from O(n) to O(log n) by doing a binary search because you know you are operating with sorted data. 如果以这种方式重构代码,则可以通过执行二进制搜索将插入点查找程序从O(n)改进为O(log n),因为您知道自己正在使用排序的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM