不进入while循环?

Not entering while loop?

[编辑] - 没关系,这是一个非常愚蠢的问题

我目前正在尝试创建一个程序,该程序基本上会告诉我何时插入排序比给定的 abn(在在这种情况下,2) 的连续幂。这是我目前所拥有的:

int comparisonSort()
{

//prompt user for values of a and b 
int a = 0;
int b = 0; 
cout << "Enter the value for a" << endl; 
cin >> a; 
cout << "Enter a value for b" << endl;
cin >> b;

double insertionSortTime = 0;
double mergeSortTime = 0;
double n = 2; 

cout << outside while loop" << endl;              //check

while (insertionSortTime < mergeSortTime)
{
    cout << "inside while loop" << endl;          //check

    //compute the insertion and merge sort execution times 
    insertionSortTime = a*n*n;
    mergeSortTime = b*n*log2(n);

    cout << "is = " << insertionSortTime << " ms = " << mergeSortTime << endl;

    n = pow(n, 2);  // n^2 
}

cout << "value of n that insertion sort beat merge sort is: " << n << endl;
return 0;
}

当我 运行 这个时,我得到:

Enter the value for a
8
Enter a value for b
64
outside while loop
value of n that insertion sort beat merge sort is: 2

我不知道为什么没有执行 while 循环...抱歉,如果这看起来是一个非常简单的问题,但我是 C++ 的新手,将不胜感激任何帮助,谢谢!

中的条件
while (insertionSortTime < mergeSortTime)
insertionSortTimemergeSortTime 都设置为零时,

在第一次迭代中是 false。这解释了为什么循环从未执行过。

也许你打算使用:

while (insertionSortTime <= mergeSortTime)

这是因为你有 insertionSortTime = 0mergeSortTime = 0 并且你的循环条件是 insertionSortTime < mergeSortTime.

当然0不是< 0所以它永远不会进入循环。

改为<=