fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void insertionSort(int[] arr, int n) {
  5. for (int i = 1; i < n; i++) {
  6. int key = arr[i];
  7. int j = i - 1;
  8. while (j >= 0 && arr[j] > key) {
  9. arr[j + 1] = arr[j];
  10. j--;
  11. }
  12. arr[j + 1] = key;
  13. }
  14. }
  15.  
  16. public static void main(String[] args) {
  17. Scanner scanner = new Scanner(System.in);
  18.  
  19. int n = scanner.nextInt();
  20. int[] arr = new int[n];
  21.  
  22. for (int i = 0; i < n; i++) {
  23. arr[i] = scanner.nextInt();
  24. }
  25.  
  26. insertionSort(arr, n);
  27.  
  28. for (int i = 0; i < n; i++) {
  29. System.out.print(arr[i] + " ");
  30. }
  31. System.out.println();
  32. scanner.close();
  33. }
  34. }
Success #stdin #stdout 0.17s 59008KB
stdin
5
2 10 5 7 3
stdout
2 3 5 7 10