【发布时间】:2016-12-09 06:04:01
【问题描述】:
如何将一组最小或最大的数字组合到一个数组中?例如,如果我想在大小为 1000 的数组中找到最小的 10 个数字。
我正在使用 C 语言,但我不需要特定语言的答案。我只是想找出一种方法来处理这类任务,因为它最近经常出现。
【问题讨论】:
如何将一组最小或最大的数字组合到一个数组中?例如,如果我想在大小为 1000 的数组中找到最小的 10 个数字。
我正在使用 C 语言,但我不需要特定语言的答案。我只是想找出一种方法来处理这类任务,因为它最近经常出现。
【问题讨论】:
QuickSelect 算法允许分离预定义的最小和最大数字(无需完全排序)。它使用像快速排序算法这样的分区程序,但是当枢轴找到需要的位置时停止。
【讨论】:
int 数组进行排序是O(n)(通过基数排序),而QuickSelect 是O(n^2) 最坏的情况,我建议进行排序。
O(nlogk) 复杂性的二进制堆)。请注意,在一般情况下,Q/S 通常是首选排序。
您可以对数组进行快速排序并获取前 10 个元素。但这相当低效,因为您只对前 10 个元素感兴趣,并且为此对整个数组进行排序是过大的。
int lowerTen = malloc(size_of_array);
//'array' is your array with 1000 elements
for(int i=0; i<size_of_array; i++){
if(comesUnderLowerTen(array[i], lowerTeb)){
addTolowerTen(array[i], lowerTen)
}
}
int comesUnderLowerTen(int num, int *lowerTen){
//if there are not yet 10 elements in lowerTen, insert.
//else if 'num' is less than the largest element in lowerTen, insert.
}
void addToLowerTen(int num, int *lowerTen){
//should make sure that num is inserted at the right place in the array
//i.e, after inserting 'num' *lowerTen should remain sorted
}
不用说,这不是一个有效的例子。也只有在“lowerTen”数组需要维护少量元素的排序列表时才这样做。如果您需要 1000 个元素数组中的前 500 个元素,这不是首选方法。
这仅在您的原始 1000 元素数组被一一填充时才有效 - 在这种情况下,您可以在填充原始数组时维护“lowerTen”数组,而不是对 1000 元素数组进行线性遍历。
如果您可以根据原始数组维护像二叉搜索树这样的数据结构,则此类任务会更容易。但同样,在你的数组上构造一个 BST,然后找到前 10 个元素与对数组进行排序然后做同样的事情一样好。仅当您的用例需要搜索一个非常大的数组并且数据需要在内存中时才这样做。
【讨论】:
int lowerTen = (int*)malloc(size_of_array);。 1) 在 C 中,转换返回值只会使代码混乱。返回的类型是void*,可以分配给任何其他指针,2) malloc() 返回一个指针,但int lowerTen 是整数,而不是指针。
for(int i=0; i<array.length; i++){。在 C 中,数组没有 .length 属性。
实现一个优先队列。 循环遍历所有数字并将它们添加到该队列中。 如果该队列的长度等于 10,则开始检查当前数字是否低于该队列中的最高数字。 如果是,请删除该最高数字并添加当前数字。
毕竟,您将拥有一个优先级队列,其中包含阵列中 10 个最低的数字。 (所需时间应该是 O(n),其中 n 是数组的长度)。
如果您需要更多提示,请添加评论:)
【讨论】:
以下代码
现在是代码
#include <stdlib.h> // size_t
void selectLowest( int *sourceArray, size_t numItemsInSource, int *lowestDest, size_t numItemsInDest )
{
size_t maxIndex = 0;
int maxValue = 0;
// initially populate lowestDest array
for( size_t i=0; i<numItemsInDest; i++ )
{
lowestDest[i] = sourceArray[i];
if( maxValue < sourceArray[i] )
{
maxValue = sourceArray[i];
maxIndex = i;
}
}
// search rest of sourceArray and
// if lower than max in lowestDest,
// then
// replace
// find new max value
for( size_t i=numItemsInDest; i<numItemsInSource; i++ )
{
if( maxValue > sourceArray[i] )
{
lowestDest[maxIndex] = sourceArray[i];
maxIndex = 0;
maxValue = 0;
for( size_t j=0; j<numItemsInDest; j++ )
{
if( maxValue < lowestDest[j] )
{
maxValue = lowestDest[j];
maxIndex = j;
}
}
}
}
} // end function: selectLowest
【讨论】: