import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Arrays; import java.util.Random; import static org.junit.Assert.*; /** * Created by corning on 2017/12/19. */ public class ArraySortTest { private int[] array; private int[] sortedArray; // è®¡æ°æåºç䏿¯æè´æ°æåº private int[] positiveArray; private int[] positiveArraySorted; @Before public void setUp() throws Exception { // çæéæºæ°ç» array = randomArray(-1000, 1000, 100); // ä½¿ç¨ Arrays.sort() æåºä½ä¸ºå¯¹æ¯ sortedArray = Arrays.copyOf(array, array.length); Arrays.sort(sortedArray); positiveArray = randomArray(0, 1000, 100); positiveArraySorted = Arrays.copyOf(positiveArray, positiveArray.length); Arrays.sort(positiveArraySorted); } /** * éæºæå®èå´å N个ä¸éå¤çæ° * å¨åå§åçæ éå¤å¾ éæ°ç»ä¸éæºäº§çä¸ä¸ªæ°æ¾å ¥ç»æä¸ï¼ * å°å¾ éæ°ç»è¢«éæºå°çæ°ï¼ç¨å¾ éæ°ç»(len-1)䏿 对åºçæ°æ¿æ¢ * ç¶åä»len-2ééæºäº§çä¸ä¸ä¸ªéæºæ°ï¼å¦æ¤ç±»æ¨ * * @param max æå®èå´æå¤§å¼ * @param min æå®èå´æå°å¼ * @param n éæºæ°ä¸ªæ° * @return int[] éæºæ°ç»æé */ public int[] randomArray(int min, int max, int n) { int len = max - min + 1; if (max < min || n > len) { return null; } //åå§åç»å®èå´çå¾ éæ°ç» int[] source = new int[len]; for (int i = min; i < min + len; i++) { source[i - min] = i; } int[] result = new int[n]; Random rd = new Random(); int index = 0; for (int i = 0; i < result.length; i++) { //å¾ éæ°ç»0å°(len-2)éæºä¸ä¸ªä¸æ index = Math.abs(rd.nextInt() % len--); //å°éæºå°çæ°æ¾å ¥ç»æé result[i] = source[index]; //å°å¾ éæ°ç»ä¸è¢«éæºå°çæ°ï¼ç¨å¾ éæ°ç»(len-1)䏿 对åºçæ°æ¿æ¢ source[index] = source[len]; } return result; } @After public void tearDown() throws Exception { array = null; sortedArray = null; } @Test public void bubbleSort() throws Exception { assertArrayEquals(sortedArray, new BubbleSort().sort(array)); } @Test public void choiceSort() throws Exception { assertArrayEquals(sortedArray, new SelectionSort().sort(array)); } @Test public void insertSort() throws Exception { assertArrayEquals(sortedArray, new InsertSort().sort(array)); } @Test public void shellSort() throws Exception { assertArrayEquals(sortedArray, new ShellSort().sort(array)); } @Test public void mergeSort() throws Exception { assertArrayEquals(sortedArray, new MergeSort().sort(array)); } @Test public void mergeSort_merge() throws Exception { assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1, 2}, new int[]{})); assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1}, new int[]{2})); assertArrayEquals(new int[]{1, 2, 3}, new MergeSort().merge(new int[]{1, 3}, new int[]{2})); } @Test public void quickSort() throws Exception { assertArrayEquals(sortedArray, new QuickSort().sort(array)); } @Test public void heapSort() throws Exception { assertArrayEquals(sortedArray, new HeapSort().sort(array)); } @Test public void countingSort() throws Exception { assertArrayEquals(positiveArraySorted, new CountingSort().sort(positiveArray)); } @Test public void bucketSort() throws Exception { assertArrayEquals(sortedArray, new BucketSort().sort(array)); } @Test public void radixSort() throws Exception { assertArrayEquals(sortedArray, new RadixSort().sort(array)); } @Test public void radixSort_getNumLenght() throws Exception { assertEquals(3, new RadixSort().getNumLenght(-100)); assertEquals(1, new RadixSort().getNumLenght(1)); } }