问题
思路:遍历,每走一趟找出最小数与i交换,并将其存入新数组,
代码
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* getLeastNumbers_Solution(int* input,int lengthofinput, int k) {
int i,j,n,temp;
int *A = (int*)malloc(sizeof(int*)*k);
for(i=0;i<k;i++){
n = i;
for(j=i+1;j<lengthofinput;j++){
if(input[j] < input[n]) n = j;
}
if(n != j){
temp = input[n];
input[n] = input[i];
input[i] = temp;
}
A[i] = input[i];
}
return A;
}