test/java/util/Arrays/Sorting.java

Print this page

        

@@ -48,14 +48,14 @@
     // Array lengths used in a short run
     private static final int[] SHORT_RUN_LENGTHS = {
         1, 2, 3, 21, 55, 1000, 10000 };
 
     // Random initial values used in a long run (default)
-    private static final long[] LONG_RUN_RANDOMS = {666, 0xC0FFEE, 999};
+    private static final long[] LONG_RUN_RANDOMS = { 666, 0xC0FFEE, 999 };
 
     // Random initial values used in a short run
-    private static final long[] SHORT_RUN_RANDOMS = {666};
+    private static final long[] SHORT_RUN_RANDOMS = { 666 };
 
     public static void main(String[] args) {
         boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
         long start = System.currentTimeMillis();
 

@@ -80,10 +80,15 @@
 
         for (long random : randoms) {
             reset(random);
 
             for (int length : lengths) {
+                testAndCheckWithInsertionSort(length, random);
+            }
+            reset(random);
+
+            for (int length : lengths) {
                 testAndCheckWithCheckSum(length, random);
             }
             reset(random);
 
             for (int length : lengths) {

@@ -266,13 +271,11 @@
             for (TypeConverter converter : TypeConverter.values()) {
                 out.println("Test 'subarray': " + converter +
                    " length = " + length + ", m = " + m);
                 Object convertedGolden = converter.convert(golden);
                 Object convertedTest = converter.convert(test);
-                // outArray(test);
                 sortSubArray(convertedTest, fromIndex, toIndex);
-                // outArray(test);
                 checkSubArray(convertedTest, fromIndex, toIndex, m);
             }
         }
         if (newLine) {
             out.println();

@@ -309,11 +312,11 @@
     }
 
     private static void checkSorted(Pair[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i].getKey() > a[i + 1].getKey()) {
-                failed(i, "" + a[i].getKey(), "" + a[i + 1].getKey());
+                failedSorted(i, "" + a[i].getKey(), "" + a[i + 1].getKey());
             }
         }
     }
 
     private static void checkStable(Pair[] a) {

@@ -326,11 +329,11 @@
             int value3 = a[i++].getValue();
             int key4 = a[i].getKey();
             int value4 = a[i++].getValue();
 
             if (!(key1 == key2 && key2 == key3 && key3 == key4)) {
-                failed("On position " + i + " must keys are different " +
+                failed("On position " + i + " keys are different " +
                     key1 + ", " + key2 + ", " + key3 + ", " + key4);
             }
             if (!(value1 < value2 && value2 < value3 && value3 < value4)) {
                 failed("Sorting is not stable at position " + i +
                     ". Second values have been changed: " +  value1 + ", " +

@@ -383,10 +386,39 @@
 
         private int myKey;
         private int myValue;
     }
 
+
+    private static void testAndCheckWithInsertionSort(int length, long random) {
+        if (length > 1000) {
+            return;
+        }
+        ourDescription = "Check sorting with insertion sort";
+        int[] golden = new int[length];
+
+        for (int m = 1; m < 2 * length; m *= 2) {
+            for (UnsortedBuilder builder : UnsortedBuilder.values()) {
+                builder.build(golden, m);
+                int[] test = golden.clone();
+
+                for (TypeConverter converter : TypeConverter.values()) {
+                    out.println("Test 'insertion sort': " + converter + " " +
+                        builder + "random = " +  random + ", length = " +
+                        length + ", m = " + m);
+                    Object convertedGolden = converter.convert(golden);
+                    Object convertedTest1 = converter.convert(test);
+                    Object convertedTest2 = converter.convert(test);
+                    sort(convertedTest1);
+                    sortByInsertionSort(convertedTest2);
+                    compare(convertedTest1, convertedTest2);
+                }
+            }
+        }
+        out.println();
+    }
+
     private static void testAndCheckWithCheckSum(int length, long random) {
         ourDescription = "Check sorting with check sum";
         int[] golden = new int[length];
 
         for (int m = 1; m < 2 * length; m *= 2) {

@@ -458,13 +490,11 @@
                                    ", g = " + g + ", z = " + z + ", n = " + n +
                                    ", p = " + p);
                                 builder.build(golden, a, g, z, n, p);
                                 float[] test = golden.clone();
                                 scramble(test);
-                                // outArray(test);
                                 sort(test);
-                                // outArray(test);
                                 compare(test, golden, a, n, g);
                             }
                             newLine = true;
                         }
                     }

@@ -498,13 +528,11 @@
                                    ", length = " + length + ", a = " + a + ", g = " +
                                    g + ", z = " + z + ", n = " + n + ", p = " + p);
                                 builder.build(golden, a, g, z, n, p);
                                 double[] test = golden.clone();
                                 scramble(test);
