1个回答
展开全部
//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<arr.length-1; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
Insert(arr, len, data);
System.out.println("插入元素后:");
for(i=0; i<arr.length; i++){
System.out.print(arr[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;
}
}
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<arr.length-1; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
Insert(arr, len, data);
System.out.println("插入元素后:");
for(i=0; i<arr.length; i++){
System.out.print(arr[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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询