Insertion Sort
4870 ワード
1 import java.io.*;
2 import java.util.*;
3
4 public class Solution {
5
6 public static void insertionSort(int[] ar)
7 {
8 int shifts = 0;
9 for(int i = 1;i<ar.length;i++){
10 int temp = ar[i];
11 int j = i-1;
12 for(;j>=0 && ar[j]>temp;j--){
13 ar[j+1] = ar[j];
14 shifts++;
15 }
16 ar[j+1] = temp;
17 }
18 printArray(ar);
19 }
20
21
22
23 public static void main(String[] args) {
24 Scanner in = new Scanner(System.in);
25 int s = in.nextInt();
26 int[] ar = new int[s];
27 for(int i=0;i<s;i++){
28 ar[i]=in.nextInt();
29 }
30 System.out.println(insertionSortPart2(ar));
31
32 }
33 private static void printArray(int[] ar) {
34 for(int n: ar){
35 System.out.print(n+" ");
36 }
37 System.out.println("");
38 }
39 }