【发布时间】:2021-05-05 21:02:57
【问题描述】:
为什么这个快速排序算法看起来比 std::sort 更快?我已经检查以确保它实际上是在对数组进行排序。我还用具有相同迭代次数的空心 for 循环替换了两个排序调用,以测试计时基准并检查所有内容。
我还想知道我可以对快速排序进行哪些调整以使其递归更多次。也许某种可变内存管理?
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <chrono>
using namespace std;
void quickSort(int*, int);
void fillRandom(int*, int,int b2);
int main() {
//setup arrays
int size = 100000;
auto myints = new int[size];
auto myints2 = new int[size];
fillRandom(myints, size,10000);
std::copy(myints, myints + size, myints2);
//measurement 1
auto t1 = std::chrono::high_resolution_clock::now();
quickSort(myints, size);
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
std::cout << endl << "Execution 1 took: "<< duration << endl;
//measurement 2
t1 = std::chrono::high_resolution_clock::now();
std::sort(myints2,myints2+size);
t2 = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
std::cout << endl << "Execution 2 took: " << duration << endl;
cout << "finished!";
return 1;
}
void fillRandom(int* p, int size,int upTo) {
srand(time(0));
for (int i = 0;i < size;i++) {
p[i] = rand() % upTo + 1;
}
}
void quickSortSwap(int *p1, int*p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void quickSort(int* original, int len) {
int split = *original;
int greaterIndex = len - 1;
int lesserIndex = 1;
int* currentP;
//rearrange stuff so smaller is left, bigger is right
for (int i = 1;i < len;i++) {
currentP = original + lesserIndex;
//cout << *currentP << " compared to " << split << endl;
if (*currentP <= split) {
lesserIndex++;
}
else {
//cout << "greater: " << *currentP <<endl;
quickSortSwap(currentP, original + greaterIndex);
greaterIndex--;
}
}
//uhh, now we switch pivot element with the right most left side element. Adjust our left side length measurement accordingly.
lesserIndex--;
quickSortSwap(original, original + lesserIndex);
greaterIndex++;
//this point
if (lesserIndex > 1) {
quickSort(original, lesserIndex);
}
int greater_range = len - greaterIndex;
if (greater_range > 1) {
quickSort(original + greaterIndex, greater_range);
}
}
【问题讨论】:
-
您是否在启用优化的情况下进行编译?
-
@alter 当然不是,这样做之后谁会问关于 SO 的优化问题?
-
请注意,数组中的许多元素将相等。这可能会使比较有点偏颇。此外,我猜你检查你的快速排序是否提供了正确的结果;
-
我希望有一个常见问题解答或 SO 指南来解释发布性能问题时的适当要求。太多的问题要么被原始发布者“关闭”,而他们只需要打开优化,和/或“哎呀,我不知道”,当打开优化重新运行相同的测试时。
-
@PaulMcKenzie 在Meta Stack Overflow 上创建各自的主题。
标签: c++ algorithm optimization quicksort