package untitled;
import java.util.Random;
import java.util.Scanner;
public class dk {
public static void main(String[] args) {
Random rand=new Random();
int[] r=new int [2000];
System.out.print("参与选择的有n个整数,请确定n:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.print("选择第k小整数,请确定k:");
int k=sc.nextInt();
sc.close();
for(int i=0;i<n;i++)
{
r[i]=rand.nextInt(100);
System.out.print(r[i]+" ");
}
System.out.println();
yhy(r, 1, n, k);
System.out.print("以上"+n+"个整数中第"+k+"小整数为"+r[k]);
}
public static int yhy(int[] r,int m1,int m2,int m) {
int i,j;
if(m1<m2){
i=m1;
j=m2;
r[0]=r[i];
while(i!=j){
while(r[j]>=r[0]&&j>i)
j=j-1;
if(i<j) {r[i]=r[j];i=i+1;}
while(r[i]<=r[0]&&j>i)
i=i+1;
if(i<j) {r[j]=r[i];j=j-1;}
}
r[i]=r[0];
if(i==m)
return r[m];
else if(i>m)
return yhy(r, m1, i-1, m);
else
return yhy(r, i+1, m2, m);
}
else return 0;
}
}