#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int datatype;
typedef struct
{
datatype data[MAXSIZE];
int last;
}SeqList;
SeqList *CreatSeqList()
{
SeqList *L;
int temp,i=0;
L=(SeqList *)malloc(sizeof(SeqList));
L->last=-1;
printf("please input the numbers:(End with any character)\n");
while(scanf("%d",&temp))
L->data[i++]=temp;
--i;
L->last=i;
return L;
}
datatype part(SeqList *L,int a,int b,int k)
{
int i,j;
datatype m,t;
datatype* data = L->data;
m=data[a+k-1], data[a+k-1] = data[b],data[b] = m;
for (i=a-1, j = a; j != b; ++j)
if (data[j] < m)
t=data[++i], data[i]=data[j], data[j]=t;
t=data[++i], data[i]=data[j], data[j]=t;
if (k==i-a+1) return m;
if (k>i-a+1) return part(L, i+1, b, k-i+a-1);
else return part(L, a, i-1, k);
}
int main()
{
SeqList *L;
datatype x;
int k,n;
L=CreatSeqList();
n=L->last;
printf("The total number is %d\n",n+1);
printf("Now you can enter number k(1 <=k <=%d):\n",n+1);
fflush(stdin);
scanf("%d",&k);
x=part(L,0,L->last,k);
printf("The %d smallest is %d",k,x);
// getch();
return 0;
}
please input the numbers:(End with any character)
1 3 4 5 7 9
b
The total number is 6
Now you can enter number k(1 <=k <=6):
3
The 3 smallest is 4Press any key to continue