蹭点分~~
/*
纯过程实现的~~ ! 不知道有没有bug!
好像有点长! ~~ 没学好! 只能写这么长了~~
*/
import java.util.*;
class A
{
public static void main(String[] args)
{
int j;//数组长度
//int len=0;
int tv;//插入的值
int position;//插入位置
Scanner sc = new Scanner(System.in);
System.out.printf("请输入数组长度!\n");
j = sc.nextInt();
int[] arr = new int[j];
System.out.printf("请输入数值!\n");
//初始化~
for(int i=0; i<arr.length; ++i)
{
arr[i] = sc.nextInt();
}
printf(arr);//输出函数
System.out.printf("请输入要插入的值:\n");
tv = sc.nextInt();
System.out.printf("\n");
System.out.printf("请输入要插入位置的下标:");
position = sc.nextInt();
//判断数组空间
if(position>arr.length)
{
System.out.printf("位置错误!\n");
System.exit(0);
}
lnsert(arr, tv, position, j);//插入函数
}
//遍历数组
static void printf(int arr[])
{
for(int j=0; j<arr.length; ++j)
{
System.out.printf(" %d", arr[j]);
}
System.out.printf("\n");
}
//插入数值
static void lnsert(int arr[], int tv, int position, int len)
{
int[] buf = new int[len+1];
int k = buf.length-1;
for(int i=0; i<buf.length-1; ++i)
{
buf[i] = arr[i];
}
for(int j=buf.length-2; j>=-1; --j)
{
if(j<position)
{
buf[k] = tv;
break;
}
buf[k] = buf[j];
--k;
}
printf(buf);
System.out.printf("插入成功!\n");
Sorting(buf, 0, buf.length-1);
printf(buf);
}
//下面两个函数是排序!
static void Sorting(int buf[], int low, int hihg)
{
int pos;
if(low<hihg)
{
//递归
pos = Fast(buf, low, hihg);
Sorting(buf, pos+1, hihg);
Sorting(buf, low, pos-1);
}
}
static int Fast(int buf[], int low, int hihg)
{
int val = buf[low];
while(low<hihg)
{
while(low<hihg && buf[hihg]>=val)
--hihg;
buf[low] = buf[hihg];
while(low<hihg && buf[low]<=val)
++low;
buf[hihg] = buf[low];
}
buf[low] = val;
return low;
}
} |