参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); } binaryInsert(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); } } public static void binaryInsert(int[] arr) { for (int val = arr.length / 2; val > 0; val /= 2) { for (int i = val; i < arr.length; i++) { for (int j = i; j > 0; j -= val) { if (arr[j - 1] > arr[j]) { int temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } } } }
0.0分
0 人评分