我需要一个高效的排序方法,不需要回调函数,但是与使用qsort()一样可定制。我希望它像迭代器一样工作,在循环中连续调用排序API,直到完成,在循环中进行比较,而不是在回调函数中进行。这样自定义比较就局限于调用函数(因此可以访问局部变量,可能更有效率等)。我已经为低效的选择排序实现了这个功能,但需要它更加高效,所以更喜欢快速排序的衍生版本。
有人做过类似的事情吗?我尝试过对快速排序进行操作,但试图颠倒算法太费神了。
以下是如何使用的示例。
有人做过类似的事情吗?我尝试过对快速排序进行操作,但试图颠倒算法太费神了。
以下是如何使用的示例。
// the array of data we are sorting
MyData array[5000], *firstP, *secondP;
// (assume data is filled in)
Sorter sorter;
// initialize sorter
int result = sortInit (&sorter, array, 5000,
(void **)&firstP, (void **)&secondP, sizeof(MyData));
// loop until complete
while (sortIteration (&sorter, result) == 0) {
// here's where we do the custom comparison...here we
// just sort by member "value" but we could do anything
result = firstP->value - secondP->value;
}