|  | 
|  | 1 | +/*  | 
|  | 2 | + * Sorting algorithms demo (Java) | 
|  | 3 | + *  | 
|  | 4 | + * Copyright (c) Project Nayuki | 
|  | 5 | + * https://www.nayuki.io/page/sorting-algorithms-demo-java | 
|  | 6 | + *  | 
|  | 7 | + * (MIT License) | 
|  | 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of | 
|  | 9 | + * this software and associated documentation files (the "Software"), to deal in | 
|  | 10 | + * the Software without restriction, including without limitation the rights to | 
|  | 11 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | 
|  | 12 | + * the Software, and to permit persons to whom the Software is furnished to do so, | 
|  | 13 | + * subject to the following conditions: | 
|  | 14 | + * - The above copyright notice and this permission notice shall be included in | 
|  | 15 | + * all copies or substantial portions of the Software. | 
|  | 16 | + * - The Software is provided "as is", without warranty of any kind, express or | 
|  | 17 | + * implied, including but not limited to the warranties of merchantability, | 
|  | 18 | + * fitness for a particular purpose and noninfringement. In no event shall the | 
|  | 19 | + * authors or copyright holders be liable for any claim, damages or other | 
|  | 20 | + * liability, whether in an action of contract, tort or otherwise, arising from, | 
|  | 21 | + * out of or in connection with the Software or the use or other dealings in the | 
|  | 22 | + * Software. | 
|  | 23 | + */ | 
|  | 24 | + | 
|  | 25 | +package io.nayuki.sortalgodemo.algo; | 
|  | 26 | + | 
|  | 27 | +import io.nayuki.sortalgodemo.core.SortAlgorithm; | 
|  | 28 | +import io.nayuki.sortalgodemo.core.SortArray; | 
|  | 29 | + | 
|  | 30 | + | 
|  | 31 | +/** | 
|  | 32 | + * Sorts by scanning forward and swapping inverted adjacent elements. | 
|  | 33 | + * <ul> | 
|  | 34 | + * <li>Time complexity, best case: Θ(<var>n</var>).</li> | 
|  | 35 | + * <li>Time complexity, average case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 36 | + * <li>Time complexity, worst case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 37 | + * <li>Number of comparisons, best case: Θ(<var>n</var>).</li> | 
|  | 38 | + * <li>Number of comparisons, average case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 39 | + * <li>Number of comparisons, worst case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 40 | + * <li>Number of swaps, best case: Θ(1).</li> | 
|  | 41 | + * <li>Number of swaps, average case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 42 | + * <li>Number of swaps, worst case: Θ(<var>n</var><sup>2</sup>).</li> | 
|  | 43 | + * <li>Auxiliary space complexity, all cases: Θ(1).</li> | 
|  | 44 | + * </ul> | 
|  | 45 | + */ | 
|  | 46 | +public final class OddEvenSort implements SortAlgorithm { | 
|  | 47 | + | 
|  | 48 | +	// Singleton | 
|  | 49 | +	public static final SortAlgorithm INSTANCE = new OddEvenSort(); | 
|  | 50 | + | 
|  | 51 | + | 
|  | 52 | +	private OddEvenSort() {} | 
|  | 53 | + | 
|  | 54 | + | 
|  | 55 | +	@Override public String getName() { | 
|  | 56 | +		return "Odd-even sort"; | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | + | 
|  | 60 | +	/*---- Algorithm ----*/ | 
|  | 61 | + | 
|  | 62 | +	@Override public void sort(SortArray array) { | 
|  | 63 | +		while (true) { | 
|  | 64 | +			boolean changed = false; | 
|  | 65 | +			for (int i = 0; array.length() - i >= 2; i += 2) | 
|  | 66 | +				changed |= array.compareAndSwap(i, i + 1); | 
|  | 67 | +			for (int i = 1; array.length() - i >= 2; i += 2) | 
|  | 68 | +				changed |= array.compareAndSwap(i, i + 1); | 
|  | 69 | +			if (!changed) | 
|  | 70 | +				break; | 
|  | 71 | +		} | 
|  | 72 | +	} | 
|  | 73 | + | 
|  | 74 | +} | 
0 commit comments