-                                // outArray(test);
                                 sort(test);
-                                // outArray(test);
                                 compare(test, golden, a, n, g);
                             }
                             newLine = true;
                         }
                     }

@@ -719,16 +747,16 @@
         }
         final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f);
 
         for (int i = numNeg; i < numNeg + numNegZero; i++) {
             if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) {
-                failed("On position " + i + " must be -0.0f instead of " + a[i]);
+                failed("On position " + i + " must be -0.0 instead of " + a[i]);
             }
         }
         for (int i = 0; i < a.length - numNaN; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void writeValue(double[] a, double value, int fromIndex, int count) {

@@ -745,16 +773,16 @@
         }
         final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d);
 
         for (int i = numNeg; i < numNeg + numNegZero; i++) {
             if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) {
-                failed("On position " + i + " must be -0.0d instead of " + a[i]);
+                failed("On position " + i + " must be -0.0 instead of " + a[i]);
             }
         }
         for (int i = 0; i < a.length - numNaN; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static enum SortedBuilder {

@@ -839,12 +867,12 @@
         SAW {
             void build(int[] a, int m) {
                 int incCount = 1;
                 int decCount = a.length;
                 int i = 0;
-                int period = m;
-                m--;
+                int period = m--;
+
                 while (true) {
                     for (int k = 1; k <= period; k++) {
                         if (i >= a.length) {
                             return;
                         }

@@ -920,10 +948,29 @@
             }
             return name;
         }
     }
 
+    private static void checkWithCheckSum(Object test, Object golden) {
+        checkSorted(test);
+        checkCheckSum(test, golden);
+    }
+
+    private static void failed(String message) {
+        err.format("\n*** TEST FAILED - %s.\n\n%s.\n\n", ourDescription, message);
+        throw new RuntimeException("Test failed - see log file for details");
+    }
+
+    private static void failedSorted(int index, String value1, String value2) {
+        failed("Array is not sorted at " + index + "-th position: " +
+            value1 + " and " + value2);
+    }
+
+    private static void failedCompare(int index, String value1, String value2) {
+        failed("On position " + index + " must be " + value2 + " instead of " + value1);
+    }
+
     private static void compare(Object test, Object golden) {
         if (test instanceof int[]) {
             compare((int[]) test, (int[]) golden);
         } else if (test instanceof long[]) {
             compare((long[]) test, (long[]) golden);

@@ -943,278 +990,465 @@
             failed("Unknow type of array: " + test + " of class " +
                 test.getClass().getName());
         }
     }
 
-    private static void checkWithCheckSum(Object test, Object golden) {
-        checkSorted(test);
-        checkCheckSum(test, golden);
-    }
-
-    private static void failed(String message) {
-        err.format("\n*** TEST FAILED - %s\n\n%s\n\n", ourDescription, message);
-        throw new RuntimeException("Test failed - see log file for details");
-    }
-
-    private static void failed(int index, String value1, String value2) {
-        failed("Array is not sorted at " + index + "-th position: " +
-            value1 + " and " + value2);
-    }
-
-    private static void checkSorted(Object object) {
-        if (object instanceof int[]) {
-            checkSorted((int[]) object);
-        } else if (object instanceof long[]) {
-            checkSorted((long[]) object);
-        } else if (object instanceof short[]) {
-            checkSorted((short[]) object);
-        } else if (object instanceof byte[]) {
-            checkSorted((byte[]) object);
-        } else if (object instanceof char[]) {
-            checkSorted((char[]) object);
-        } else if (object instanceof float[]) {
-            checkSorted((float[]) object);
-        } else if (object instanceof double[]) {
-            checkSorted((double[]) object);
-        } else if (object instanceof Integer[]) {
-            checkSorted((Integer[]) object);
-        } else {
-            failed("Unknow type of array: " + object + " of class " +
-                object.getClass().getName());
-        }
-    }
-
-    private static void compare(Integer[] a, Integer[] b) {
-        for (int i = 0; i < a.length; i++) {
-            if (a[i].intValue() != b[i].intValue()) {
-                failed(i, "" + a[i], "" + b[i]);
-            }
-        }
-    }
-
     private static void compare(int[] a, int[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(long[] a, long[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(short[] a, short[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(byte[] a, byte[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(char[] a, char[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(float[] a, float[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
     private static void compare(double[] a, double[] b) {
         for (int i = 0; i < a.length; i++) {
             if (a[i] != b[i]) {
-                failed(i, "" + a[i], "" + b[i]);
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
-    private static void checkSorted(Integer[] a) {
-        for (int i = 0; i < a.length - 1; i++) {
-            if (a[i].intValue() > a[i + 1].intValue()) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+    private static void compare(Integer[] a, Integer[] b) {
+        for (int i = 0; i < a.length; i++) {
+            if (a[i].compareTo(b[i]) != 0) {
+                failedCompare(i, "" + a[i], "" + b[i]);
             }
         }
     }
 
+    private static void checkSorted(Object object) {
+        if (object instanceof int[]) {
+            checkSorted((int[]) object);
+        } else if (object instanceof long[]) {
+            checkSorted((long[]) object);
+        } else if (object instanceof short[]) {
+            checkSorted((short[]) object);
+        } else if (object instanceof byte[]) {
+            checkSorted((byte[]) object);
+        } else if (object instanceof char[]) {
+            checkSorted((char[]) object);
+        } else if (object instanceof float[]) {
+            checkSorted((float[]) object);
+        } else if (object instanceof double[]) {
+            checkSorted((double[]) object);
+        } else if (object instanceof Integer[]) {
+            checkSorted((Integer[]) object);
+        } else {
+            failed("Unknow type of array: " + object + " of class " +
+                object.getClass().getName());
+        }
+    }
+
     private static void checkSorted(int[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(long[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(short[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(byte[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(char[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(float[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
     private static void checkSorted(double[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
     }
 
+    private static void checkSorted(Integer[] a) {
+        for (int i = 0; i < a.length - 1; i++) {
+            if (a[i].intValue() > a[i + 1].intValue()) {
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
+            }
+        }
+    }
+
     private static void checkCheckSum(Object test, Object golden) {
-        if (checkSum(test) != checkSum(golden)) {
-            failed("It seems that original and sorted arrays are not identical");
+        if (checkSumXor(test) != checkSumXor(golden)) {
+            failed("Original and sorted arrays are not identical [xor]");
         }
+        if (checkSumPlus(test) != checkSumPlus(golden)) {
+            failed("Original and sorted arrays are not identical [plus]");
     }
+    }
 
-    private static int checkSum(Object object) {
+    private static int checkSumXor(Object object) {
         if (object instanceof int[]) {
-            return checkSum((int[]) object);
+            return checkSumXor((int[]) object);
         } else if (object instanceof long[]) {
-            return checkSum((long[]) object);
+            return checkSumXor((long[]) object);
         } else if (object instanceof short[]) {
-            return checkSum((short[]) object);
+            return checkSumXor((short[]) object);
         } else if (object instanceof byte[]) {
-            return checkSum((byte[]) object);
+            return checkSumXor((byte[]) object);
         } else if (object instanceof char[]) {
-            return checkSum((char[]) object);
+            return checkSumXor((char[]) object);
         } else if (object instanceof float[]) {
-            return checkSum((float[]) object);
+            return checkSumXor((float[]) object);
         } else if (object instanceof double[]) {
-            return checkSum((double[]) object);
+            return checkSumXor((double[]) object);
         } else if (object instanceof Integer[]) {
-            return checkSum((Integer[]) object);
+            return checkSumXor((Integer[]) object);
         } else {
             failed("Unknow type of array: " + object + " of class " +
                 object.getClass().getName());
             return -1;
         }
     }
 
-    private static int checkSum(Integer[] a) {
-        int checkXorSum = 0;
+    private static int checkSumXor(Integer[] a) {
+        int checkSum = 0;
 
         for (Integer e : a) {
-            checkXorSum ^= e.intValue();
+            checkSum ^= e.intValue();
         }
-        return checkXorSum;
+        return checkSum;
     }
 
-    private static int checkSum(int[] a) {
-        int checkXorSum = 0;
+    private static int checkSumXor(int[] a) {
+        int checkSum = 0;
 
         for (int e : a) {
-            checkXorSum ^= e;
+            checkSum ^= e;
         }
-        return checkXorSum;
+        return checkSum;
     }
 
-    private static int checkSum(long[] a) {
-        long checkXorSum = 0;
+    private static int checkSumXor(long[] a) {
+        long checkSum = 0;
 
         for (long e : a) {
-            checkXorSum ^= e;
+            checkSum ^= e;
         }
-        return (int) checkXorSum;
+        return (int) checkSum;
     }
 
-    private static int checkSum(short[] a) {
-        short checkXorSum = 0;
+    private static int checkSumXor(short[] a) {
+        short checkSum = 0;
 
         for (short e : a) {
-            checkXorSum ^= e;
+            checkSum ^= e;
         }
-        return (int) checkXorSum;
+        return (int) checkSum;
     }
 
-    private static int checkSum(byte[] a) {
-        byte checkXorSum = 0;
+    private static int checkSumXor(byte[] a) {
+        byte checkSum = 0;
 
         for (byte e : a) {
-            checkXorSum ^= e;
+            checkSum ^= e;
         }
-        return (int) checkXorSum;
+        return (int) checkSum;
     }
 
-    private static int checkSum(char[] a) {
-        char checkXorSum = 0;
+    private static int checkSumXor(char[] a) {
+        char checkSum = 0;
 
         for (char e : a) {
-            checkXorSum ^= e;
+            checkSum ^= e;
         }
-        return (int) checkXorSum;
+        return (int) checkSum;
     }
 
-    private static int checkSum(float[] a) {
-        int checkXorSum = 0;
+    private static int checkSumXor(float[] a) {
+        int checkSum = 0;
 
         for (float e : a) {
-            checkXorSum ^= (int) e;
+            checkSum ^= (int) e;
         }
-        return checkXorSum;
+        return checkSum;
     }
 
-    private static int checkSum(double[] a) {
-        int checkXorSum = 0;
+    private static int checkSumXor(double[] a) {
+        int checkSum = 0;
 
         for (double e : a) {
-            checkXorSum ^= (int) e;
+            checkSum ^= (int) e;
         }
-        return checkXorSum;
+        return checkSum;
     }
 
+    private static int checkSumPlus(Object object) {
+        if (object instanceof int[]) {
+            return checkSumPlus((int[]) object);
+        } else if (object instanceof long[]) {
+            return checkSumPlus((long[]) object);
+        } else if (object instanceof short[]) {
+            return checkSumPlus((short[]) object);
+        } else if (object instanceof byte[]) {
+            return checkSumPlus((byte[]) object);
+        } else if (object instanceof char[]) {
+            return checkSumPlus((char[]) object);
+        } else if (object instanceof float[]) {
+            return checkSumPlus((float[]) object);
+        } else if (object instanceof double[]) {
+            return checkSumPlus((double[]) object);
+        } else if (object instanceof Integer[]) {
+            return checkSumPlus((Integer[]) object);
+        } else {
+            failed("Unknow type of array: " + object + " of class " +
+                object.getClass().getName());
+            return -1;
+        }
+    }
+
+    private static int checkSumPlus(int[] a) {
+        int checkSum = 0;
+
+        for (int e : a) {
+            checkSum += e;
+        }
+        return checkSum;
+    }
+
+    private static int checkSumPlus(long[] a) {
+        long checkSum = 0;
+
+        for (long e : a) {
+            checkSum += e;
+        }
+        return (int) checkSum;
+    }
+
+    private static int checkSumPlus(short[] a) {
+        short checkSum = 0;
+
+        for (short e : a) {
+            checkSum += e;
+        }
+        return (int) checkSum;
+    }
+
+    private static int checkSumPlus(byte[] a) {
+        byte checkSum = 0;
+
+        for (byte e : a) {
+            checkSum += e;
+        }
+        return (int) checkSum;
+    }
+
+    private static int checkSumPlus(char[] a) {
+        char checkSum = 0;
+
+        for (char e : a) {
+            checkSum += e;
+        }
+        return (int) checkSum;
+    }
+
+    private static int checkSumPlus(float[] a) {
+        int checkSum = 0;
+
+        for (float e : a) {
+            checkSum += (int) e;
+        }
+        return checkSum;
+    }
+
+    private static int checkSumPlus(double[] a) {
+        int checkSum = 0;
+
+        for (double e : a) {
+            checkSum += (int) e;
+        }
+        return checkSum;
+    }
+
+    private static int checkSumPlus(Integer[] a) {
+        int checkSum = 0;
+
+        for (Integer e : a) {
+            checkSum += e.intValue();
+        }
+        return checkSum;
+    }
+
+    private static void sortByInsertionSort(Object object) {
+        if (object instanceof int[]) {
+            sortByInsertionSort((int[]) object);
+        } else if (object instanceof long[]) {
+            sortByInsertionSort((long[]) object);
+        } else if (object instanceof short[]) {
+            sortByInsertionSort((short[]) object);
+        } else if (object instanceof byte[]) {
+            sortByInsertionSort((byte[]) object);
+        } else if (object instanceof char[]) {
+            sortByInsertionSort((char[]) object);
+        } else if (object instanceof float[]) {
+            sortByInsertionSort((float[]) object);
+        } else if (object instanceof double[]) {
+            sortByInsertionSort((double[]) object);
+        } else if (object instanceof Integer[]) {
+            sortByInsertionSort((Integer[]) object);
+        } else {
+            failed("Unknow type of array: " + object + " of class " +
+                object.getClass().getName());
+        }
+    }
+
+    private static void sortByInsertionSort(int[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            int ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(long[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            long ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(short[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            short ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(byte[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            byte ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(char[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            char ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(float[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            float ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(double[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            double ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
+    private static void sortByInsertionSort(Integer[] a) {
+        for (int j, i = 1; i < a.length; i++) {
+            Integer ai = a[i];
+            for (j = i - 1; j >= 0 && ai < a[j]; j--) {
+                a[j + 1] = a[j];
+            }
+            a[j + 1] = ai;
+        }
+    }
+
     private static void sort(Object object) {
         if (object instanceof int[]) {
             Arrays.sort((int[]) object);
         } else if (object instanceof long[]) {
             Arrays.sort((long[]) object);

@@ -1290,11 +1524,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i].intValue() > a[i + 1].intValue()) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i].intValue() != 0xDEDA) {

@@ -1312,11 +1546,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != 0xDEDA) {

@@ -1334,11 +1568,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (byte) 0xDEDA) {

@@ -1356,11 +1590,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (long) 0xDEDA) {

@@ -1378,11 +1612,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (char) 0xDEDA) {

@@ -1400,11 +1634,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (short) 0xDEDA) {

@@ -1422,11 +1656,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (float) 0xDEDA) {

@@ -1444,11 +1678,11 @@
             }
         }
 
         for (int i = fromIndex; i < toIndex - 1; i++) {
             if (a[i] > a[i + 1]) {
-                failed(i, "" + a[i], "" + a[i + 1]);
+                failedSorted(i, "" + a[i], "" + a[i + 1]);
             }
         }
 
         for (int i = toIndex; i < a.length; i++) {
             if (a[i] != (double) 0xDEDA) {