人生感悟:你们是不是怀疑人生了???不怀疑人生你们打开我的文章看个屁啊!
注意事项: (输出格式啊!!!!格式!!!!)首先要注意:当排序的个数等于5的时候,它的格式跟样例的一模一样,而且最后一个Move back中间是两个空格。如果排序个数不等于5的话,Init,Final,Move back前面都是有两个空格。啊,世界多美好,没经历过毒打是不会得出这种深奥的结论的,请相信我!
参考代码:
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int number = sc.nextInt(); if (number<=100) { int arr[] = new int[number]; // Scanner sc = new Scanner(System.in); // for (int i = 0; i < arr.length; i++) { // arr[i] = sc.nextInt(); // } for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); } recursionSort(arr,0); } } //递归版的插入排序 public static void recursionSort(int arr[],int origin) { if (origin==arr.length) {//递归出口 return ; } System.out.println("Insert element["+(origin+1)+"]:"); if (arr.length==5) {//判断排序的数是否等于5 System.out.print("Init:"); }else { System.out.print(" Init:"); } for (int i = 0; i < origin+1; i++) { if (i>origin-1) { System.out.print(arr[i]); }else { System.out.print(arr[i]+" "); } } System.out.println(); int record = arr[origin]; int index = origin-1; while(index>-1 && arr[index]>record) { arr[index+1] = arr[index]; if (arr.length==5) {//判断排序的数是否等于5 if (origin==arr.length-1) { System.out.print("Move back:"); }else System.out.print("Move back:"); }else//排序的数不等于5 System.out.print(" Move back:"); for (int i = 0; i < origin+1; i++) { if (i>origin-1) { System.out.print(arr[i]); }else { System.out.print(arr[i]+" "); } } System.out.println(); arr[index] = record; index--; } if (arr.length==5) {//判断排序的数是否等于5 System.out.print("Final:"); }else System.out.print(" Final:"); for (int i = 0; i < origin+1; i++) { if (i>origin-1) { System.out.print(arr[i]); }else { System.out.print(arr[i]+" "); } } System.out.println(); recursionSort(arr,++origin);//开始递归 }
0.0分
1 人评分