|
| 1 | +package com.jwetherell.algorithms.sorts.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import com.jwetherell.algorithms.sorts.AmericanFlagSort; |
| 10 | +import com.jwetherell.algorithms.sorts.BubbleSort; |
| 11 | +import com.jwetherell.algorithms.sorts.CountingSort; |
| 12 | +import com.jwetherell.algorithms.sorts.HeapSort; |
| 13 | +import com.jwetherell.algorithms.sorts.InsertionSort; |
| 14 | +import com.jwetherell.algorithms.sorts.MergeSort; |
| 15 | +import com.jwetherell.algorithms.sorts.QuickSort; |
| 16 | +import com.jwetherell.algorithms.sorts.RadixSort; |
| 17 | +import com.jwetherell.algorithms.sorts.ShellSort; |
| 18 | + |
| 19 | +public class Sorts { |
| 20 | + |
| 21 | + private static final Random RANDOM = new Random(); |
| 22 | + private static final int SIZE = 10000; |
| 23 | + |
| 24 | + private static Integer[] unsorted = null; |
| 25 | + private static Integer[] sorted = null; |
| 26 | + private static Integer[] reverse = null; |
| 27 | + |
| 28 | + static { |
| 29 | + unsorted = new Integer[SIZE]; |
| 30 | + int i = 0; |
| 31 | + while (i < unsorted.length) { |
| 32 | + int j = RANDOM.nextInt(unsorted.length * 10); |
| 33 | + unsorted[i++] = j; |
| 34 | + } |
| 35 | + |
| 36 | + sorted = new Integer[SIZE]; |
| 37 | + for (i = 0; i < sorted.length; i++) |
| 38 | + sorted[i] = i; |
| 39 | + |
| 40 | + reverse = new Integer[SIZE]; |
| 41 | + for (i = (reverse.length - 1); i >= 0; i--) |
| 42 | + reverse[i] = (SIZE - 1) - i; |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testInsertionSorts() { |
| 47 | + // Insertion sort |
| 48 | + Integer[] result = InsertionSort.sort(unsorted.clone()); |
| 49 | + assertTrue("Inerstion sort unsorted error. result="+print(result), check(result)); |
| 50 | + result = InsertionSort.sort(sorted.clone()); |
| 51 | + assertTrue("Inerstion sort sorted error. result="+print(result), check(result)); |
| 52 | + result = InsertionSort.sort(reverse.clone()); |
| 53 | + assertTrue("Inerstion sort reverse error. result="+print(result), check(result)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testBubbleSorts() { |
| 58 | + // Bubble sort |
| 59 | + Integer[] result = BubbleSort.sort(unsorted.clone()); |
| 60 | + assertTrue("Bubble sort unsorted error. result="+print(result), check(result)); |
| 61 | + result = BubbleSort.sort(sorted.clone()); |
| 62 | + assertTrue("Bubble sort sorted error. result="+print(result), check(result)); |
| 63 | + result = BubbleSort.sort(reverse.clone()); |
| 64 | + assertTrue("Bubble sort reverse error. result="+print(result), check(result)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testShellsSorts() { |
| 69 | + int[] shells = new int[] { 10, 5, 3, 1 }; |
| 70 | + // Shell's sort |
| 71 | + Integer[] result = ShellSort.sort(shells, unsorted.clone()); |
| 72 | + assertTrue("Shell's sort unsorted error. result="+print(result), check(result)); |
| 73 | + result = ShellSort.sort(shells, sorted.clone()); |
| 74 | + assertTrue("Shell's sort sorted error. result="+print(result), check(result)); |
| 75 | + result = ShellSort.sort(shells, reverse.clone()); |
| 76 | + assertTrue("Shell's sort reverse error. result="+print(result), check(result)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testMergeSorts() { |
| 81 | + // Merge sort |
| 82 | + Integer[] result = MergeSort.sort(unsorted.clone()); |
| 83 | + assertTrue("Merge sort unsorted error. result="+print(result), check(result)); |
| 84 | + result = MergeSort.sort(sorted.clone()); |
| 85 | + assertTrue("Merge sort sorted error. result="+print(result), check(result)); |
| 86 | + result = MergeSort.sort(reverse.clone()); |
| 87 | + assertTrue("merge sort reverse error. result="+print(result), check(result)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testQuickSorts() { |
| 92 | + // Quicksort |
| 93 | + Integer[] result = QuickSort.sort(QuickSort.PIVOT_TYPE.FIRST, unsorted.clone()); |
| 94 | + assertTrue("Quick sort pivot firt unsorted error. result="+print(result), check(result)); |
| 95 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.FIRST, sorted.clone()); |
| 96 | + assertTrue("Quick sort pivot firt sorted error. result="+print(result), check(result)); |
| 97 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.FIRST, reverse.clone()); |
| 98 | + assertTrue("Quick sort pivot firt reverse error. result="+print(result), check(result)); |
| 99 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.MIDDLE, unsorted.clone()); |
| 100 | + assertTrue("Quick sort pivot middle unsorted error. result="+print(result), check(result)); |
| 101 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.MIDDLE, sorted.clone()); |
| 102 | + assertTrue("Quick sort pivot middle sorted error. result="+print(result), check(result)); |
| 103 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.MIDDLE, reverse.clone()); |
| 104 | + assertTrue("Quick sort pivot middle reverse error. result="+print(result), check(result)); |
| 105 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.RANDOM, unsorted.clone()); |
| 106 | + assertTrue("Quick sort pivot random unsorted error. result="+print(result), check(result)); |
| 107 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.RANDOM, sorted.clone()); |
| 108 | + assertTrue("Quick sort pivot random sorted error. result="+print(result), check(result)); |
| 109 | + result = QuickSort.sort(QuickSort.PIVOT_TYPE.RANDOM, reverse.clone()); |
| 110 | + assertTrue("Quick sort pivot random reverse error. result="+print(result), check(result)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testHeapSorts() { |
| 115 | + // Heapsort |
| 116 | + Integer[] result = HeapSort.sort(unsorted.clone()); |
| 117 | + assertTrue("Heap sort unsorted error. result="+print(result), check(result)); |
| 118 | + result = HeapSort.sort(sorted.clone()); |
| 119 | + assertTrue("Heap sort sorted error. result="+print(result), check(result)); |
| 120 | + result = HeapSort.sort(reverse.clone()); |
| 121 | + assertTrue("Heap sort reverse error. result="+print(result), check(result)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testCountingSorts() { |
| 126 | + // Counting sort |
| 127 | + Integer[] result = CountingSort.sort(unsorted.clone()); |
| 128 | + assertTrue("Counting sort unsorted error. result="+print(result), check(result)); |
| 129 | + result = CountingSort.sort(sorted.clone()); |
| 130 | + assertTrue("Counting sort sorted error. result="+print(result), check(result)); |
| 131 | + result = CountingSort.sort(reverse.clone()); |
| 132 | + assertTrue("Counting sort reverse error. result="+print(result), check(result)); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testRadixSorts() { |
| 137 | + // Radix sort |
| 138 | + Integer[] result = RadixSort.sort(unsorted.clone()); |
| 139 | + assertTrue("Radix sort unsorted error. result="+print(result), check(result)); |
| 140 | + result = RadixSort.sort(sorted.clone()); |
| 141 | + assertTrue("Radix sort sorted error. result="+print(result), check(result)); |
| 142 | + result = RadixSort.sort(reverse.clone()); |
| 143 | + assertTrue("Radix sort reverse error. result="+print(result), check(result)); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testAmericanFlagSorts() { |
| 148 | + // American Flag sort |
| 149 | + Integer[] result = AmericanFlagSort.sort(unsorted.clone()); |
| 150 | + assertTrue("American flag sort unsorted error. result="+print(result), check(result)); |
| 151 | + result = AmericanFlagSort.sort(sorted.clone()); |
| 152 | + assertTrue("American flag sort sorted error. result="+print(result), check(result)); |
| 153 | + result = AmericanFlagSort.sort(reverse.clone()); |
| 154 | + assertTrue("American flag sort reverse error. result="+print(result), check(result)); |
| 155 | + } |
| 156 | + |
| 157 | + private static final boolean check(Integer[] array) { |
| 158 | + for (int i = 1; i<array.length; i++) { |
| 159 | + if (array[i-1]>array[i]) |
| 160 | + return false; |
| 161 | + } |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + private static final String print(Integer[] array) { |
| 166 | + return print(array, 0, array.length); |
| 167 | + } |
| 168 | + |
| 169 | + private static final String print(Integer[] array, int start, int length) { |
| 170 | + final Integer[] clone = array.clone(); |
| 171 | + StringBuilder builder = new StringBuilder(); |
| 172 | + for (int i = 0; i<length; i++) { |
| 173 | + int e = clone[start+i]; |
| 174 | + builder.append(e+" "); |
| 175 | + } |
| 176 | + return builder.toString(); |
| 177 | + } |
| 178 | +} |
0 commit comments