chillurbrain

Task9_6_3

Dec 18th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Task9_6_3 {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         int n = sc.nextInt();
  7.         int[] numArr = new int[n];
  8.         for (int i = 0; i < n; i++) {
  9.             numArr[i] = sc.nextInt();
  10.         }
  11.         insertionSort(numArr, n);
  12.     }
  13.  
  14.     public static void insertionSort(int[] a, int n) {
  15.         int in, out;
  16.         for (out = 1; out < n; out++){
  17.             int temp = a[out];
  18.             in = out;
  19.             while (in > 0 && a[in-1] >= temp) {
  20.                 a[in] = a[in - 1];
  21.                 --in;
  22.             }
  23.             a[in] = temp;
  24.         }
  25.         for (int i = 0; i < n; i++) {
  26.             System.out.print(a[i] + " ");
  27.         }
  28.     }
  29. }
Add Comment
Please, Sign In to add comment