fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static <C extends Comparable<C>> void insertionSort(C[] a) {
  11. for (int i = 1; i < a.length; i++) {
  12. C ithElement = a[i];
  13. int j = i;
  14. for (j = i; j > 0 && ithElement.compareTo(a[j - 1]) < 0; --j) {
  15. a[j] = a[j - 1];
  16. }
  17. a[j] = ithElement;
  18. }
  19. }
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. Integer[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  24. Collections.shuffle(Arrays.asList(a));
  25. System.out.println(Arrays.toString(a));
  26. insertionSort(a);
  27. System.out.println(Arrays.toString(a));
  28. }
  29. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
[7, 3, 10, 9, 4, 1, 5, 8, 6, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]