Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "+ Even Odd Sort & Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine."— Presentation transcript:

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

2 + 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,

3 + 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

4 + Example

5 + Best Case: O(n) Worst Case: O(n 2 )

6 +

7 + 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

8 + Visual Representation

9 + 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.

10 + Psuedo Code OddEvenMergeSort(list) Input: sequence of a o ……a n-1 whose 2 halves of a o ……a n/2-1 and of a n/2 ……a n-1 are sorted Method: if n>2 then 1. apply odd-even mergesort(n/2) to a o, a 2, …, a n-2 and a 1, a 3, …, a n-1 2. perform odd-even merge(n) 3. for i = {1, 3, …, n-3} if a i > a i+1 swap [a i, a i+1 ] else if a 0 > a 1 swap [a 0, a 1 ] Output: the sorted sequence

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

12 + Best/Worst Case Performance: O(log 2 (n)) Significantly more efficient then merge sort of quick sort: Average/Worst Case Performance O(nlog 2 (n)) Best Case Performance O(n)/O(nlog 2 (n)) (With Parallel Processing)

13 + Odd Even Merge Sort Video https://www.youtube.com/watch?v=DNbd1EFQ_1o


Download ppt "+ Even Odd Sort & Even-Odd Merge Sort Wolfie Herwald Pengfei Wang Rachel Celestine."

Similar presentations


Ads by Google