//Java代码
import java.util.Scanner;
public class Conversion {
public static void main(String args[]){
int[] arr = new int[5]; //按非递减有序排列,如10,20,20,30
int i;
int len = 4;
int data = 15;
arr[0] = 10;
arr[1] = 20;
arr[2] = 20;
arr[3] = 30;
System.out.println("插入元素前:");
for(i=0; i
}
System.out.println();
Insert(arr, len, data);
System.out.println("插入元素后:");
for(i=0; i
}
System.out.println();
}
//将数据data插入到非递减有序的数组中后,使数组依旧保持非递减有序
public static void Insert(int[] arr, int len, int data){
int i;
if(data >= arr[len-1]){
arr[len] = data;
return;
}
for(i=len-1; i>=0 && data < arr[i]; i--){
arr[i+1] = arr[i];
System.out.println(i + " : " + arr[i]);
}
arr[i+1] = data;
}
}