13
+ Even Odd Sort & Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+ Even Odd Sort Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

Embed Size (px)

DESCRIPTION

+ How it Works 1. Comparing all odd or all even indexed pairs of adjacent elements in the list If the first value is larger than the second value then we swap them (similar to bubble sort) 2. The next step repeats this for all even or all odd indexed pairs and continues to alternate until the list is sorted

Citation preview

Page 1: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+

Even Odd Sort &Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

Page 2: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+

Simple algorithm developed originally for use on parallel processors with local interconnections. The algorithm was originally presented, and shown to be

efficient on parallel processors, by Habermann in 1972.

On a parallel processors, with one value per processor and only left-right neighbor connections, the processors all concurrently do a compare-exchange operation with their neighbors, alternating between odd-even and even-odd pairings,

Page 3: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+How it Works

1. Comparing all odd or all even indexed pairs of adjacent elements in the list

If the first value is larger than the second value then we swap them (similar to bubble sort)

2. The next step repeats this for all even or all odd indexed pairs and continues to alternate until the list is sorted

Page 4: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Example

Page 5: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+

Best Case: O(n)

Worst Case: O(n2)

Page 6: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+

Page 7: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Odd Even Merge Sort Odd-Even MergeSort was developed by Ken Batcher A related but more efficient algorithm, that uses

compare-exchange and perfect shuffle operations It is based on a merge algorithm that merges two

sorted halves of a sequence to a completely sorted sequence. In contrast to mergesort, this algorithm is not data-

dependent meaning that same comparisons are performed regardless of the actual data.

Because of this odd-even mergesort can be implemented as a sorting network

Page 8: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Visual Representation

Page 9: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Theorem

For any list of length n, where n is a multiple of 4, first sorting separately the first and second halves, then sorting separately the odd-indexed keys and the even-indexed keys, and finally comparing-and-switching the keys indexed 2` and 2` + 1 for ` = 1, 2, · · · ,(n/2) − 1 results in a sorted list.

Proof. After the two halves of the list have been sorted separately, it is obvious that for all i between 1 and n except for 1 and n 2 + 1, the (i − 1)-st element of the list is less than the i-th. Call the (i − 1)-st key the predecessor of the i-th. Note that 1 and n/2 + 1 are both odd. Every even-indexed key has as its predecessor a number smaller than itself (since any even-indexed key and its predecessor are in the same half), so the `-th smallest even-indexed key must be larger than at least ` odd-indexed keys (look at its predecessor as well as the predecessors of the ` − 1 even-indexed keys that are smaller than it). Similarly, every odd-indexed key (with the possible exception of two keys, namely the 1st and (n 2 + 1)-st) has as its predecessor a number smaller than itself, so the `+ 1-st smallest odd-indexed key must be larger than at least `−1 even-indexed keys. If we denote by ki the i-th indexed key after sorting the first and second halves and the even and odd halves, we have just argued that k2`−1 ≤ k2` and k2`−2 ≤ k2`+1, for any appropriate `. Since we have sorted the even indexed keys and the odd indexed keys, we also know that k2`−2 ≤ k2` and that k2`−1 ≤ k2`+1. Thus, if we group the elements in pairs (k2` , k2`+1) for each appropriate `, we see that both elements of a pair are greater or equal to both elements of the previous pair. To finish the sort after sorting the odds and evens, it is therefore only necessary to compare the ` + 1-st smallest odd-indexed key (k2`+1) to the `-th smallest even-indexed key (k2`) for each appropriate ` to determine its rank completely; this is precisely what the final step does.

Page 10: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Psuedo CodeOddEvenMergeSort(list)Input: sequence of ao……an-1 whose 2 halves of ao……an/2-1 and of an/2……an-1 are sorted

Method: if n>2 then 1. apply odd-even mergesort(n/2) to ao, a2,…, an-2 and a1, a3,…, an-1

2. perform odd-even merge(n)3. for i = {1, 3, …, n-3} if ai > ai+1 swap [ai, ai+1]else if a0 > a1 swap [a0, a1]

Output: the sorted sequence

Page 11: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Example

Original array: 3,30,1,6,2,10,20,9,15,8,21,7

Page 12: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+

Best/Worst Case Performance: O(log2(n))

Significantly more efficient then merge sort of quick sort: Average/Worst Case Performance O(nlog2(n))

Best Case Performance O(n)/O(nlog2(n))

(With Parallel Processing)

Page 13: + Even Odd Sort  Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine

+Odd Even Merge Sort Video

https://www.youtube.com/watch?v=DNbd1EFQ_1o