Popular recipes tagged "sorting" but not "functional"http://code.activestate.com/recipes/tags/sorting-functional/2016-04-14T12:58:40-07:00ActiveState Code RecipesFast min/max function (Python) 2011-10-22T18:40:32-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577916-fast-minmax-function/ <p style="color: grey"> Python recipe 577916 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/minmax/">minmax</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>Minimizes the number of comparisons to compute the minimum and maximum of a dataset. Uses 1.5 compares for every element, improving on the more obvious solution that does 2 comparisons per element.</p> Dependency resolution (Python) 2016-04-14T12:58:40-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/580642-dependency-resolution/ <p style="color: grey"> Python recipe 580642 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/dependency/">dependency</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>This recipe shows how to take a list of objects, each with their own list of dependencies, and resolve them to proper order. It includes some poor mans circular dependency detection (very poor mans).</p> Topological Sort (Python) 2012-09-27T12:21:23-07:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578272-topological-sort/ <p style="color: grey"> Python recipe 578272 by <a href="/recipes/users/4172262/">Sam Denton</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>, <a href="/recipes/tags/toposort/">toposort</a>). </p> <p>A topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a directed graph is a linear ordering of its vertices such that, for every edge uv, u comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).</p> lazy DSU (Python) 2012-09-12T04:49:59-07:00Chris Smithhttp://code.activestate.com/recipes/users/2725752/http://code.activestate.com/recipes/578259-lazy-dsu/ <p style="color: grey"> Python recipe 578259 by <a href="/recipes/users/2725752/">Chris Smith</a> (<a href="/recipes/tags/dsu/">dsu</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>sort a sequence by applying decoration only as needed</p> Fast Sorting of n dimensional array by first dimension (Python) 2012-03-14T15:14:11-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578074-fast-sorting-of-n-dimensional-array-by-first-dimen/ <p style="color: grey"> Python recipe 578074 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/argsort/">argsort</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array: <br /> ray = [[1,2,3,7,5][10,11,12,13,14]] </p> <p>I would want it to come out as ray = [[1,2,3,5,7][10,11,12,14,13]]</p> <p>There are several ways to do this. One is zipped = zip(<em>ray) zipped.sort() ray = zip(</em>zipped)</p> <p>but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.</p> <p>if the above were a numpy array you could simply do the following: indexes = numpy.argsort(ray[0]) for n in xrange(len(ray)) ray[n] = ray[n][indexes]</p> <p>I did a time test of the two methods below.</p> Sorting a dict's items and keys (Python) 2012-02-04T04:23:01-08:00George V. Reillyhttp://code.activestate.com/recipes/users/4161272/http://code.activestate.com/recipes/578031-sorting-a-dicts-items-and-keys/ <p style="color: grey"> Python recipe 578031 by <a href="/recipes/users/4161272/">George V. Reilly</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p><a href="https://github.com/collective/icalendar">icalendar</a> uses its own CaselessDict as the base of many classes. I needed to produce the keys and items in a canonical order, so that certain keys would appear first.</p> Sleepsort (Python) 2011-06-17T05:17:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577756-sleepsort/ <p style="color: grey"> Python recipe 577756 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>Funky sort routine to demonstrate Python's threading basics.</p> Sleepsort with processes and pipes (Python) 2011-06-17T02:37:58-07:00Benjamin Petersonhttp://code.activestate.com/recipes/users/4170802/http://code.activestate.com/recipes/577758-sleepsort-with-processes-and-pipes/ <p style="color: grey"> Python recipe 577758 by <a href="/recipes/users/4170802/">Benjamin Peterson</a> (<a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/unix/">unix</a>). Revision 2. </p> <p>Sleepsort is a sorting algorithm that uses the system sleep syscall in a very creative fashion.</p> <p>This is the same algorithm as <a href="http://code.activestate.com/recipes/577756/">recipe 577756</a> but using *nix processes instead of threads.</p> Topological Sort (Python) 2010-09-28T19:22:27-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/577413-topological-sort/ <p style="color: grey"> Python recipe 577413 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/topological/">topological</a>). </p> <p>Given items that depend on other items, a topological sort arranges items in order that no one item precedes an item it depends on. In this example items are strings and dependencies are expressed in a dictionary whose keys are items and whose values are a set of dependent items. Note that the dict may contain self-dependencies (which are ignored), and dependent items that are not also dict keys, such as the item 'ieee'.</p> Sorting big files the Python 2.6 way (Python) 2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/ <p style="color: grey"> Python recipe 576755 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p> Total Ordering: Class Decorator for Filling in Rich Comparison Methods When Only One is Implemented (Python) 2008-10-28T11:54:42-07:00Michael Foordhttp://code.activestate.com/recipes/users/2183852/http://code.activestate.com/recipes/576529-total-ordering-class-decorator-for-filling-in-rich/ <p style="color: grey"> Python recipe 576529 by <a href="/recipes/users/2183852/">Michael Foord</a> (<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/ordering/">ordering</a>, <a href="/recipes/tags/sorting/">sorting</a>). Revision 5. </p> <p><code>total_ordering</code> and <code>force_total_ordering</code> are class decorators for Python 2.6 &amp; Python 3.</p> <p>They provides <em>all</em> the rich comparison methods on a class by defining <em>any</em> one of '__lt__', '__gt__', '__le__', '__ge__'.</p> <p><code>total_ordering</code> fills in all unimplemented rich comparison methods, assuming at least one is implemented. <code>__lt__</code> is taken as the base comparison method on which the others are built, but if that is not available it will be constructed from the first one found.</p> <p><code>force_total_ordering</code> does the same, but having taken a comparison method as the base it fills in <em>all</em> the others - this overwrites additional comparison methods that may be implemented, guaranteeing consistent comparison semantics.</p> Sorting a list of dictionary based on a key value (Python) 2008-08-20T03:30:37-07:00riteshnhttp://code.activestate.com/recipes/users/4166555/http://code.activestate.com/recipes/576433-sorting-a-list-of-dictionary-based-on-a-key-value/ <p style="color: grey"> Python recipe 576433 by <a href="/recipes/users/4166555/">riteshn</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>Here is a quick and dirty way to sort a list of dictionary based on a key value.</p>