For example, in Java syntax, it will be something like:
List l = new ArrayList();
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(6));
l.add(new Integer(9));
Random rand = new Random();
for (int i=0; i < n; i++) {
l.add(new Integer(rand.nextInt(1000)));
}
Collections.sort(l);
l.remove(0);
But it seems it's inefficient. Any better algorithm?
Use a binary insert (works like a binary search) for the new value. Discard the smallest. Should be quite fast.
By the way - this can be implemented as a handy extension method:
private static int GetSortedIndex( this IList list, IComparer comparer, object item, int startIndex, int endIndex )
{
if( startIndex > endIndex )
{
return startIndex;
}
var midIndex = startIndex + ( endIndex - startIndex ) / 2;
return comparer.Compare( list[midIndex], item ) < 0 ?
GetSortedIndex( list, comparer, item, midIndex + 1, endIndex ) :
GetSortedIndex( list, comparer, item, startIndex, midIndex - 1 );
}
public static void InsertSorted( this IList list, IComparer comparer, object item )
{
list.Insert( list.GetSortedIndex( comparer, item ), item );
}
Java Equivalent
public static void main(String[] args)
{
List l = new ArrayList();
l.add(new Integer(2));
l.add(new Integer(3));
l.add(new Integer(6));
l.add(new Integer(9));
Random rand = new Random();
for (int i=0; i < 10; i++) {
Integer rnd = new Integer(rand.nextInt(1000));
int pos = Collections.binarySearch(l,rnd);
if(pos < 0) pos = ~pos;
l.add(pos,rnd);
}
System.out.println(l);
}
Use a TreeSet instead of a List, it'll maintain the order such that such that the largest value will always be at SortedSet#last(). If using 1.6+ you can use NavigableSet methods; pollLast() will return and remove the highest value.
NavigableSet<Integer> set = new TreeSet<Integer>();
//... setup data
Integer highest = set.pollLast();
set.add(rand.nextInt(1000));
Integer newHighest = set.pollLast();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With