@Test public void testSortEmptyArray() throws Exception { testArray = new int[] {}; expectedArray = new int[] {}; bubbleSort.sort(testArray); Assert.assertArrayEquals(expectedArray, testArray); }
@Test public void testSortWithNeg() throws Exception { testArray = new int[] {-3, 2, 1}; expectedArray = new int[] {-3, 1, 2}; bubbleSort.sort(testArray); Assert.assertArrayEquals(expectedArray, testArray); }
public static void main(String args[]) throws Exception { int[] array = {2, 4, 5, 62, 23, 34, 1, 23, 53, 5}; printArray(array); BubbleSort bs = new BubbleSort(); bs.sort(array); }
public static void main(String[] args) { int[] mas = {123, 22, 10, -56, 232, 1223, 45}; // create array // and link with mas reference BubbleSort.sort(mas); // pass reference of array System.out.println("Sorted mas"); for (int i = 0; i < mas.length; i++) { System.out.print(mas[i] + " "); } }
public static void main(String[] args) { // Array length int length = 20000; // Random number range int range = 200; // Print results boolean print = false; // For measuring performance long startTime, endTime; int[] unsortedArray = new int[length]; // Array of randomly generated numbers Random generator = new Random(); for (int i = 0; i < unsortedArray.length; i++) { unsortedArray[i] = generator.nextInt(range); } // BubbleSort run startTime = System.nanoTime(); int[] bubbleSortedArray = BubbleSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("BubbleSort " + (endTime - startTime)); // SelectionSort run startTime = System.nanoTime(); int[] selectionSortedArray = SelectionSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("SelectionSort " + (endTime - startTime)); // HeapSort run startTime = System.nanoTime(); int[] heapSortedArray = HeapSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("HeapSort " + (endTime - startTime)); // Print the arrays if (print) { Helper.printArray(unsortedArray); Helper.printArray(bubbleSortedArray); Helper.printArray(heapSortedArray); Helper.printArray(selectionSortedArray); } }
@Test public void testPerformance() { // Make a random target list. int s = 50000; int[] target = new int[s]; Random rand = new Random(System.currentTimeMillis()); for (int i = 0; i < target.length; i++) target[i] = rand.nextInt(s / 2); // Sort the resulting list with each sorting algorithm. MergeSort ms = new MergeSort(target); QuickSort qs = new QuickSort(target); HeapSort hs = new HeapSort(target); BubbleSort bs = new BubbleSort(target); InsertionSort is = new InsertionSort(target); long start; start = System.currentTimeMillis(); int[] mr = ms.sort(MergeSort.ASC_DESC.DESCENDING); long mt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] qr = qs.sort(QuickSort.ASC_DESC.DESCENDING); long qt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] hr = hs.sort(HeapSort.ASC_DESC.DESCENDING); long ht = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] br = bs.sort(BubbleSort.ASC_DESC.DESCENDING); long bt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] ir = is.sort(InsertionSort.ASC_DESC.DESCENDING); long it = System.currentTimeMillis() - start; // Compare the results. /* for (int i = 0; i < target.length; i++) { assertEquals(mr[i], qr[i]); assertEquals(qr[i], hr[i]); assertEquals(hr[i], br[i]); assertEquals(br[i], ir[i]); }*/ assertArrayEquals(mr, qr); assertArrayEquals(qr, hr); assertArrayEquals(hr, br); assertArrayEquals(br, ir); System.out.println( "SortTest.testPerformance(): MS " + mt + ", QS " + qt + ", HS " + ht + ", BS " + bt + ", IS " + it); // Analyze the performance result. assertTrue(mt < bt && mt < it); assertTrue(qt < bt && qt < it); assertTrue(ht < bt && ht < it); assertTrue( it < bt); // Check whether your system does the same with TA's, since insertion sort is // faster in TA's machine. // Analyze with more tight conditions. double factor = 0.25; assertTrue(mt < factor * it); assertTrue(qt < factor * it); assertTrue(ht < factor * it); // FYI, in TA's machine, it is like 'MS 41, QS 216, HS 19, BS 6146, IS 1942'. }
public void outputColors() { // erst sortieren BubbleSort.sort(vec); System.out.println("pause"); }
public static void test() { // Using Generics. BubbleSort<Integer> bubbleSort = new BubbleSort<Integer>(); Integer[] intArray = { 1, 170, 45, 78, 90, 802, 24, 2, 66, 55, 1, 48, 756, 159198498, 156, 423, 1790, 15684, 489, 38540, 189, 489 }; Character[] charArray = {'M', 'H', 'O', 'G', 'P', 'F', 'Z', 'D', 'A', 'K'}; System.out.println("Example using generic types."); System.out.println("Integer array input:"); for (Integer i : intArray) { System.out.print(i + " "); } System.out.println("\nInteger array output:"); for (Integer i : bubbleSort.sort(intArray)) { System.out.print(i + " "); } BubbleSort<Character> bubbleSort2 = new BubbleSort<Character>(); System.out.println("\nCharacter array input:"); for (Character c : charArray) { System.out.print(c + " "); } System.out.println("\nCharacter array output:"); for (Character c : bubbleSort2.sort(charArray)) { System.out.print(c + " "); } System.out.println("\n"); // Using primitive type sorting System.out.println("Example using primitive types."); BubbleSort bubbleSort3 = new BubbleSort(); int[] intArray2 = { 1, 170, 45, 78, 90, 802, 24, 2, 66, 55, 1, 48, 756, 159198498, 156, 423, 1790, 15684, 489, 38540, 189, 489 }; char[] charArray2 = {'M', 'H', 'O', 'G', 'P', 'F', 'Z', 'D', 'A', 'K'}; System.out.println("int array input:"); for (Integer i : intArray2) { System.out.print(i + " "); } System.out.println("\nint array output:"); for (int i : bubbleSort3.sort(intArray2)) { System.out.print(i + " "); } System.out.println("\nchar array input:"); for (char c : charArray2) { System.out.print(c + " "); } System.out.println("\nchar array output:"); for (char c : bubbleSort3.sort(charArray2)) { System.out.print(c + " "); } System.out.println("\n"); }
@Test public void testBubbleSort() { List<Integer> list = getRandomUnsortedList(); testSort(list, BubbleSort.sort(list)); }