Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the K "smallest" elements from a list of objects?

I'm looping through a List to find a particular entry, then assigning that to a variable and trying to remove it later. It's easier to demo than to explain.

ArrayList<Example> list1 = populate();

Example ex1 = list1.get(0);
Example ex2 = ex1;
list1.remove(ex2);

I know this likely has something to do with Java's inability to handle pointers, but a viable solution would be great.

Edit: To elaborate, this is a brief example of my code rather than giving you the full thing. What I'm doing is iterating through a list to find the lowest 10 numbers. My technique is to go through the list, find the lowest and add it to another list, then remove that number from the original list and repeat. But my list is made of objects which have an int value inside them, rather than a list of integers.

for(0 to 9){
    for(0 to list.size){
        if(list.get(x) < smallest)
            smallest = list.get(x)
    }
    smallestList.add(smallest);
    list.remove(smallest)
}
like image 842
user2341412 Avatar asked May 03 '13 11:05

user2341412


People also ask

How do you find the smallest element in an array K?

K'th smallest element in an unsorted array using sorting:Sort the given array and return the element at index K-1 in the sorted array. Follow the given steps to solve the problem: Sort the input array in the increasing order. Return the element at the K-1 index (0 – Based indexing) in the sorted array.

How do you select the kth smallest number among n numbers?

The basic idea here is to create a min-heap of all n elements and then extract the minimum element K times. The last element to be extracted will be the Kth smallest element. Extract the minimum elements K-1 times, i.e. delete the root and perform heapify operation K times.

How do you find the kth smallest element in a min-heap?

Naive approach: We can extract the minimum element from the min-heap k times and the last element extracted will be the kth least element. Each deletion operation takes O(log n) time, so the total time complexity of this approach comes out to be O(k * log n).


2 Answers

I would sort the list. Then, I would create a list with those 10 smallest objects and change the original list list1 to contain the remaining objects. Something like:

Collection.sort(list1);
ArrayList<Example> yourSmallestElements = (ArrayList<Example>)(list1.sublist(0, 9).clone());
list1.removeAll(yourSmallestElements);

NOTE: I cloned the sublist because sublist() only returns a view of the list list1, and that's not what you want here.

Your class Example can implement "Comparable" so that you can define how they need to be compared. You will need to implement the method compareTo(). Something like this:

public class Example implements Comparable<Example> {
    private int integerVal = <a value>;

    public int compareTo(Example exampleObject) {
        return exampleObject.integerVal - this.integerVal;
    }   
}

Have a look at this link, more precisely the class that begins as follows:

public class Fruit implements Comparable<Fruit>{
like image 150
JonasVautherin Avatar answered Oct 18 '22 08:10

JonasVautherin


If you want to sort your objects...

Example e;
int min=-1; // assuming the list has +ve numbers only
for (Example elem : yourList)
{
if ( elem.gtVaribale() <= min ) //assuming you have variable field in your object
{
  e = elem;
  min = elem.getVariable();
}
}
yourList.remove(e);

//repeat this for remaining elements of the list

//you can create another sorted list, and do sortedList.add(e), so that sortedList
//have objects in ascending order (of the variable you want to sort) of objects you had in yourList

This is just a pseudoCode and I have not compiled it.

like image 34
Bill Avatar answered Oct 18 '22 09:10

Bill