|
|
Line 1: |
Line 1: |
| {{Infobox Algorithm
| | Alyson Meagher is the name her parents gave her but she doesn't like when individuals use her full title. I've usually loved living in Alaska. The preferred pastime for him and his kids is to perform lacross and he'll be beginning something else along with it. Invoicing is my occupation.<br><br>My webpage - [http://myoceancounty.net/groups/apply-these-guidelines-when-gardening-and-grow/ accurate psychic predictions] |
| |class=[[Sorting algorithm]]
| |
| |image=[[File:Odd even sort animation.gif|Example of odd-even transposition sort sorting a list of random numbers.|Example of odd-even transposition sort sorting a list of random numbers.]]
| |
| |caption=Example of odd-even transposition sort sorting a list of random numbers.
| |
| |data=[[Array data structure|Array]]
| |
| |time=<math>O(n^2)</math>
| |
| |best-time=<math>O(n)</math>
| |
| |space= <math>O(1)</math>
| |
| |optimal=No
| |
| }}
| |
| | |
| In computing, an '''odd–even sort''' or '''odd–even transposition sort''' (also known as '''brick sort'''<ref>{{cite web|last=Phillips|first=Malcolm|title=Array Sorting|url=http://homepages.ihug.co.nz/~aurora76/Malc/Sorting_Array.htm#Exchanging Sort Techniques|accessdate=3 August 2011}}</ref>) is a relatively simple [[sorting algorithm]], developed originally for use on parallel processors with local interconnections. It is a [[comparison sort]] related to [[bubble sort]], with which it shares many characteristics. It functions by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order (the first is larger than the second) the elements are switched. The next step repeats this for (even, odd)-indexed pairs (of adjacent elements). Then it alternates between (odd, even) and (even, odd) steps until the list is sorted.
| |
| | |
| ==Sorting on processor arrays==
| |
| | |
| On parallel processors, with one value per processor and only local left–right neighbor connections, the processors all concurrently do a compare–exchange operation with their neighbors, alternating between odd–even and even–odd pairings. This algorithm was originally presented, and shown to be efficient on such processors, by Habermann in 1972.<ref>N. Haberman (1972) "Parallel Neighbor Sort (or the Glory of the Induction Principle)," CMU Computer Science Report (available as Technical report AD-759 248, National Technical Information Service, US Department of Commerce, 5285 Port Royal Rd Sprigfield VA 22151).</ref>
| |
| | |
| The algorithm extends efficiently to the case of multiple items per processor. In the Baudet–Stevenson odd–even merge-splitting algorithm, each processor sorts its own sublist at each step, using any efficient sort algorithm, and then performs a merge splitting, or transposition–merge, operation with its neighbor, with neighbor pairing alternating between odd–even and even–odd on each step.<ref>
| |
| {{citation
| |
| | journal = Advances in computers
| |
| | editors = Franz L. Alt and Marshall C. Yovits
| |
| | title = Parallel Sorting Algorithms
| |
| | author = S. Lakshmivarahan, S. K. Dhall, and L. L. Miller
| |
| | volume = 23
| |
| | publisher = Academic Press
| |
| | pages = 295–351
| |
| | isbn = 978-0-12-012123-6
| |
| | year = 1984
| |
| | url = http://books.google.com/books?id=Mo2Q-TEwKGUC&pg=PA322
| |
| }}</ref>
| |
| | |
| ==Batcher's odd–even mergesort==
| |
| | |
| A related but more efficient sort algorithm is the [[Batcher odd–even mergesort]], using compare–exchange operations and perfect-shuffle operations.<ref>
| |
| {{cite book
| |
| | title = Algorithms in Java, Parts 1-4
| |
| | edition = 3rd
| |
| | author = Robert Sedgewick
| |
| | publisher = Addison-Wesley Professional
| |
| | year = 2003
| |
| | isbn = 978-0-201-36120-9
| |
| | pages = 454–464
| |
| | url = http://books.google.com/books?id=hyvdUQUmf2UC&pg=PA455
| |
| }}</ref>
| |
| Batcher's method is efficient on parallel processors with long-range connections.<ref>
| |
| {{cite book
| |
| | title = Encyclopedia of Computer Science and Technology: Supplement 14
| |
| | author = Allen Kent and James G. Williams
| |
| | publisher = CRC Press
| |
| | year = 1993
| |
| | isbn = 978-0-8247-2282-1
| |
| | pages = 33–38
| |
| | url = http://books.google.com/books?id=F9Y4oZ9qZnYC&pg=PA33
| |
| }}</ref>
| |
| | |
| == Algorithm ==
| |
| | |
| The single-processor algorithm, like [[bubblesort]], is simple but not very efficient. Here a [[zero-based]] index is assumed:
| |
| | |
| <source lang="javascript"> | |
| /* Assumes a is an array of values to be sorted. */
| |
| var sorted = false;
| |
| while(!sorted)
| |
| {
| |
| sorted=true;
| |
| for(var i = 1; i < list.length-1; i += 2)
| |
| {
| |
| if(a[i] > a[i+1])
| |
| {
| |
| swap(a, i, i+1);
| |
| sorted = false;
| |
| }
| |
| }
| |
| | |
| for(var i = 0; i < list.length-1; i += 2)
| |
| {
| |
| if(a[i] > a[i+1])
| |
| {
| |
| swap(a, i, i+1);
| |
| sorted = false;
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
| | |
| ==Proof of Correctness==
| |
| | |
| Claim: Let <math>a_1, ..., a_n</math> be a sequence of data ordered by <. The odd-even sort algorithm correctly sorts this data in n passes. (A pass here is defined to be a full sequence of odd-even, or even-odd comparisons. The passes occur in order pass 1: odd-even, pass 2: even-odd, etc.)
| |
| | |
| Proof:
| |
| | |
| This proof is based loosely on one by Thomas Worsch.<ref>http://liinwww.ira.uka.de/~thw/vl-hiroshima/slides-4.pdf</ref>
| |
| | |
| Since the sorting algorithm only involves comparison-swap operations and is oblivious (the order of comparison-swap operations does not depend on the data), by Knuth's 0-1 sorting principle,<ref>http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/networks/nulleinsen.htm</ref> <ref>http://www.net.t-labs.tu-berlin.de/~stefan/netalg13-9-sort.pdf</ref> it suffices to check correctness when each <math>a_i</math> is either 0 or 1. Assume that there are e 1's.
| |
| | |
| Observe that the rightmost 1 can be either in an even or odd position, so it might not be moved by the first odd-even pass. But after the first odd-even pass, the rightmost 1 will be in an even position. It follows that it will be moved to the right by all remaining passes. Since the rightmost one starts in position greater than or equal to e, it must be moved at most n-e steps. It follows that it takes at most n-e+1 passes to move the rightmost 1 to its correct position.
| |
| | |
| Now, consider the second rightmost 1. After two passes, the 1 to its right will have moved right by at least one step. It follows that, for all remaining passes, we can view the second rightmost 1 as the rightmost 1. The second rightmost 1 starts in position at least e-1 at must be moved to position at most n-1, so it must be moved at most (n-1) - (e-1) = n-e steps. After at most 2 passes, the rightmost 1 will have already moved, so the entry to the right of the second rightmost 1 will be 0. Hence, for all passes after the first two, the second rightmost 1 will move to the right. It thus takes at most n-e +2 passes to move the second rightmost 1 to its correct position.
| |
| | |
| Continuing in this manner, by induction it can be shown that the i^th rightmost 1 is moved to its correct position in at most n-e+i+1 passes. It follows that the e^th rightmost 1 is moved to its correct position in at most n-e+(e-1)+1 = n passes (consider: counting starts at value "0") . The list is thus correctly sorted in n passes. QED.
| |
| | |
| We remark that each pass takes O(n) steps, so this algorithm is O(n^2) complexity.
| |
| | |
| ==References==
| |
| | |
| {{reflist}}
| |
| | |
| {{sorting}}
| |
| | |
| {{DEFAULTSORT:Odd-even sort}}
| |
| [[Category:Sorting algorithms]]
| |
| [[Category:Comparison sorts]]
| |
| [[Category:Stable sorts]]
| |
| [[Category:Articles with example pseudocode]]
| |
| [[Category:Articles_containing_proofs]]
| |
| | |
| | |
| {{compu-sci-stub}}
| |
Alyson Meagher is the name her parents gave her but she doesn't like when individuals use her full title. I've usually loved living in Alaska. The preferred pastime for him and his kids is to perform lacross and he'll be beginning something else along with it. Invoicing is my occupation.
My webpage - accurate psychic predictions