fork download
  1. // binary insertion sort
  2.  
  3. import java.util.Arrays;
  4. class GFG
  5. {
  6.  
  7. public static void main(String[] args)
  8. {
  9. final int[] arr = { 37, 23, 0, 17, 12, 72,
  10. 31, 46, 100, 88, 54 };
  11.  
  12. new GFG().sort(arr);
  13.  
  14. for (int i = 0; i < arr.length; i++)
  15. System.out.print(arr[i] + " ");
  16. }
  17.  
  18. // Driver Code
  19. public void sort(int array[])
  20. {
  21. for (int i = 1; i < array.length; i++)
  22. {
  23. int x = array[i];
  24.  
  25. // Find location to insert
  26. // using binary search
  27. int j = Math.abs(
  28. Arrays.binarySearch(array, 0,
  29. i, x) + 1);
  30.  
  31. // Shifting array to one
  32. // location right
  33. System.arraycopy(array, j,
  34. array, j + 1, i - j);
  35.  
  36. // Placing element at its
  37. // correct location
  38. array[j] = x;
  39. }
  40. }
  41. }
  42.  
  43.  
Success #stdin #stdout 0.11s 55500KB
stdin
1 2 3 6 77 889 09 
stdout
0 12 17 23 31 37 46 54 72 88 100