通常的做法是:
排序有点高级。有一些简单(易于理解和实现)的算法可以很好地处理小型数据集。一种这样的算法是“冒泡排序”。你从一端开始,比较两个数字。较大的“冒泡”起来,再次比较,继续前进。一圈后,您最大的数字位于顶部。现在重复,从底部开始,但要尽快停止。如果您只需要第 90 个百分位数(而不是完全排序的数组),则只需执行几次(N 次的 1/10) - 因为当您按顺序排列 10% 的最大数字时,其中最小的是你的答案。
根据问题的出色措辞,在我看来,您可以应对自己编写此代码的挑战;如果你不是,请发表评论!
编辑这里是代码:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* fp;
char* chBuf=NULL; // where line will be stored
int* myArray;
int ii, jj;
int lineCount;
int numCount;
size_t byteCount; // used for reading in the line
if((fp = fopen("numbers.txt", "r")) == NULL) {
printf("Unable to open file\n");
return -1;
}
// got here because file is openened.
// Let's find out how many lines there are
lineCount = 0;
while(getline(&chBuf, &byteCount, fp)>0) lineCount++;
printf("There are %d lines in the file\n", lineCount);
// now "rewind" to the beginning, and read one line at a time:
fseek(fp, 0, SEEK_SET);
// create space for the numbers:
myArray = malloc(lineCount * sizeof(int));
numCount = 0;
// read numbers in - this time, convert them to integers:
while(getline(&chBuf, &byteCount, fp) > 0) {
myArray[numCount] = atoi(chBuf);
// take this line out - just there to show it is working:
printf("converted number %d: it is %d\n", numCount, myArray[numCount]);
numCount++;
}
fclose(fp);
// now we have to sort. Since data was sorted low to high,
// I will sort high to low just to show it works:
for(ii = 0; ii < numCount - 1; ii++) {
for(jj = ii + 1; jj < numCount; jj++) {
if(myArray[ii] < myArray[jj]) {
int temp = myArray[ii];
myArray[ii] = myArray[jj];
myArray[jj] = temp;
}
}
printf("sorted element %d: %d\n", ii, myArray[ii]);
}
// we never "sort" the last number... it bubbled to the end:
printf("sorted element %d: %d\n", ii, myArray[ii]);
// now find 10% of the number of elements (rounded down)
// and we will have the number that is bigger than 90% of the numbers in the file
int index90 = 0.1 * numCount - 1; // automatically gets truncated;
// offset by 1 since index starts at 0
printf("The first number bigger than 90%% is element %d: it is %d\n", \
index90, myArray[index90]);
}
这里有几个“技巧”值得向新手程序员指出:
- 检查文件是否打开成功,如果没有则采取措施
- 使用
getline
(实际上是一个 gcc 扩展 - 我不知道你是否拥有它)安全地读取一行:它将确保缓冲区中有足够的空间。您的方法对您的文件有效 - 我的方法“通常更安全”。
- 用于
malloc
为数字数组分配足够的空间
- 即使我真的只需要对前 10% 进行排序来解决问题,我也会对“所有数字”进行排序。
ii
您可以通过更改外部排序循环中的上限来提高性能(对于这种情况) 。
int
我使用这样一个事实,即在计算我想要的数字的索引时,将浮点数分配给 an会自动截断它。
享受!