不正确的比较和交换计数器输出用于快速排序功能
Incorrect Comparisons And Swaps Counter Output for Quicksort Function
我目前正在尝试实现一个c ++程序,该程序比较各种不同排序方法中的交换和比较次数。该程序似乎适用于所有排序方法(选择排序、插入排序(,但快速排序除外,无论列表的数据大小或顺序如何,它都只输出比较和交换计数 0。我在下面包含了完整的程序。快速排序函数肯定在工作,它唯一的计数元素并不奇怪,因为它使用外部比较和交换函数,旨在每次调用它们时增加适当的计数器。任何帮助将不胜感激。
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <string>
#include<limits>
using namespace std;
unsigned long long comparisons = 0;
unsigned long long swaps = 0;
bool comp_less(int a, int b){
++comparisons;
if ( comparisons == std::numeric_limits<unsigned long long>::max() )
{
std::cout << "Number of comparisons reached max value. Resetting it 0.n";
swaps = 0;
}
return a < b;
}
void swap(int& a, int& b)
{
++swaps;
if ( swaps == std::numeric_limits<unsigned long long>::max() )
{
std::cout << "Number of swaps reached max value. Resetting it 0.n";
swaps = 0;
}
int t = a;
a = b;
b = t;
}
void selectionSort(int *first, int *last)
{
for(int *i = first; i < (last - 1); ++i){
int *min = i;
for(int *j = i + 1; j < last; ++j){
if(comp_less(*j, *min)){
min = j;
}
}
swap(*i, *min);
}
}
void insertionSort(int* first, int* last)
{
for (int *i = first + 1; i < last; ++i)
{
int temp = *i;
int *j;
for (j = i; j > first && comp_less(temp, *(j - 1)); --j)
{
swap(*j, *(j - 1));
}
*j = temp;
}
}
int *partition(int *first, int *last)
{
int *pivot = last - 1;
int *i = first;
int *j = last - 1;
for (;;)
{
while (comp_less(*i, *pivot) && i < last)
{
++i;
}
while (*j >= *pivot && j > first)
{
--j;
}
if (i >= j)
break;
swap(*i, *j);
}
swap(*(last - 1), *i);
return i;
}
void quickSort(int* first, int* last) {
{
if ((first - last) <= 1)
return;
int *pivot = partition(first, last);
quickSort(first, pivot);
quickSort(pivot + 1, last);
}
}
int main(int argc, char** argv)
{
string algorithm = "selection";
string dataset = "random";
for (int c; (c = getopt(argc, argv, "ravqsin")) != -1;) {
switch (c) {
case 'r':
dataset = "random";
break;
case 'a':
dataset = "sorted";
break;
case 'v':
dataset = "reverse";
break;
case 'q':
algorithm = "quicksort";
break;
case 's':
algorithm = "selection";
break;
case 'i':
algorithm = "insertion";
break;
case 'n':
algorithm = "none";
break;
}
}
argc -= optind;
argv += optind;
const int size = argc > 0 ? atoi(argv[0]) : 10000;
int* data = new int[size];
if (dataset == "sorted") {
for (int i = 0; i < size; ++i) {
data[i] = i;
}
}
else if (dataset == "reverse") {
for (int i = 0; i < size; ++i) {
data[i] = size - i - 1;
}
}
else if (dataset == "random") {
for (int i = 0; i < size; ++i) {
data[i] = rand() % size;
}
}
if (algorithm == "quicksort") {
quickSort(data, data + size);
}
else if (algorithm == "selection") {
selectionSort(data, data + size);
}
else if (algorithm == "insertion") {
insertionSort(data, data + size);
}
else if (algorithm=="none"){
cout<< "Oops!" <<'n';
exit(1);
}
cout << "OK" << 'n';
cout << "Algorithm: " << algorithm << 'n';
cout << "Data set: " << dataset << 'n';
cout << "Size: " << size << 'n';
cout << "Swaps: " << swaps << 'n';
cout << "Comparisons: " << comparisons << 'n';
return 0;
}
在quickSort
函数中,更改
if ((first - last) <= 1)
自
if ((last - first) <= 1)
相关文章:
- 不正确的比较和交换计数器输出用于快速排序功能
- 用于输出矩阵的二维指针
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- C++ 重载"<<"不适用于输出"obja + objb"
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 二进制搜索不适用于特定输出
- 输出(用于 GraphViz)提升图顶点及其属性,使用具有私有变量的类作为捆绑属性
- 用于了解输入和输出流缓冲区实际工作方式的程序
- 用于计算周长和面积的程序,而不是从 void 函数输出值
- 在VS2017中使用整数用于输出debugstring
- 编译器将输出的流运算符<<解释为用于按位左移的二进制运算符<<
- 递归函数用于计算 n 个数字之和的意外输出
- 音频缓冲区列表,用于输出流格式已压缩的 AUHAL 单元
- C 两个线程一个用于输入,一个用于输出
- 用于输出 2D 矢量的语法
- 提升正则表达式 cpp 用于查找 %% 之间的字符串,输出不包括 % 字符本身
- 如何将我的输出用于另一个方程式
- VisualStudio代码C 终端用于输出
- 指针或局部变量,用于函数的输出参数
- 创建一个简单的窗口来输出用于调试的信息(日志样式)- wxWidgets