题目如下:
编写一个程序解决选择问题,令 k = N/2
设一组有N个数而要确定其中的第k个最大者 ,我们称之为选择问题
代码如下:
# include <stdio.h>
# include <stdlib.h>
/*
编写一个程序解决选择问题,令 k = N/2,计算出不同值的运行时间
设一组有N个数而要确定其中的第k个最大者 ,我们称之为选择问题
*/
int select(int *a,int a_len,int k);//传入数组名(指针)、数组大小、k的值(题中要求)
int main()
{
int N;
printf("please give the size of your array!\n");
scanf("%d",&N);
int *a;
a = (int*)malloc(N * sizeof(int));
int cnt;
for(cnt = 0;cnt < N;cnt++)
{
printf("give me every number in your array in order!\n");
scanf("%d",&a[cnt]);
}
int k;
k = N/2;
printf("%d",select(a,N,k));
free(a);
return 0;
}
int select(int *a,int a_len,int k)
{
int i,j;
for(i=0 ;i<a_len ;i++)
{
for(j=0 ; j<(a_len -i-1) ; j++)
{
if(a[j]<a[j+1])
{
int t;
t = a[j+1];
a[j+1] = a[j];
a[j] = t;
}
}
}
return(a[k]);
}