fork download
  1. import java.util.Arrays;
  2.  
  3. class InsertionSort {
  4.  
  5. void insertionSort(int array[]) {
  6. int size = array.length;
  7.  
  8. for (int step = 1; step < size; step++) {
  9. int key = array[step];
  10. int j = step - 1;
  11. while (j >= 0 && key < array[j]) {
  12. array[j + 1] = array[j];
  13. --j;
  14. }
  15. array[j + 1] = key;
  16. }
  17. }
  18. public static void main(String args[]) {
  19. int[] data = { 9, 5, 1, 4, 3 };
  20. InsertionSort is = new InsertionSort();
  21. is.insertionSort(data);
  22. System.out.println("Sorted Array in Ascending Order: ");
  23. System.out.println(Arrays.toString(data));
  24. }
  25. }
Success #stdin #stdout 0.06s 32464KB
stdin
Standard input is empty
stdout
Sorted Array in Ascending Order: 
[1, 3, 4, 5, 9]