Finding minimum in list
03-25-2017, 03:10 PM
Post: #11
 Han Senior Member Posts: 1,882 Joined: Dec 2013
RE: Finding minimum in list
(03-25-2017 02:25 PM)DrD Wrote:
(03-22-2017 03:30 PM)Han Wrote:  In the event of two or more sublists having the same minimal first values, the first occurrence will be used. Did you have any other conditions for such cases?

My application needs the first occurrence of multiple sublists which have the same minimum first values. At first, I thought this is what the SORT command would do. However, I've found that isn't always true :

L0:={{10,1},{2,2},{2,4},{8,9},{8,1}};
SORT(L0,1); // ==> {{2,4},{2,2},{8,9},{8,1},{10,1}}

The set of sublists with 2 as the first value get reversed in the SORT procedure, whereas the set of sublists with 8 as the first value do not get reversed. SORT cannot be relied upon to return the first occurrence for same sublist first values. This behavior can be difficult to debug, in otherwise normally functional code!

SORT likely uses an unstable algorithm (perhaps they are using quicksort for speed and memory). The code I provided should be stable.

Graph 3D | QPI | SolveSys
 « Next Oldest | Next Newest »

 Messages In This Thread Finding minimum in list - DrD - 03-22-2017, 03:14 PM RE: Finding minimum in list - Han - 03-22-2017, 03:30 PM RE: Finding minimum in list - DrD - 03-22-2017, 05:29 PM RE: Finding minimum in list - Han - 03-22-2017, 05:42 PM RE: Finding minimum in list - Tyann - 03-23-2017, 05:38 AM RE: Finding minimum in list - DrD - 03-25-2017, 02:25 PM RE: Finding minimum in list - Han - 03-25-2017 03:10 PM RE: Finding minimum in list - Didier Lachieze - 03-22-2017, 04:06 PM RE: Finding minimum in list - Han - 03-22-2017, 04:11 PM RE: Finding minimum in list - Han - 03-22-2017, 04:32 PM RE: Finding minimum in list - DrD - 03-23-2017, 10:18 AM RE: Finding minimum in list - John P - 03-26-2017, 04:11 AM RE: Finding minimum in list - cyrille de brĂ©bisson - 03-28-2017, 10:30 AM RE: Finding minimum in list - DrD - 03-28-2017, 01:52 PM RE: Finding minimum in list - DrD - 03-29-2017, 10:54 AM RE: Finding minimum in list - Didier Lachieze - 03-29-2017, 06:50 PM

User(s) browsing this thread: 1 Guest(s)