Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 The Perl idiom  





2 Efficiency analysis  





3 Example  





4 History  





5 Comparison to other languages  





6 References  





7 External links  














Schwartzian transform






Català

Polski
Русский
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikibooks
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 

(Redirected from Schwartzian Transform)

Incomputer programming, the Schwartzian transform is a technique used to improve the efficiency of sorting a list of items. This idiom[1] is appropriate for comparison-based sorting when the ordering is actually based on the ordering of a certain property (the key) of the elements, where computing that property is an intensive operation that should be performed a minimal number of times. The Schwartzian transform is notable in that it does not use named temporary arrays.

The Schwartzian transform is a version of a Lisp idiom known as decorate-sort-undecorate, which avoids recomputing the sort keys by temporarily associating them with the input items. This approach is similar to memoization, which avoids repeating the calculation of the key corresponding to a specific input value. By comparison, this idiom assures that each input item's key is calculated exactly once, which may still result in repeating some calculations if the input data contains duplicate items.

The idiom is named after Randal L. Schwartz, who first demonstrated it in Perl shortly after the release of Perl 5 in 1994. The term "Schwartzian transform" applied solely to Perl programming for a number of years, but it has later been adopted by some users of other languages, such as Python, to refer to similar idioms in those languages. However, the algorithm was already in use in other languages (under no specific name) before it was popularized among the Perl community in the form of that particular idiom by Schwartz. The term "Schwartzian transform" indicates a specific idiom, and not the algorithm in general.

For example, to sort the word list ("aaaa","a","aa") according to word length: first build the list (["aaaa",4],["a",1],["aa",2]), then sort it according to the numeric values getting (["a",1],["aa",2],["aaaa",4]), then strip off the numbers and you get ("a","aa","aaaa"). That was the algorithm in general, so it does not count as a transform. To make it a true Schwartzian transform, it would be done in Perl like this:

@sorted = map  { $_->[0] }
          sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } # Use numeric comparison, fall back to string sort on original
          map  { [$_, length($_)] }    # Calculate the length of the string
               @unsorted;

The Perl idiom

[edit]

The general form of the Schwartzian transform is:

@sorted = map  { $_->[0] }
          sort { $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] }
          map  { [$_, foo($_)] }
               @unsorted;

Here foo($_) represents an expression that takes $_ (each item of the list in turn) and produces the corresponding value that is to be compared in its stead.

Reading from right to left (or from the bottom to the top):

The use of anonymous arrays ensures that memory will be reclaimed by the Perl garbage collector immediately after the sorting is done.

Efficiency analysis

[edit]

Without the Schwartzian transform, the sorting in the example above would be written in Perl like this:

@sorted = sort { foo($a) cmp foo($b) } @unsorted;

While it is shorter to code, the naive approach here could be much less efficient if the key function (called foo in the example above) is expensive to compute. This is because the code inside the brackets is evaluated each time two elements need to be compared. An optimal comparison sort performs O(n log n) comparisons (where n is the length of the list), with 2 calls to foo every comparison, resulting in O(n log n) calls to foo. In comparison, using the Schwartzian transform, we only make 1 call to foo per element, at the beginning map stage, for a total of n calls to foo.

However, if the function foo is relatively simple, then the extra overhead of the Schwartzian transform may be unwarranted.

Example

[edit]

For example, to sort a list of files by their modification times, a naive approach might be as follows:

 function naiveCompare(file a, file b) {
     return modificationTime(a) < modificationTime(b)
 }
 
 // Assume that sort(list, comparisonPredicate) sorts the given list using
 // the comparisonPredicate to compare two elements.
 sortedArray := sort(filesArray, naiveCompare)

Unless the modification times are memoized for each file, this method requires re-computing them every time a file is compared in the sort. Using the Schwartzian transform, the modification time is calculated only once per file.

A Schwartzian transform involves the functional idiom described above, which does not use temporary arrays.

The same algorithm can be written procedurally to better illustrate how it works, but this requires using temporary arrays, and is not a Schwartzian transform. The following example pseudo-code implements the algorithm in this way:

 for each file in filesArray
     insert array(file, modificationTime(file)) at end of transformedArray
 
 function simpleCompare(array a, array b) {
     return a[2] < b[2]
 }
 
 transformedArray := sort(transformedArray, simpleCompare)
 
 for each file in transformedArray
     insert file[1] at end of sortedArray

History

[edit]

The first known online appearance of the Schwartzian transform is a December 16, 1994 posting by Randal Schwartz to a thread in comp.unix.shell Usenet newsgroup, crossposted to comp.lang.perl. (The current version of the Perl Timeline is incorrect and refers to a later date in 1995.) The thread began with a question about how to sort a list of lines by their "last" word:

adjn:Joshua Ng
adktk:KaLap Timothy Kwong
admg:Mahalingam Gobieramanan
admln:Martha L. Nangalama

Schwartz responded with:

#!/usr/bin/perl
require 5; # New features, new bugs!
print
    map { $_->[0] }
    sort { $a->[1] cmp $b->[1] }
    map { [$_, /(\S+)$/] }
    <>;

This code produces the result:

admg:Mahalingam Gobieramanan
adktk:KaLap Timothy Kwong
admln:Martha L. Nangalama
adjn:Joshua Ng

Schwartz noted in the post that he was "Speak[ing] with a lisp in Perl", a reference to the idiom's Lisp origins.

The term "Schwartzian transform" itself was coined by Tom Christiansen in a follow-up reply. Later posts by Christiansen made it clear that he had not intended to name the construct, but merely to refer to it from the original post: his attempt to finally name it "The Black Transform" did not take hold ("Black" here being a pun on "schwar[t]z", which means black in German).

Comparison to other languages

[edit]

Some other languages provide a convenient interface to the same optimization as the Schwartzian transform:

References

[edit]
  1. ^ Martelli, Alex; Ascher, David, eds. (2002). "2.3 Sorting While Guaranteeing Sort Stability". Python Cookbook. O'Reilly & Associates. p. 43. ISBN 0-596-00167-3. This idiom is also known as the 'Schwartzian transform', by analogy with a related Perl idiom.
  • ^ "How To/Sorting/Decorate Sort Undecorate".
  • ^ "Ruby-doc Core-API Classes". Retrieved 14 September 2011.
  • [edit]
    Retrieved from "https://en.wikipedia.org/w/index.php?title=Schwartzian_transform&oldid=1234679303"

    Categories: 
    Sorting algorithms
    Perl
    Hidden categories: 
    Articles with short description
    Short description matches Wikidata
    Articles lacking in-text citations from February 2024
    All articles lacking in-text citations
    Articles with example Perl code
    Articles with example Racket code
     



    This page was last edited on 15 July 2024, at 16:05 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki